本以为这篇搜集整理的代码会是很不错的文章,花了一天时间,搜索到最后居然出来一篇叫做"C# 与 C++ 数据类型对照表"的文章.几乎囊括掉和大部分的数据了,太打击我了. 本文中有部分的数据没有测试.也有一些不错的是看了上百篇网文对比整理得来的.希望有帮助.


  1. /C++中的DLL函数原型为  

  2. //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsigned char* 变量名2)

  3. //extern "C" __declspec(dllexport) bool 方法名二(const unsigned char* 变量名1, char* 变量名2)

  4. //C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试

  5. //c++:HANDLE(void   *)          ----    c#:System.IntPtr

  6. //c++:Byte(unsigned   char)     ----    c#:System.Byte

  7. //c++:SHORT(short)              ----    c#:System.Int16

  8. //c++:WORD(unsigned   short)    ----    c#:System.UInt16

  9. //c++:INT(int)                  ----    c#:System.Int16

  10. //c++:INT(int)                  ----    c#:System.Int32

  11. //c++:UINT(unsigned   int)      ----    c#:System.UInt16

  12. //c++:UINT(unsigned   int)      ----    c#:System.UInt32

  13. //c++:LONG(long)                ----    c#:System.Int32

  14. //c++:ULONG(unsigned   long)    ----    c#:System.UInt32

  15. //c++:DWORD(unsigned   long)    ----    c#:System.UInt32

  16. //c++:DECIMAL                   ----    c#:System.Decimal

  17. //c++:BOOL(long)                ----    c#:System.Boolean

  18. //c++:CHAR(char)                ----    c#:System.Char

  19. //c++:LPSTR(char   *)           ----    c#:System.String

  20. //c++:LPWSTR(wchar_t   *)       ----    c#:System.String

  21. //c++:LPCSTR(const   char   *)  ----    c#:System.String

  22. //c++:LPCWSTR(const   wchar_t   *)      ----    c#:System.String

  23. //c++:PCAHR(char   *)   ----    c#:System.String

  24. //c++:BSTR              ----    c#:System.String

  25. //c++:FLOAT(float)      ----    c#:System.Single

  26. //c++:DOUBLE(double)    ----    c#:System.Double

  27. //c++:VARIANT           ----    c#:System.Object

  28. //c++:PBYTE(byte   *)   ----    c#:System.Byte[]

  29. //c++:BSTR      ----    c#:StringBuilder

  30. //c++:LPCTSTR   ----    c#:StringBuilder

  31. //c++:LPCTSTR   ----    c#:string

  32. //c++:LPTSTR    ----    c#:[MarshalAs(UnmanagedType.LPTStr)] string

  33. //c++:LPTSTR 输出变量名    ----    c#:StringBuilder 输出变量名

  34. //c++:LPCWSTR   ----    c#:IntPtr

  35. //c++:BOOL      ----    c#:bool  

  36. //c++:HMODULE   ----    c#:IntPtr  

  37. //c++:HINSTANCE ----    c#:IntPtr

  38. //c++:结构体    ----    c#:public struct 结构体{};

  39. //c++:结构体 **变量名   ----    c#:out 变量名   //C#中提前申明一个结构体实例化后的变量名

  40. //c++:结构体 &变量名    ----    c#:ref 结构体 变量名

  41. //c++:WORD      ----    c#:ushort

  42. //c++:DWORD     ----    c#:uint

  43. //c++:DWORD     ----    c#:int

  44. //c++:UCHAR     ----    c#:int

  45. //c++:UCHAR     ----    c#:byte

  46. //c++:UCHAR*    ----    c#:string

  47. //c++:UCHAR*    ----    c#:IntPtr

  48. //c++:GUID      ----    c#:Guid

  49. //c++:Handle    ----    c#:IntPtr

  50. //c++:HWND      ----    c#:IntPtr

  51. //c++:DWORD     ----    c#:int

  52. //c++:COLORREF  ----    c#:uint

  53. //c++:unsigned char     ----    c#:byte

  54. //c++:unsigned char *   ----    c#:ref byte

  55. //c++:unsigned char *   ----    c#:[MarshalAs(UnmanagedType.LPArray)] byte[]

  56. //c++:unsigned char *   ----    c#:[MarshalAs(UnmanagedType.LPArray)] Intptr

  57. //c++:unsigned char &   ----    c#:ref byte

  58. //c++:unsigned char 变量名      ----    c#:byte 变量名

  59. //c++:unsigned short 变量名     ----    c#:ushort 变量名

  60. //c++:unsigned int 变量名       ----    c#:uint 变量名

  61. //c++:unsigned long 变量名      ----    c#:ulong 变量名

  62. //c++:char 变量名       ----    c#:byte 变量名   //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示

  63. //c++:char 数组名[数组大小]     ----    c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)]        public string 数组名; ushort

  64. //c++:char *            ----    c#:string       //传入参数

  65. //c++:char *            ----    c#:StringBuilder//传出参数

  66. //c++:char *变量名      ----    c#:ref string 变量名

  67. //c++:char *输入变量名  ----    c#:string 输入变量名

  68. //c++:char *输出变量名  ----    c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名

  69. //c++:char **           ----    c#:string

  70. //c++:char **变量名     ----    c#:ref string 变量名

  71. //c++:const char *      ----    c#:string

  72. //c++:char[]            ----    c#:string

  73. //c++:char 变量名[数组大小]     ----    c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名;

  74. //c++:struct 结构体名 *变量名   ----    c#:ref 结构体名 变量名

  75. //c++:委托 变量名   ----    c#:委托 变量名

  76. //c++:int       ----    c#:int

  77. //c++:int       ----    c#:ref int

  78. //c++:int &     ----    c#:ref int

  79. //c++:int *     ----    c#:ref int      //C#中调用前需定义int 变量名 = 0;

  80. //c++:*int      ----    c#:IntPtr

  81. //c++:int32 PIPTR *     ----    c#:int32[]

  82. //c++:float PIPTR *     ----    c#:float[]

  83. //c++:double** 数组名          ----    c#:ref double 数组名

  84. //c++:double*[] 数组名          ----    c#:ref double 数组名

  85. //c++:long          ----    c#:int

  86. //c++:ulong         ----    c#:int

  87. //c++:UINT8 *       ----    c#:ref byte       //C#中调用前需定义byte 变量名 = new byte();      

  88. //c++:handle    ----    c#:IntPtr

  89. //c++:hwnd      ----    c#:IntPtr

  90. //c++:void *    ----    c#:IntPtr      

  91. //c++:void * user_obj_param    ----    c#:IntPtr user_obj_param

  92. //c++:void * 对象名称    ----    c#:([MarshalAs(UnmanagedType.AsAny)]Object 对象名称

  93. //c++:char, INT8, SBYTE, CHAR                               ----    c#:System.SByte

  94. //c++:short, short int, INT16, SHORT                        ----    c#:System.Int16

  95. //c++:int, long, long int, INT32, LONG32, BOOL , INT        ----    c#:System.Int32

  96. //c++:__int64, INT64, LONGLONG                              ----    c#:System.Int64

  97. //c++:unsigned char, UINT8, UCHAR , BYTE                    ----    c#:System.Byte

  98. //c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t             ----    c#:System.UInt16

  99. //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT      ----    c#:System.UInt32

  100. //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG                            ----    c#:System.UInt64

  101. //c++:float, FLOAT                                                              ----    c#:System.Single

  102. //c++:double, long double, DOUBLE                                               ----    c#:System.Double

  103. //Win32 Types        ----  CLR Type

  104. //Struct需要在C#里重新定义一个Struct

  105. //CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str);

  106. //unsigned char** ppImage替换成IntPtr ppImage

  107. //int& nWidth替换成ref int nWidth

  108. //int*, int&, 则都可用 ref int 对应

  109. //双针指类型参数,可以用 ref IntPtr

  110. //函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double  fun_type1(double);

  111. //char* 的操作c++: char*; 对应 c#:StringBuilder;

  112. //c#中使用指针:在需要使用指针的地方 加 unsafe

  113. //unsigned   char对应public   byte

  114. /*

  115.         * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg);

  116.         * typedef void (*CALLBACKFUN1A)(char*, void* pArg);

  117.         * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg);

  118.         * 调用方式为

  119.         * [UnmanagedFunctionPointer(CallingConvention.Cdecl)]

  120.         * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg);

  121.         *

  122.         *

  123.         */

  1. C++              C#  

  2. =====================================  

  3. WORD             ushort

  4. DWORD             uint

  5. UCHAR             int/byte   大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte  

  6. UCHAR*             string/IntPtr  

  7. unsigned char*          [MarshalAs(UnmanagedType.LPArray)]byte[]/?(Intptr)  

  8. char*             string

  9. LPCTSTR             string

  10. LPTSTR             [MarshalAs(UnmanagedType.LPTStr)] string

  11. longint

  12. ulonguint

  13. Handle             IntPtr  

  14. HWND             IntPtr  

  15. void*             IntPtr  

  16. intint

  17. int*             refint

  18. *int             IntPtr  

  19. unsigned intuint

  20. COLORREF                uint

  21. API与C#的数据类型对应关系表  

  22. API数据类型 类型描述   C#类型  API数据类型  类型描述  C#类型  

  23. WORD 16位无符号整数   ushort  CHAR   字符   char

  24. LONG 32位无符号整数   int  DWORDLONG  64位长整数  long

  25. DWORD 32位无符号整数   uint  HDC   设备描述表句柄  int

  26. HANDLE 句柄,32位整数   int  HGDIOBJ GDI 对象句柄  int

  27. UINT 32位无符号整数   uint  HINSTANCE  实例句柄  int

  28. BOOL 32位布尔型整数   bool  HWM   窗口句柄  int

  29. LPSTR 指向字符的32位指针  string  HPARAM   32位消息参数  int

  30. LPCSTR 指向常字符的32位指针  String  LPARAM   32位消息参数  int

  31. BYTE 字节    byte  WPARAM   32位消息参数  int

  32. BOOL=System.Int32  

  33. BOOLEAN=System.Int32  

  34. BYTE=System.UInt16  

  35. CHAR=System.Int16  

  36. COLORREF=System.UInt32  

  37. DWORD=System.UInt32  

  38. DWORD32=System.UInt32  

  39. DWORD64=System.UInt64  

  40. FLOAT=System.Float  

  41. HACCEL=System.IntPtr  

  42. HANDLE=System.IntPtr  

  43. HBITMAP=System.IntPtr  

  44. HBRUSH=System.IntPtr  

  45. HCONV=System.IntPtr  

  46. HCONVLIST=System.IntPtr  

  47. HCURSOR=System.IntPtr  

  48. HDC=System.IntPtr  

  49. HDDEDATA=System.IntPtr  

  50. HDESK=System.IntPtr  

  51. HDROP=System.IntPtr  

  52. HDWP=System.IntPtr  

  53. HENHMETAFILE=System.IntPtr  

  54. HFILE=System.IntPtr  

  55. HFONT=System.IntPtr  

  56. HGDIOBJ=System.IntPtr  

  57. HGLOBAL=System.IntPtr  

  58. HHOOK=System.IntPtr  

  59. HICON=System.IntPtr  

  60. HIMAGELIST=System.IntPtr  

  61. HIMC=System.IntPtr  

  62. HINSTANCE=System.IntPtr  

  63. HKEY=System.IntPtr  

  64. HLOCAL=System.IntPtr  

  65. HMENU=System.IntPtr  

  66. HMETAFILE=System.IntPtr  

  67. HMODULE=System.IntPtr  

  68. HMONITOR=System.IntPtr  

  69. HPALETTE=System.IntPtr  

  70. HPEN=System.IntPtr  

  71. HRGN=System.IntPtr  

  72. HRSRC=System.IntPtr  

  73. HSZ=System.IntPtr  

  74. HWINSTA=System.IntPtr  

  75. HWND=System.IntPtr  

  76. INT=System.Int32  

  77. INT32=System.Int32  

  78. INT64=System.Int64  

  79. LONG=System.Int32  

  80. LONG32=System.Int32  

  81. LONG64=System.Int64  

  82. LONGLONG=System.Int64  

  83. LPARAM=System.IntPtr  

  84. LPBOOL=System.Int16[]  

  85. LPBYTE=System.UInt16[]  

  86. LPCOLORREF=System.UInt32[]  

  87. LPCSTR=System.String  

  88. LPCTSTR=System.String  

  89. LPCVOID=System.UInt32  

  90. LPCWSTR=System.String  

  91. LPDWORD=System.UInt32[]  

  92. LPHANDLE=System.UInt32  

  93. LPINT=System.Int32[]  

  94. LPLONG=System.Int32[]  

  95. LPSTR=System.String  

  96. LPTSTR=System.String  

  97. LPVOID=System.UInt32  

  98. LPWORD=System.Int32[]  

  99. LPWSTR=System.String  

  100. LRESULT=System.IntPtr  

  101. PBOOL=System.Int16[]  

  102. PBOOLEAN=System.Int16[]  

  103. PBYTE=System.UInt16[]  

  104. PCHAR=System.Char[]  

  105. PCSTR=System.String  

  106. PCTSTR=System.String  

  107. PCWCH=System.UInt32  

  108. PCWSTR=System.UInt32  

  109. PDWORD=System.Int32[]  

  110. PFLOAT=System.Float[]  

  111. PHANDLE=System.UInt32  

  112. PHKEY=System.UInt32  

  113. PINT=System.Int32[]  

  114. PLCID=System.UInt32  

  115. PLONG=System.Int32[]  

  116. PLUID=System.UInt32  

  117. PSHORT=System.Int16[]  

  118. PSTR=System.String  

  119. PTBYTE=System.Char[]  

  120. PTCHAR=System.Char[]  

  121. PTSTR=System.String  

  122. PUCHAR=System.Char[]  

  123. PUINT=System.UInt32[]  

  124. PULONG=System.UInt32[]  

  125. PUSHORT=System.UInt16[]  

  126. PVOID=System.UInt32  

  127. PWCHAR=System.Char[]  

  128. PWORD=System.Int16[]  

  129. PWSTR=System.String  

  130. REGSAM=System.UInt32  

  131. SC_HANDLE=System.IntPtr  

  132. SC_LOCK=System.IntPtr  

  133. SHORT=System.Int16  

  134. SIZE_T=System.UInt32  

  135. SSIZE_=System.UInt32  

  136. TBYTE=System.Char  

  137. TCHAR=System.Char  

  138. UCHAR=System.Byte  

  139. UINT=System.UInt32  

  140. UINT32=System.UInt32  

  141. UINT64=System.UInt64  

  142. ULONG=System.UInt32  

  143. ULONG32=System.UInt32  

  144. ULONG64=System.UInt64  

  145. ULONGLONG=System.UInt64  

  146. USHORT=System.UInt16  

  147. WORD=System.UInt16  

  148. WPARAM=System.IntPtr  

  149. Wtypes.h 中的非托管类型    非托管C 语言类型     托管类名           说明  

  150. HANDLE                     void*                System.IntPtr      32 位  

  151. BYTE                       unsigned char        System.Byte        8 位  

  152. SHORT                      short                System.Int16       16 位  

  153. WORD                       unsigned short       System.UInt16      16 位  

  154. INT                        int                  System.Int32       32 位  

  155. UINT                       unsigned int         System.UInt32      32 位  

  156. LONG                       long                 System.Int32       32 位  

  157. BOOL                       long                 System.Int32       32 位  

  158. DWORD                      unsigned long        System.UInt32      32 位  

  159. ULONG                      unsigned long       System.UInt32      32 位  

  160. CHAR                       char                 System.Char        用 ANSI 修饰。  

  161. LPSTR                      char*                System.String 或 System.StringBuilder  用 ANSI 修饰。  

  162. LPCSTR                     Const char*          System.String 或 System.StringBuilder  用 ANSI 修饰。  

  163. LPWSTR                     wchar_t*             System.String 或 System.StringBuilder  用 Unicode 修饰。  

  164. LPCWSTR                    Const wchar_t*     System.String 或 System.StringBuilder  用 Unicode 修饰。  

  165. FLOAT                      Float                System.Single     32 位  

  166. DOUBLE                     Double               System.Double     64 位  

  167. C/C++中的结构类型数据在C#下的转换  

  168. 在做项目移植的时候,经常会碰到数据类型的转换,而我这一次碰到的是C/C++中的结构怎样转换到C#。折腾了一个晚上终于有一个完美的方案。  

  169. 例如我们在C/C++下的结构数据如下:  

  170. typedef struct

  171. {  

  172. char  sLibName[ 256 ];  

  173. char  sPathToLibrary[ 256 ];  

  174.    INT32       iEntries;  

  175.    INT32       iUsed;  

  176.    UINT16     iSort;  

  177.    UINT16     iVersion;  

  178.    BOOLEAN     fContainsSubDirectories;  

  179.    INT32       iReserved;  

  180. } LIBHEADER;  

  181. 我们想把它转成C#下的结构类型如下:  

  182. publicstruct LIBHEADER  

  183.    {  

  184. publicchar[] sLibName;  

  185. publicchar[] sPathToLibrary;  

  186. public Int32 iEntries;  

  187. public Int32 iUsed;  

  188. public UInt16 iSort;  

  189. public UInt16 iVersion;  

  190. public Boolean fContainsSubDirectories;  

  191. public Int32 iReserved;  

  192.    }  

  193. 看上去好像没问题了,呵呵呵,其实这样是不行的,我们得再给C#编译器一些信息,告诉它一些字符数组的大小。然后它们在C#下面长得样子就变成这样:  

  194.    [StructLayout(LayoutKind.Sequential)]  

  195. publicstruct LIBHEADER  

  196.    {  

  197.        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]  

  198. publicchar[] sLibName;  

  199.        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]  

  200. publicchar[] sPathToLibrary;  

  201. public Int32 iEntries;  

  202. public Int32 iUsed;  

  203. public UInt16 iSort;  

  204. public UInt16 iVersion;  

  205. public Boolean fContainsSubDirectories;  

  206. public Int32 iReserved;  

  207.    }  

  208. 然后写一个函数负责转换。  

  209. public StructType ConverBytesToStructure<StructType>(byte[] bytesBuffer)  

  210.        {  

  211. // 检查长度。

  212. if (bytesBuffer.Length != Marshal.SizeOf(typeof(StructType)))  

  213.            {  

  214. thrownew ArgumentException("bytesBuffer参数和structObject参数字节长度不一致。");  

  215.            }  

  216.            IntPtr bufferHandler = Marshal.AllocHGlobal(bytesBuffer.Length);  

  217. for (int index = 0; index < bytesBuffer.Length; index++)  

  218.            {  

  219.                Marshal.WriteByte(bufferHandler, index, bytesBuffer[index]);  

  220.            }  

  221.            StructType structObject = (StructType)Marshal.PtrToStructure(bufferHandler, typeof(StructType));  

  222.            Marshal.FreeHGlobal(bufferHandler);  

  223. return structObject;  

  224.        }  

  225. 然后我们的函数用例是这样:  

  226.     FileStream file = File.OpenRead(@"D:/Jagged Alliance 2 Gold/INSTALL.LOG");  

  227. byte[] buffer = newbyte[Marshal.SizeOf(typeof(LIBHEADER))];  

  228.     file.Read(buffer, 0, buffer.Length);  

  229. LIBHEADER testValue = CommonTools.ConverBytesToStructure<LIBHEADER>(buffer);  

  230. string libName = newstring(testValue.sLibName);  

  231. string pathToLibrary= newstring(testValue.sPathToLibrary);  

  232. OK,搞定。  

  233. 如果想去掉后面两句的char数组的转换哪代码如下  

  234. C#中的结构代码  

  235.    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]  

  236. publicstruct LIBHEADER  

  237.    {  

  238.        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]  

  239. publicstring sLibName;  

  240.        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]  

  241. publicstring sPathToLibrary;  

  242. public Int32 iEntries;  

  243. public Int32 iUsed;  

  244. public UInt16 iSort;  

  245. public UInt16 iVersion;  

  246. public Boolean fContainsSubDirectories;  

  247. public Int32 iReserved;  

  248.    }  

  249. 其它代码不用作修改便可使用。  

参考原文:http://blog.csdn.net/jasonleesjtu/article/details/7837813