C# 与 CPP 类型映射

 

  1. //c++:HANDLE(void   *)                          ----    c#:System.IntPtr 
  2. //c++:Byte(unsigned   char)                     ----    c#:System.Byte 
  3. //c++:SHORT(short)                              ----    c#:System.Int16 
  4. //c++:WORD(unsigned   short)                    ----    c#:System.UInt16 
  5. //c++:INT(int)                                  ----    c#:System.Int16 
  6. //c++:INT(int)                                  ----    c#:System.Int32 
  7. //c++:UINT(unsigned   int)                      ----    c#:System.UInt16 
  8. //c++:UINT(unsigned   int)                      ----    c#:System.UInt32 
  9. //c++:LONG(long)                                ----    c#:System.Int32 
  10. //c++:ULONG(unsigned   long)                    ----    c#:System.UInt32 
  11. //c++:DWORD(unsigned   long)                    ----    c#:System.UInt32 
  12. //c++:DECIMAL                                   ----    c#:System.Decimal 
  13. //c++:BOOL(long)                                ----    c#:System.Boolean 
  14. //c++:CHAR(char)                                ----    c#:System.Char 
  15. //c++:LPSTR(char   *)                           ----    c#:System.String 
  16. //c++:LPWSTR(wchar_t   *)                       ----    c#:System.String 
  17. //c++:LPCSTR(const   char   *)                  ----    c#:System.String 
  18. //c++:LPCWSTR(const   wchar_t   *)              ----    c#:System.String 
  19. //c++:PCAHR(char   *)                           ----    c#:System.String 
  20. //c++:BSTR                                      ----    c#:System.String 
  21. //c++:FLOAT(float)                              ----    c#:System.Single 
  22. //c++:DOUBLE(double)                            ----    c#:System.Double 
  23. //c++:VARIANT                                   ----    c#:System.Object 
  24. //c++:PBYTE(byte   *)                           ----    c#:System.Byte[] 
  25.  
  26. //c++:BSTR                                      ----    c#:StringBuilder 
  27. //c++:LPCTSTR                                   ----    c#:StringBuilder 
  28. //c++:LPCTSTR                                   ----    c#:string 
  29. //c++:LPTSTR                                    ----    c#:[MarshalAs(UnmanagedType.LPTStr)] string 
  30. //c++:LPTSTR 输出变量名                         ----    c#:StringBuilder 输出变量名 
  31. //c++:LPCWSTR                                   ----    c#:IntPtr 
  32. //c++:BOOL                                      ----    c#:bool  
  33. //c++:HMODULE                                   ----    c#:IntPtr   
  34. //c++:HINSTANCE                                 ----    c#:IntPtr 
  35. //c++:结构体                                    ----    c#:public struct 结构体{}; 
  36. //c++:结构体 **变量名                           ----    c#:out 变量名   //C#中提前申明一个结构体实例化后的变量名 
  37. //c++:结构体 &变量名                            ----    c#:ref 结构体 变量名 
  38. //c++:WORD                                      ----    c#:ushort 
  39. //c++:DWORD                                     ----    c#:uint 
  40. //c++:DWORD                                     ----    c#:int 
  41. //c++:UCHAR                                     ----    c#:int 
  42. //c++:UCHAR                                     ----    c#:byte 
  43. //c++:UCHAR*                                    ----    c#:string 
  44. //c++:UCHAR*                                    ----    c#:IntPtr 
  45. //c++:GUID                                      ----    c#:Guid 
  46. //c++:Handle                                    ----    c#:IntPtr 
  47. //c++:HWND                                      ----    c#:IntPtr 
  48. //c++:DWORD                                     ----    c#:int 
  49. //c++:COLORREF                                  ----    c#:uint 
  50.  
  51.  
  52. //c++:unsigned char                             ----    c#:byte 
  53. //c++:unsigned char *                           ----    c#:ref byte 
  54. //c++:unsigned char *                           ----    c#:[MarshalAs(UnmanagedType.LPArray)] byte[] 
  55. //c++:unsigned char *                           ----    c#:[MarshalAs(UnmanagedType.LPArray)] Intptr 
  56.  
  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.  
  63. //c++:char 变量名                               ----    c#:byte 变量名   //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示 
  64. //c++:char 数组名[数组大小]                     ----    c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst         =         数组大小)]        public string 数组名; ushort 
  65.  
  66. //c++:char *                                    ----    c#:string       //传入参数 
  67. //c++:char *                                    ----    c#:StringBuilder//传出参数 
  68. //c++:char *变量名                              ----    c#:ref string 变量名 
  69. //c++:char *输入变量名                          ----    c#:string 输入变量名 
  70. //c++:char *输出变量名                          ----    c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名 
  71.  
  72. //c++:char **                                   ----    c#:string 
  73. //c++:char **变量名                             ----    c#:ref string 变量名 
  74. //c++:const char *                              ----    c#:string 
  75. //c++:char[]                                    ----    c#:string 
  76. //c++:char 变量名[数组大小]                     ----    c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst        =        数组大小)] public string 变量名; 
  77.  
  78. //c++:struct 结构体名 *变量名                   ----    c#:ref 结构体名 变量名 
  79. //c++:委托 变量名                               ----    c#:委托 变量名 
  80.  
  81. //c++:int                                       ----    c#:int 
  82. //c++:int                                       ----    c#:ref int 
  83. //c++:int &                                     ----    c#:ref int 
  84. //c++:int *                                     ----    c#:ref int      //C#中调用前需定义int 变量名         =         0; 
  85.  
  86. //c++:*int                                      ----    c#:IntPtr 
  87. //c++:int32 PIPTR *                             ----    c#:int32[] 
  88. //c++:float PIPTR *                             ----    c#:float[] 
  89.        
  90.  
  91. //c++:double** 数组名                           ----    c#:ref double 数组名 
  92. //c++:double*[] 数组名                          ----    c#:ref double 数组名 
  93. //c++:long                                      ----    c#:int 
  94. //c++:ulong                                     ----    c#:int 
  95.        
  96. //c++:UINT8 *                                   ----    c#:ref byte       //C#中调用前需定义byte 变量名         =         new byte();       
  97.  
  98.  
  99. //c++:handle                                    ----    c#:IntPtr 
  100. //c++:hwnd                                      ----    c#:IntPtr       
  101. //c++:void *                                    ----    c#:IntPtr       
  102. //c++:void * user_obj_param                     ----    c#:IntPtr user_obj_param 
  103. //c++:void * 对象名称                           ----    c#:([MarshalAs(UnmanagedType.AsAny)]Object 对象名称 
  104.  
  105.  
  106.        
  107. //c++:char, INT8, SBYTE, CHAR                               ----    c#:System.SByte 
  108. //c++:short, short int, INT16, SHORT                        ----    c#:System.Int16 
  109. //c++:int, long, long int, INT32, LONG32, BOOL , INT        ----    c#:System.Int32 
  110. //c++:__int64, INT64, LONGLONG                              ----    c#:System.Int64 
  111. //c++:unsigned char, UINT8, UCHAR , BYTE                    ----    c#:System.Byte 
  112. //c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t             ----    c#:System.UInt16 
  113. //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT      ----    c#:System.UInt32 
  114. //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG                            ----    c#:System.UInt64 
  115. //c++:float, FLOAT                                                              ----    c#:System.Single 
  116. //c++:double, long double, DOUBLE                                               ----    c#:System.Double 
  117.  
  118. //Win32 Types                                                                   ----  CLR Type 
  119.        
  120.  
  121. //Struct需要在C#里重新定义一个Struct 
  122. //CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str); 
  123.  
  124. //unsigned char** ppImage替换成IntPtr ppImage 
  125. //int& nWidth替换成ref int nWidth 
  126. //int*, int&, 则都可用 ref int 对应 
  127. //双针指类型参数,可以用 ref IntPtr 
  128. //函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double  fun_type1(double); 
  129. //char* 的操作c++: char*; 对应 c#:StringBuilder; 
  130. //c#中使用指针:在需要使用指针的地方 加 unsafe 
  131.  
  132.  
  133. //unsigned   char对应public   byte 
  134.  
  135.  
  136. WORD                                ushort 
  137. DWORD                 uint 
  138. UCHAR                 int/byte               大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte 
  139. UCHAR*                 string/IntPtr 
  140. unsigned char*      [MarshalAs(UnmanagedType.LPArray)]byte[]/?(Intptr) 
  141. char*                 string 
  142. LPCTSTR             string 
  143. LPTSTR                 [MarshalAs(UnmanagedType.LPTStr)] string 
  144. long                     int 
  145. ulong               uint 
  146. Handle                 IntPtr 
  147. HWND                     IntPtr 
  148. void*                 IntPtr 
  149. int                      int 
  150. int*                     ref int 
  151. *int                     IntPtr 
  152. unsigned int        uint 
  153. COLORREF            uint 
  154.  
  155. API与C#的数据类型对应关系表 
  156. API数据类型 类型描述               C#类型  API数据类型  类型描述              C#类型 
  157. WORD 16位无符号整数               ushort  CHAR                   字符                       char 
  158. LONG 32位无符号整数               int          DWORDLONG          64位长整数          long 
  159. DWORD 32位无符号整数              uint      HDC                   设备描述表句柄  int 
  160. HANDLE 句柄,32位整数              int          HGDIOBJ GDI     对象句柄              int 
  161. UINT 32位无符号整数               uint      HINSTANCE          实例句柄              int 
  162. BOOL 32位布尔型整数               bool      HWM                   窗口句柄              int 
  163. LPSTR 指向字符的32位指针      string  HPARAM               32位消息参数      int 
  164. LPCSTR 指向常字符的32位指针 String  LPARAM               32位消息参数      int 
  165. BYTE 字节                                    byte      WPARAM               32位消息参数      int 
  166.  
  167. BOOL          =        System.Int32 
  168. BOOLEAN       =        System.Int32 
  169. BYTE          =        System.UInt16 
  170. CHAR          =        System.Int16 
  171. COLORREF      =        System.UInt32 
  172. DWORD         =        System.UInt32 
  173. DWORD32       =        System.UInt32 
  174. DWORD64       =        System.UInt64 
  175. FLOAT         =        System.Float 
  176. HACCEL        =        System.IntPtr 
  177. HANDLE        =        System.IntPtr 
  178. HBITMAP       =        System.IntPtr 
  179. HBRUSH        =        System.IntPtr 
  180. HCONV         =        System.IntPtr 
  181. HCONVLIST     =        System.IntPtr 
  182. HCURSOR       =        System.IntPtr 
  183. HDC           =        System.IntPtr 
  184. HDDEDATA      =        System.IntPtr 
  185. HDESK         =        System.IntPtr 
  186. HDROP         =        System.IntPtr 
  187. HDWP          =        System.IntPtr 
  188. HENHMETAFILE  =        System.IntPtr 
  189. HFILE         =        System.IntPtr 
  190. HFONT         =        System.IntPtr 
  191. HGDIOBJ       =        System.IntPtr 
  192. HGLOBAL       =        System.IntPtr 
  193. HHOOK         =        System.IntPtr 
  194. HICON         =        System.IntPtr 
  195. HIMAGELIST    =        System.IntPtr 
  196. HIMC          =        System.IntPtr 
  197. HINSTANCE     =        System.IntPtr 
  198. HKEY          =        System.IntPtr 
  199. HLOCAL        =        System.IntPtr 
  200. HMENU         =        System.IntPtr 
  201. HMETAFILE     =        System.IntPtr 
  202. HMODULE       =        System.IntPtr 
  203. HMONITOR      =        System.IntPtr 
  204. HPALETTE      =        System.IntPtr 
  205. HPEN          =        System.IntPtr 
  206. HRGN          =        System.IntPtr 
  207. HRSRC         =        System.IntPtr 
  208. HSZ           =        System.IntPtr 
  209. HWINSTA       =        System.IntPtr 
  210. HWND          =        System.IntPtr 
  211. INT           =        System.Int32 
  212. INT32         =        System.Int32 
  213. INT64         =        System.Int64 
  214. LONG          =        System.Int32 
  215. LONG32        =        System.Int32 
  216. LONG64        =        System.Int64 
  217. LONGLONG      =        System.Int64 
  218. LPARAM        =        System.IntPtr 
  219. LPBOOL        =        System.Int16[] 
  220. LPBYTE        =        System.UInt16[] 
  221. LPCOLORREF    =        System.UInt32[] 
  222. LPCSTR        =        System.String 
  223. LPCTSTR       =        System.String 
  224. LPCVOID       =        System.UInt32 
  225. LPCWSTR       =        System.String 
  226. LPDWORD       =        System.UInt32[] 
  227. LPHANDLE      =        System.UInt32 
  228. LPINT         =        System.Int32[] 
  229. LPLONG        =        System.Int32[] 
  230. LPSTR         =        System.String 
  231. LPTSTR        =        System.String 
  232. LPVOID        =        System.UInt32 
  233. LPWORD        =        System.Int32[] 
  234. LPWSTR        =        System.String 
  235. LRESULT       =        System.IntPtr 
  236. PBOOL         =        System.Int16[] 
  237. PBOOLEAN      =        System.Int16[] 
  238. PBYTE         =        System.UInt16[] 
  239. PCHAR         =        System.Char[] 
  240. PCSTR         =        System.String 
  241. PCTSTR        =        System.String 
  242. PCWCH         =        System.UInt32 
  243. PCWSTR        =        System.UInt32 
  244. PDWORD        =        System.Int32[] 
  245. PFLOAT        =        System.Float[] 
  246. PHANDLE       =        System.UInt32 
  247. PHKEY         =        System.UInt32 
  248. PINT          =        System.Int32[] 
  249. PLCID         =        System.UInt32 
  250. PLONG         =        System.Int32[] 
  251. PLUID         =        System.UInt32 
  252. PSHORT        =        System.Int16[] 
  253. PSTR          =        System.String 
  254. PTBYTE        =        System.Char[] 
  255. PTCHAR        =        System.Char[] 
  256. PTSTR         =        System.String 
  257. PUCHAR        =        System.Char[] 
  258. PUINT         =        System.UInt32[] 
  259. PULONG        =        System.UInt32[] 
  260. PUSHORT       =        System.UInt16[] 
  261. PVOID         =        System.UInt32 
  262. PWCHAR        =        System.Char[] 
  263. PWORD         =        System.Int16[] 
  264. PWSTR         =        System.String 
  265. REGSAM        =        System.UInt32 
  266. SC_HANDLE     =        System.IntPtr 
  267. SC_LOCK       =        System.IntPtr 
  268. SHORT         =        System.Int16 
  269. SIZE_T        =        System.UInt32 
  270. SSIZE_        =        System.UInt32 
  271. TBYTE         =        System.Char 
  272. TCHAR         =        System.Char 
  273. UCHAR         =        System.Byte 
  274. UINT          =        System.UInt32 
  275. UINT32        =        System.UInt32 
  276. UINT64        =        System.UInt64 
  277. ULONG         =        System.UInt32 
  278. ULONG32       =        System.UInt32 
  279. ULONG64       =        System.UInt64 
  280. ULONGLONG     =        System.UInt64 
  281. USHORT        =        System.UInt16 
  282. WORD          =        System.UInt16 
  283. WPARAM        =        System.IntPtr 
  284.  
  285. Wtypes.h 中的非托管类型    非托管C 语言类型     托管类名           说明 
  286. HANDLE                     void*                System.IntPtr      32 位 
  287. BYTE                       unsigned char        System.Byte        8 位 
  288. SHORT                      short                System.Int16       16 位 
  289. WORD                       unsigned short       System.UInt16      16 位 
  290. INT                        int                  System.Int32       32 位 
  291. UINT                       unsigned int         System.UInt32      32 位 
  292. LONG                       long                 System.Int32       32 位 
  293. BOOL                       long                 System.Int32       32 位 
  294. DWORD                      unsigned long        System.UInt32      32 位 
  295. ULONG                      unsigned long        System.UInt32      32 位 
  296. CHAR                       char                 System.Char        用 ANSI 修饰。 
  297. LPSTR                      char*                System.String 或 System.StringBuilder  用 ANSI 修饰。 
  298. LPCSTR                     Const char*          System.String 或 System.StringBuilder  用 ANSI 修饰。 
  299. LPWSTR                     wchar_t*             System.String 或 System.StringBuilder  用 Unicode 修饰。 
  300. LPCWSTR                    Const wchar_t*       System.String 或 System.StringBuilder  用 Unicode 修饰。 
  301. FLOAT                      Float                System.Single     32 位 
  302. DOUBLE                     Double               System.Double     64 位 
  303.   
  304. C/C++中的结构类型数据在C#下的转换 
  305.   
  306. 在做项目移植的时候,经常会碰到数据类型的转换,而我这一次碰到的是C/C++中的结构怎样转换到C#。折腾了一个晚上终于有一个完美的方案。 
  307. 例如我们在C/C++下的结构数据如下: 
  308.  
  309. typedef struct 
  310.     char  sLibName[ 256 ]; 
  311.     char  sPathToLibrary[ 256 ]; 
  312.     INT32       iEntries; 
  313.     INT32       iUsed; 
  314.     UINT16     iSort; 
  315.     UINT16     iVersion; 
  316.     BOOLEAN     fContainsSubDirectories; 
  317.     INT32       iReserved; 
  318. } LIBHEADER; 
  319.  
  320. 我们想把它转成C#下的结构类型如下: 
  321.     public struct LIBHEADER 
  322.     { 
  323.         public char[] sLibName; 
  324.         public char[] sPathToLibrary; 
  325.         public Int32 iEntries; 
  326.         public Int32 iUsed; 
  327.         public UInt16 iSort; 
  328.         public UInt16 iVersion; 
  329.         public Boolean fContainsSubDirectories; 
  330.         public Int32 iReserved; 
  331.     } 
  332.     
  333.     
  334. 看上去好像没问题了,呵呵呵,其实这样是不行的,我们得再给C#编译器一些信息,告诉它一些字符数组的大小。然后它们在C#下面长得样子就变成这样: 
  335.     
  336.     
  337.     [StructLayout(LayoutKind.Sequential)] 
  338.     public struct LIBHEADER 
  339.     { 
  340.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] 
  341.         public char[] sLibName; 
  342.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] 
  343.         public char[] sPathToLibrary; 
  344.         public Int32 iEntries; 
  345.         public Int32 iUsed; 
  346.         public UInt16 iSort; 
  347.         public UInt16 iVersion; 
  348.         public Boolean fContainsSubDirectories; 
  349.         public Int32 iReserved; 
  350.     } 
  351.     
  352. 然后写一个函数负责转换。 
  353. public StructType ConverBytesToStructure<StructType>(byte[] bytesBuffer) 
  354.         { 
  355.             // 检查长度。 
  356.             if (bytesBuffer.Length != Marshal.SizeOf(typeof(StructType))) 
  357.             { 
  358.                 throw new ArgumentException("bytesBuffer参数和structObject参数字节长度不一致。"); 
  359.             } 
  360.  
  361.             IntPtr bufferHandler = Marshal.AllocHGlobal(bytesBuffer.Length); 
  362.             for (int index = 0; index < bytesBuffer.Length; index++) 
  363.             { 
  364.                 Marshal.WriteByte(bufferHandler, index, bytesBuffer[index]); 
  365.             } 
  366.             StructType structObject = (StructType)Marshal.PtrToStructure(bufferHandler, typeof(StructType)); 
  367.             Marshal.FreeHGlobal(bufferHandler); 
  368.             return structObject; 
  369.         } 
  370.         
  371.     然后我们的函数用例是这样: 
  372.             FileStream file = File.OpenRead(@"D:/Jagged Alliance 2 Gold/INSTALL.LOG"); 
  373.             byte[] buffer = new byte[Marshal.SizeOf(typeof(LIBHEADER))]; 
  374.             file.Read(buffer, 0, buffer.Length); 
  375.             LIBHEADER testValue = CommonTools.ConverBytesToStructure<LIBHEADER>(buffer); 
  376.              string libName = new string(testValue.sLibName); 
  377.              string pathToLibrary = new string(testValue.sPathToLibrary); 
  378.              
  379. OK,搞定。 
  380. 如果想去掉后面两句的char数组的转换哪代码如下 
  381.  
  382. C#中的结构代码 
  383.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
  384.     public struct LIBHEADER 
  385.     { 
  386.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
  387.         public string sLibName; 
  388.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
  389.         public string sPathToLibrary; 
  390.         public Int32 iEntries; 
  391.         public Int32 iUsed; 
  392.         public UInt16 iSort; 
  393.         public UInt16 iVersion; 
  394.         public Boolean fContainsSubDirectories; 
  395.         public Int32 iReserved; 
  396.     } 
  397.     
  398. 其它代码不用作修改便可使用 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章