C与CC类型对照表完整版.docx
《C与CC类型对照表完整版.docx》由会员分享,可在线阅读,更多相关《C与CC类型对照表完整版.docx(12页珍藏版)》请在冰豆网上搜索。
C与CC类型对照表完整版
集团标准化办公室:
[VV986T-J682P28-JP266L8-68PNN]
C与CC类型对照表
C#与C/C类型对照表
CC#
=====================================
WORDushort
DWORDuint
UCHARint/byte大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte
UCHAR*string/IntPtr
unsignedchar*[MarshalAs(UnmanagedType.LPArray)]byte[]/(Intptr)
char*string
LPCTSTRstring
LPTSTR[MarshalAs(UnmanagedType.LPTStr)]string
longint
ulonguint
HandleIntPtr
HWNDIntPtr
void*IntPtr
intint
int*refint
*intIntPtr
unsignedintuint
COLORREFuint
API与C#的数据类型对应关系表
API数据类型类型描述C#类型API数据类型类型描述C#类型
WORD16位无符号整数ushortCHAR字符char
LONG32位无符号整数intDWORDLONG64位长整数long
DWORD32位无符号整数uintHDC设备描述表句柄int
HANDLE句柄,32位整数intHGDIOBJGDI对象句柄int
UINT32位无符号整数uintHINSTANCE实例句柄int
BOOL32位布尔型整数boolHWM窗口句柄int
LPSTR指向字符的32位指针stringHPARAM32位消息参数int
LPCSTR指向常字符的32位指针StringLPARAM32位消息参数int
BYTE字节byteWPARAM32位消息参数int
BOOL=System.Int32
BOOLEAN=System.Int32
BYTE=System.UInt16
CHAR=System.Int16
COLORREF=System.UInt32
DWORD=System.UInt32
DWORD32=System.UInt32
DWORD64=System.UInt64
FLOAT=System.Float
HACCEL=System.IntPtr
HANDLE=System.IntPtr
HBITMAP=System.IntPtr
HBRUSH=System.IntPtr
HCONV=System.IntPtr
HCONVLIST=System.IntPtr
HCURSOR=System.IntPtr
HDC=System.IntPtr
HDDEDATA=System.IntPtr
HDESK=System.IntPtr
HDROP=System.IntPtr
HDWP=System.IntPtr
HENHMETAFILE=System.IntPtr
HFILE=System.IntPtr
HFONT=System.IntPtr
HGDIOBJ=System.IntPtr
HGLOBAL=System.IntPtr
HHOOK=System.IntPtr
HICON=System.IntPtr
HIMAGELIST=System.IntPtr
HIMC=System.IntPtr
HINSTANCE=System.IntPtr
HKEY=System.IntPtr
HLOCAL=System.IntPtr
HMENU=System.IntPtr
HMETAFILE=System.IntPtr
HMODULE=System.IntPtr
HMONITOR=System.IntPtr
HPALETTE=System.IntPtr
HPEN=System.IntPtr
HRGN=System.IntPtr
HRSRC=System.IntPtr
HSZ=System.IntPtr
HWINSTA=System.IntPtr
HWND=System.IntPtr
INT=System.Int32
INT32=System.Int32
INT64=System.Int64
LONG=System.Int32
LONG32=System.Int32
LONG64=System.Int64
LONGLONG=System.Int64
LPARAM=System.IntPtr
LPBOOL=System.Int16[]
LPBYTE=System.UInt16[]
LPCOLORREF=System.UInt32[]
LPCSTR=System.String
LPCTSTR=System.String
LPCVOID=System.UInt32
LPCWSTR=System.String
LPDWORD=System.UInt32[]
LPHANDLE=System.UInt32
LPINT=System.Int32[]
LPLONG=System.Int32[]
LPSTR=System.String
LPTSTR=System.String
LPVOID=System.UInt32
LPWORD=System.Int32[]
LPWSTR=System.String
LRESULT=System.IntPtr
PBOOL=System.Int16[]
PBOOLEAN=System.Int16[]
PBYTE=System.UInt16[]
PCHAR=System.Char[]
PCSTR=System.String
PCTSTR=System.String
PCWCH=System.UInt32
PCWSTR=System.UInt32
PDWORD=System.Int32[]
PFLOAT=System.Float[]
PHANDLE=System.UInt32
PHKEY=System.UInt32
PINT=System.Int32[]
PLCID=System.UInt32
PLONG=System.Int32[]
PLUID=System.UInt32
PSHORT=System.Int16[]
PSTR=System.String
PTBYTE=System.Char[]
PTCHAR=System.Char[]
PTSTR=System.String
PUCHAR=System.Char[]
PUINT=System.UInt32[]
PULONG=System.UInt32[]
PUSHORT=System.UInt16[]
PVOID=System.UInt32
PWCHAR=System.Char[]
PWORD=System.Int16[]
PWSTR=System.String
REGSAM=System.UInt32
SC_HANDLE=System.IntPtr
SC_LOCK=System.IntPtr
SHORT=System.Int16
SIZE_T=System.UInt32
SSIZE_=System.UInt32
TBYTE=System.Char
TCHAR=System.Char
UCHAR=System.Byte
UINT=System.UInt32
UINT32=System.UInt32
UINT64=System.UInt64
ULONG=System.UInt32
ULONG32=System.UInt32
ULONG64=System.UInt64
ULONGLONG=System.UInt64
USHORT=System.UInt16
WORD=System.UInt16
WPARAM=System.IntPtr
<---------补充----------->
Wtypes.h中的非托管类型非托管C语言类型托管类名说明
HANDLEvoid*System.IntPtr32位
BYTEunsignedcharSystem.Byte8位
SHORTshortSystem.Int1616位
WORDunsignedshortSystem.UInt1616位
INTintSystem.Int3232位
UINTunsignedintSystem.UInt3232位
LONGlongSystem.Int3232位
BOOLlongSystem.Int3232位
DWORDunsignedlongSystem.UInt3232位
ULONGunsignedlongSystem.UInt3232位
CHARcharSystem.Char用ANSI修饰。
LPSTRchar*System.String或System.StringBuilder用ANSI修饰。
LPCSTRConstchar*System.String或System.StringBuilder用ANSI修饰。
LPWSTRwchar_t*System.String或System.StringBuilder用Unicode修饰。
LPCWSTRConstwchar_t*System.String或System.StringBuilder用Unicode修饰。
FLOATFloatSystem.Single32位
DOUBLEDoubleSystem.Double64位
c#写程序有时需要调用c编译的dll,普通的调用方法比较简单。
有时调用回调函数就有些迷茫了。
通过c#的委托可以实现c的回调,简单介绍下:
1、首先添加usingSystem.Runtime.InteropServices;
引用c的dll
创建个委托:
publicdelegatevoidIndexFunc(intn,intindex);
[DllImport(@"D:
/SDK.dll")]
privatestaticexternboolInfoCallBack(intn,IndexFuncFunc,intindex);
publicboolIndexInfoCallBack(intn,IndexFuncFunc,intindex)
{
returnInfoCallBack(n,Func,index);
}
2、窗体调用
privatevoidbutton1_Click(objectsender,EventArgse)
{
IndexFuncfunc=newIndexFunc(ssss);
boola=IndexInfoCallBack(0,func,0);
}
当执行IndexInfoCallBack的时候就实现callback并将回调回来的函数付给ssss方法中的m,n
publicvoidssss(intm,intn)
{
MessageBox.Show(m.ToString()n.ToString());
}
*****************************************************************************************************
*****************************************************************************************************
*****************************************************************************************************
在c#里调用C的dll时,需要注意的一些问题
在c#里调用C的dll,遇到了一些头疼的问题:
C里头文件定义形势如下:
typedefvoid(*CALLBACKFUN1W)(wchar_t*,void*pArg);
typedefvoid(*CALLBACKFUN1A)(char*,void*pArg);
boolBIOPRINT_SENSOR_APIdllFun1(CALLBACKFUN1pCallbackFun1,void*pArg);
在其中一个导入的dll方法里,有一个回调函数的参数
[DllImport("test.dll",EntryPoint="dllFunc1",CharSet=CharSet.Unicode)]
publicstaticexternbooldllFunc1([MarshalAs(UnmanagedType.FunctionPtr)]CallbackFunc1pCallbackFunc1,IntPtrpArg);
回调函数在C#里定义成委托如下:
publicdelegatevoidCallbackFunc1(StringBuilderstrName,IntPtrpArg);
调试运行,报错。
有时是直接出错退出,信息如下:
Bufferoverrundetected!
Program:
...
Abufferoverrunhasbeendetectedwhichhascorruptedtheprogram'sinternalstate.Theprogramcannotsafelycontinueexecutionandmustnowbeterminated.
有时则能运行起来,但会抛出异常:
System.AccessViolationException:
尝试读取或写入受保护的内存。
这通常指示其他内存已损坏。
几经周折,觅得答案,原来是要指定调用方式,如下就OK了:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
publicdelegatevoidCallbackFunc1(IntPtrhWnd,IntPtrpArg);
而系统默认方式为CallingConvention.StdCall。
程序终于不报错了,但是又出现结果不对了
定义成如下时,strName在方法中的值,只有一个字符,
publicdelegatevoidCallbackFunc1(StringBuilderstrName,IntPtrpArg);
后来改为:
publicdelegatevoidCallbackFunc1([MarshalAs(UnmanagedType.LPWStr)]StringBuilderstrName,IntPtrpArg);
OK了,strName带出来的值完整了,参数类型定义成string或者StringBuilder都无所谓
还可以用IntPtr,或者char*都行(用char*得加unsafe)
char*类型的,得到值后,可循环至'\0'得到整个字符串
IntPtr类型的,可以用Marshal.Copy出来,如:
Marshal.Copy(IntPtr_source,toBytes,0,1024);
如果报
“尝试读取或写入受保护的内存。
这通常指示其他内存已损坏。
”
异常,
还有可能是因为C和C#的参数类型对应问题,
如:
bool__declspec(dllimport)getImage(unsignedchar**ppImage,int&nWidth,int&nHeight);
对应成
[DllImport("test.dll")]
publicstaticexternboolgetImage(IntPtrppImage,refintnWidth,refintnHeight);
时,
则该方法在调用前,要对传入的ppImage分配空间,如下
IntPtrpImage=Marshal.AllocHGlobal(iWidth*iHeight);
这种方法不推荐,因为是带出结果来,一般这种指针不确定需要分配多大空间的。
正确的要对应成下面这样:
[DllImport("test.dll")]
publicstaticexternboolgetImage(refIntPtrppImage,refintnWidth,refintnHeight);
调用时只要定义就行了:
IntPtrpImage=newIntPtr();
intrefWidth=0,refHeight=0;
getImage(refpImage,refrefWidth,refrefHeight);
总结,凡是双针指类型参数,可以用refIntPtr
而对于int*,int&,则都可用refint对应
另外,提一下自定义消息的响应
publicconstintWM_USER=0x0400;
publicconstintWM_TEST_MSG=(WM_USER0x100);
C#要响应dll的自定义消息,则要重写WinForm的DefWndProc方法。
protectedoverridevoidDefWndProc(refMessagem)
{
switch(m.Msg)
{
caseWM_TEST_MSG:
{
}
break;
default:
base.DefWndProc(refm);
break;
}
}
消息发送是通过Windows提供的API函数SendMessage来实现的,它的原型定义:
[DllImport("User32.dll",EntryPoint="SendMessage")]
privatestaticexternintSendMessage(
IntPtrhWnd,//handletodestinationwindow
uintMsg,//message
uintwParam,//firstmessageparameter
uintlParam//secondmessageparameter
);
再转贴一篇相关文章:
C#中调用WindowsAPI的要点
在.NetFrameworkSDK文档中,关于调用WindowsAPI的指示比较零散,并且其中稍全面一点的是针对VisualBasic.net讲述的。
本文将C#中调用API的要点汇集如下,希望给未在C#中使用过API的朋友一点帮助。
另外如果安装了VisualStudio.net的话,在C:
\ProgramFiles\MicrosoftVisualStudio.NET\FrameworkSDK\Samples\Technologies\Interop\PlatformInvoke\WinAPIs\CS目录下有大量的调用API的例子。
一、调用格式
usingSystem.Runtime.InteropServices;//引用此名称空间,简化后面的代码
...
//使用DllImportAttribute特性来引入api函数,注意声明的是空方法,即方法体为空。
[DllImport("user32.dll")]
publicstaticexternReturnTypeFunctionName(typearg1,typearg2,...);
//调用时与调用其他方法并无区别
可以使用字段进一步说明特性,用逗号隔开,如:
[DllImport("kernel32",EntryPoint="GetVersionEx")]
DllImportAttribute特性的公共字段如下:
1、CallingConvention指示向非托管实现传递方法参数时所用的CallingConvention值。
CallingConvention.Cdecl:
调用方清理堆栈。
它使您能够调用具有varargs的函数。
CallingConvention.StdCall:
被调用方清理堆栈。
它是从托管代码调用非托管函数的默认约定。
2、CharSet控制调用函数的名称版本及指示如何向方法封送String参数。
此字段被设置为CharSet值之一。
如果CharSet字段设置为Unicode,则所有字符串参数在传递到非托管实现之前都转换成Unicode字符。
这还导致向DLLEntryPoint的名称中追加字母“W”。
如果此字段设置为Ansi,则字符串将转换成ANSI字符串,同时向DLLEntryPoint的名称中追加字母“A”。
大多数Win32API使用这种追加“W”或“A”的约定。
如果CharSet设置为Auto,则这种转换就是与平台有关的(在WindowsNT上为Unicode,在Windows98上为Ansi)。
CharSet的默认值为Ansi。
CharSet字段也用于确定将从指定的DLL导入哪个版本的函数。
CharSet.Ansi和CharSet.Unicode的名称匹配规则大不相同。
对于Ansi来说,如果将EntryPoint设置为“MyMethod”且它存在的话,则返回“MyMethod”。
如果DLL中没有“MyMethod”,但存在“MyMethodA”,则返回“MyMethodA”。
对于Unicode来说则正好相反。
如果将EntryPoint设置为“MyMethod”且它存在的话,则返回“MyMethodW”。
如果DLL中不存在“MyMethodW”,但存在“MyMethod”,则返回“MyMethod”。
如果使用的是Auto,则匹配规则与平台有关(在WindowsNT上为Unicode,在Windows98上为Ansi)。
如果ExactSpelling设置为true,则只有当DLL中存在“MyMethod”时才返回“MyMethod”。
3、EntryPoint指示要调用的DLL入口点的名称或序号。
如果你的方法名不想与api函数同名的话,一定要指定此参数,例如:
[DllImport("user32.dll",CharSet="CharSet.Auto",Entr