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. 其它代碼不用作修改便可使用 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章