C#与C++之间类型的对应

来源:互联网 发布:spc数据分析 编辑:程序博客网 时间:2024/06/04 19:58
[c-sharp] view plaincopy
  1.  C#与C++之间类型的对应   
  2. Windows Data Type    .NET Data Type  
  3. BOOL, BOOLEAN    Boolean or Int32  
  4. BSTR    String  
  5. BYTE    Byte  
  6. CHAR    Char  
  7. DOUBLE    Double  
  8. DWORD    Int32 or UInt32  
  9. FLOAT    Single  
  10. HANDLE (and all other handle types, such as HFONT and HMENU)    IntPtr, UintPtr or HandleRef   
  11. HRESULT    Int32 or UInt32  
  12. INT    Int32  
  13. LANGID    Int16 or UInt16  
  14. LCID    Int32 or UInt32  
  15. LONG    Int32  
  16. LPARAM    IntPtr, UintPtr or Object  
  17. LPCSTR    String  
  18. LPCTSTR    String  
  19. LPCWSTR    String  
  20. LPSTR    String or StringBuilder*  
  21. LPTSTR    String or StringBuilder  
  22. LPWSTR    String or StringBuilder  
  23. LPVOID    IntPtr, UintPtr or Object  
  24. LRESULT    IntPtr  
  25. SAFEARRAY    .NET array type  
  26. SHORT    Int16  
  27. TCHAR    Char  
  28. UCHAR    SByte  
  29. UINT    Int32 or UInt32  
  30. ULONG    Int32 or UInt32  
  31. VARIANT    Object  
  32. VARIANT_BOOL    Boolean  
  33. WCHAR    Char  
  34. WORD    Int16 or UInt16  
  35. WPARAM    IntPtr, UintPtr or Object  
  36. 另: 在进行string转换时,需要加入前缀[MarshalAs(UnmanagedType.LPStr)]lpdword 对应于 ref int  
  37.   
  38.   
  39. C/C++    C#  
  40. HANDLE, LPDWORD, LPVOID, void*    IntPtr  
  41. LPCTSTR, LPCTSTR, LPSTR, char*, const char*, Wchar_t*, LPWSTR    String [in], StringBuilder [inout]  
  42. DWORD, unsigned long, Ulong    UInt32, [MarshalAs(UnmanagedType.U4)]  
  43. bool    bool  
  44. LP<struct>    [In] ref <struct>  
  45. SIZE_T    uint  
  46. LPDWORD    out uint  
  47. LPTSTR    [Out] StringBuilder  
  48. PULARGE_INTEGER    out ulong  
  49. WORD    uInt16  
  50. Byte, unsigned char    byte  
  51. Short    Int16  
  52. Long, int    Int32  
  53. float    single  
  54. double    double  
  55. NULL pointer    IntPtr.Zero  
  56. Uint    Uint32  
  57.   
  58. C#调用DLL文件时参数对应表  
  59. Wtypes.h中的非托管类型  非托管 C语言类型        托管类名                说明   
  60. HANDLE           void*                     System.IntPtr                 32 位   
  61. BYTE             unsigned char             System.Byte                   8 位   
  62. SHORT            short                     System.Int16                  16 位   
  63. WORD              unsigned short           System.UInt16                 16 位   
  64. INT               int                      System.Int32                  32 位   
  65. UINT               unsigned int            System.UInt32                  32 位   
  66. LONG              long                     System.Int32                  32 位   
  67. BOOL              long                     System.Int32                  32 位   
  68. DWORD             unsigned long            System.UInt32                 32 位   
  69. ULONG              unsigned long           System.UInt32                 32 位   
  70. CHAR              char                     System.Char                   用 ANSI 修饰。  
  71. LPSTR             char*              System.String 或 System.StringBuilder   用 ANSI 修饰。   
  72. LPCSTR            Const char*        System.String 或System.StringBuilder   用 ANSI 修饰。   
  73. LPWSTR            wchar_t*           System.String 或System.StringBuilder   用 Unicode 修饰。   
  74. LPCWSTR           Const wchar_t*     System.String 或System.StringBuilder   用 Unicode 修饰。   
  75. FLOAT              Float             System.Single                       32 位   
  76. DOUBLE             Double            System.Double                       64 位  
  77.   
  78. 习惯用C#写东西,但平时又会碰到很多要用win32 API的地方,所以经常要用DllImport,但win32函数的类型写法是很庞杂的,相信为之困扰的不止我一个,现在我整理一份我个人的理解如下,希望高人不吝赐教。  
  79. 我的基本原则有如下几点:  
  80. 1、下面都是针对32位系统的,所以int是32位.long也是32位;  
  81. 2、各种句柄类的(H开头),我认为一律是System.IntPtr,到目前为止没发现出错;如果哪位在使用中出错,请指出;  
  82. 3、LP和P,我实在不懂(对C 不太了解),对于LP和P开头的函数,如果是和STR有关的,一律写为System.String,像PLCID这样指向什么东西的,写为System.UInt32(因为指向另一个地址,那就是指针,指针是32位吧),int之类的数值型,那我就写为int[],以方便.net程序引用(写成System.UInt32的话,给API调用应该也不会出错,但.net程序就不好引用了)  
  83. 如有意见,欢迎指教。谢谢  
  84. BOOL=System.Int32  
  85. BOOLEAN=System.Int32  
  86. BYTE=System.UInt16  
  87. CHAR=System.Int16  
  88. COLORREF=System.UInt32  
  89. DWORD=System.UInt32  
  90. DWORD32=System.UInt32  
  91. DWORD64=System.UInt64  
  92. FLOAT=System.Float  
  93. HACCEL=System.IntPtr  
  94. HANDLE=System.IntPtr  
  95. HBITMAP=System.IntPtr  
  96. HBRUSH=System.IntPtr  
  97. HCONV=System.IntPtr  
  98. HCONVLIST=System.IntPtr  
  99. HCURSOR=System.IntPtr  
  100. HDC=System.IntPtr  
  101. HDDEDATA=System.IntPtr  
  102. HDESK=System.IntPtr  
  103. HDROP=System.IntPtr  
  104. HDWP=System.IntPtr  
  105. HENHMETAFILE=System.IntPtr  
  106. HFILE=System.IntPtr  
  107. HFONT=System.IntPtr  
  108. HGDIOBJ=System.IntPtr  
  109. HGLOBAL=System.IntPtr  
  110. HHOOK=System.IntPtr  
  111. HICON=System.IntPtr  
  112. HIMAGELIST=System.IntPtr  
  113. HIMC=System.IntPtr  
  114. HINSTANCE=System.IntPtr  
  115. HKEY=System.IntPtr  
  116. HLOCAL=System.IntPtr  
  117. HMENU=System.IntPtr  
  118. HMETAFILE=System.IntPtr  
  119. HMODULE=System.IntPtr  
  120. HMONITOR=System.IntPtr  
  121. HPALETTE=System.IntPtr  
  122. HPEN=System.IntPtr  
  123. HRGN=System.IntPtr  
  124. HRSRC=System.IntPtr  
  125. HSZ=System.IntPtr  
  126. HWINSTA=System.IntPtr  
  127. HWND=System.IntPtr  
  128. INT=System.Int32  
  129. INT32=System.Int32  
  130. INT64=System.Int64  
  131. LONG=System.Int32  
  132. LONG32=System.Int32  
  133. LONG64=System.Int64  
  134. LONGLONG=System.Int64  
  135. LPARAM=System.IntPtr  
  136. LPBOOL=System.Int16[]  
  137. LPBYTE=System.UInt16[]  
  138. LPCOLORREF=System.UInt32[]  
  139. LPCSTR=System.String  
  140. LPCTSTR=System.String  
  141. LPCVOID=System.UInt32  
  142. LPCWSTR=System.String  
  143. LPDWORD=System.UInt32[]  
  144. LPHANDLE=System.UInt32  
  145. LPINT=System.Int32[]  
  146. LPLONG=System.Int32[]  
  147. LPSTR=System.String  
  148. LPTSTR=System.String  
  149. LPVOID=System.UInt32  
  150. LPWORD=System.Int32[]  
  151. LPWSTR=System.String  
  152. LRESULT=System.IntPtr  
  153. PBOOL=System.Int16[]  
  154. PBOOLEAN=System.Int16[]  
  155. PBYTE=System.UInt16[]  
  156. PCHAR=System.Char[]  
  157. PCSTR=System.String  
  158. PCTSTR=System.String  
  159. PCWCH=System.UInt32  
  160. PCWSTR=System.UInt32  
  161. PDWORD=System.Int32[]  
  162. PFLOAT=System.Float[]  
  163. PHANDLE=System.UInt32  
  164. PHKEY=System.UInt32  
  165. PINT=System.Int32[]  
  166. PLCID=System.UInt32  
  167. PLONG=System.Int32[]  
  168. PLUID=System.UInt32  
  169. PSHORT=System.Int16[]  
  170. PSTR=System.String  
  171. PTBYTE=System.Char[]  
  172. PTCHAR=System.Char[]  
  173. PTSTR=System.String  
  174. PUCHAR=System.Char[]  
  175. PUINT=System.UInt32[]  
  176. PULONG=System.UInt32[]  
  177. PUSHORT=System.UInt16[]  
  178. PVOID=System.UInt32  
  179. PWCHAR=System.Char[]  
  180. PWORD=System.Int16[]  
  181. PWSTR=System.String  
  182. REGSAM=System.UInt32  
  183. SC_HANDLE=System.IntPtr  
  184. SC_LOCK=System.IntPtr  
  185. SHORT=System.Int16  
  186. SIZE_T=System.UInt32  
  187. SSIZE_=System.UInt32  
  188. TBYTE=System.Char  
  189. TCHAR=System.Char  
  190. UCHAR=System.  
  191. Wtypes.h 中的非托管类型     非托管 C 语言类型     托管类名     说明   
  192. HANDLE     void*     System.IntPtr     在 32 位 Windows 操作系统上为 32 位,在 64 位 Windows 操作系统上为 64 位。  
  193. BYTE     unsigned char     System.Byte     8 位  
  194. SHORT     short     System.Int16     16 位  
  195. WORD     unsigned short     System.UInt16     16 位  
  196. INT     int     System.Int32     32 位  
  197. UINT     unsigned int     System.UInt32     32 位  
  198. LONG     long     System.Int32     32 位  
  199. BOOL     long     System.Int32     32 位  
  200. DWORD     unsigned long     System.UInt32     32 位  
  201. ULONG     unsigned long     System.UInt32     32 位  
  202. CHAR     char     System.Char     用 ANSI 修饰。  
  203. LPSTR     char*     System.String 或 System.Text.StringBuilder    用 ANSI 修饰。  
  204. LPCSTR     Const char*     System.String 或 System.Text.StringBuilder    用 ANSI 修饰。  
  205. LPWSTR     wchar_t*     System.String 或 System.Text.StringBuilder    用 Unicode 修饰。  
  206. LPCWSTR     Const wchar_t*     System.String 或 System.Text.StringBuilder    用 Unicode 修饰。  
  207. FLOAT     Float     System.Single     32 位  
  208. DOUBLE     Double     System.Double     64 位  
0 0
原创粉丝点击