C#调用C++的DLL 所有数据类型转换方式

来源:互联网 发布:郑州淘宝加盟美女 编辑:程序博客网 时间:2024/05/21 10:03



01./C++中的DLL函数原型为 
02. //extern "C" __declspec(dllexport) bool 
方法名一(const char* 变量名1, unsigned char* 变量名2) 
03. //extern "C" __declspec(dllexport) bool 
方法名二(const unsigned char* 变量名1, char* 变量名2) 
04. 
05. //C#
调用C++DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 
06. //c++:HANDLE(void *) ---- c#:System.IntPtr 
07. //c++:Byte(unsigned char) ---- c#:System.Byte 
08. //c++:SHORT(short) ---- c#:System.Int16 
09. //c++:WORD(unsigned short) ---- c#:System.UInt16 
10. //c++:INT(int) ---- c#:System.Int16 
11. //c++:INT(int) ---- c#:System.Int32 
12. //c++:UINT(unsigned int) ---- c#:System.UInt16 
13. //c++:UINT(unsigned int) ---- c#:System.UInt32 
14. //c++:LONG(long) ---- c#:System.Int32 
15. //c++:ULONG(unsigned long) ---- c#:System.UInt32 
16. //c++:DWORD(unsigned long) ---- c#:System.UInt32 
17. //c++:DECIMAL ---- c#:System.Decimal 
18. //c++:BOOL(long) ---- c#:System.Boolean 
19. //c++:CHAR(char) ---- c#:System.Char 
20. //c++:LPSTR(char *) ---- c#:System.String 
21. //c++:LPWSTR(wchar_t *) ---- c#:System.String 
22. //c++:LPCSTR(const char *) ---- c#:System.String 
23. //c++:LPCWSTR(const wchar_t *) ---- c#:System.String 
24. //c++:PCAHR(char *) ---- c#:System.String 
25. //c++:BSTR ---- c#:System.String 
26. //c++:FLOAT(float) ---- c#:System.Single 
27. //c++:DOUBLE(double) ---- c#:System.Double 
28. //c++:VARIANT ---- c#:System.Object 
29. //c++:PBYTE(byte *) ---- c#:System.Byte[] 
30. 
31. //c++:BSTR ---- c#:StringBuilder 
32. //c++:LPCTSTR ---- c#:StringBuilder 
33. //c++:LPCTSTR ---- c#:string 
34. //c++:LPTSTR ---- c#:[MarshalAs(UnmanagedType.LPTStr)] string 
35. //c++:LPTSTR 
输出变量名 ---- c#:StringBuilder 输出变量名 
36. //c++:LPCWSTR ---- c#:IntPtr 
37. //c++:BOOL ---- c#:bool 
38. //c++:HMODULE ---- c#:IntPtr 
39. //c++:HINSTANCE ---- c#:IntPtr 
40. //c++:
结构体 ---- c#:public struct 结构体{}; 
41. //c++:
结构体 **变量名 ---- c#:out 变量名 //C#中提前申明一个结构体实例化后的变量名 
42. //c++:
结构体 &变量名 ---- c#:ref 结构体 变量名 
43. 
44. 
45. //c++:WORD ---- c#:ushort 
46. //c++:DWORD ---- c#:uint 
47. //c++:DWORD ---- c#:int 
48. 
49. //c++:UCHAR ---- c#:int 
50. //c++:UCHAR ---- c#:byte 
51. //c++:UCHAR* ---- c#:string 
52. //c++:UCHAR* ---- c#:IntPtr 
53. 
54. //c++:GUID ---- c#:Guid 
55. //c++:Handle ---- c#:IntPtr 
56. //c++:HWND ---- c#:IntPtr 
57. //c++:DWORD ---- c#:int 
58. //c++:COLORREF ---- c#:uint 
59. 
60. 
61. //c++:unsigned char ---- c#:byte 
62. //c++:unsigned char * ---- c#:ref byte 
63. //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] byte[] 
64. //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] Intptr 
65. 
66. //c++:unsigned char & ---- c#:ref byte 
67. //c++:unsigned char 
变量名 ---- c#:byte 变量名 
68. //c++:unsigned short 
变量名 ---- c#:ushort 变量名 
69. //c++:unsigned int 
变量名 ---- c#:uint 变量名 
70. //c++:unsigned long 
变量名 ---- c#:ulong 变量名 
71. 
72. //c++:char 
变量名 ---- c#:byte 变量名 //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示 
73. //c++:char 
数组名[数组大小] ---- c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名; ushort 
74. 
75. //c++:char * ---- c#:string //
传入参数 
76. //c++:char * ---- c#:StringBuilder//
传出参数 
77. //c++:char *
变量名 ---- c#:ref string 变量名 
78. //c++:char *
输入变量名 ---- c#:string 输入变量名 
79. //c++:char *
输出变量名 ---- c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名 
80. 
81. //c++:char ** ---- c#:string 
82. //c++:char **
变量名 ---- c#:ref string 变量名 
83. //c++:const char * ---- c#:string 
84. //c++:char[] ---- c#:string 
85. //c++:char 
变量名[数组大小] ---- c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名
86. 
87. //c++:struct 
结构体名 *变量名 ---- c#:ref 结构体名 变量名 
88. //c++:
委托 变量名 ---- c#:委托 变量名 
89. 
90. //c++:int ---- c#:int 
91. //c++:int ---- c#:ref int 
92. //c++:int & ---- c#:ref int 
93. //c++:int * ---- c#:ref int //C#
中调用前需定义int 变量名 = 0; 
94. 
95. //c++:*int ---- c#:IntPtr 
96. //c++:int32 PIPTR * ---- c#:int32[] 
97. //c++:float PIPTR * ---- c#:float[] 
98. 
99. 
100. //c++:double** 
数组名 ---- c#:ref double 数组名 
101. //c++:double*[] 
数组名 ---- c#:ref double 数组名 
102. //c++:long ---- c#:int 
103. //c++:ulong ---- c#:int 
104. 
105. //c++:UINT8 * ---- c#:ref byte //C#
中调用前需定义byte 变量名 = new byte(); 
106. 
107. 
108. //c++:handle ---- c#:IntPtr 
109. //c++:hwnd ---- c#:IntPtr 
110. 
111. 
112. //c++:void * ---- c#:IntPtr 
113. //c++:void * user_obj_param ---- c#:IntPtr user_obj_param 
114. //c++:void * 
对象名称 ---- c#:([MarshalAs(UnmanagedType.AsAny)]Object 对象名称 
115. 
116. 
117. 
118. //c++:char, INT8, SBYTE, CHAR ---- c#:System.SByte 
119. //c++:short, short int, INT16, SHORT ---- c#:System.Int16 
120. //c++:int, long, long int, INT32, LONG32, BOOL , INT ---- c#:System.Int32 
121. //c++:__int64, INT64, LONGLONG ---- c#:System.Int64 
122. //c++:unsigned char, UINT8, UCHAR , BYTE ---- c#:System.Byte 
123. //c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t ---- c#:System.UInt16 
124. //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT ---- c#:System.UInt32 
125. //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG ---- c#:System.UInt64 
126. //c++:float, FLOAT ---- c#:System.Single 
127. //c++:double, long double, DOUBLE ---- c#:System.Double 
128. 
129. //Win32 Types ---- CLR Type 
130. 
131. 
132. //Struct
需要在C#里重新定义一个Struct 
133. //CallBack
回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str); 
134. 
135. //unsigned char** ppImage
替换成IntPtr ppImage 
136. //int& nWidth
替换成ref int nWidth 
137. //int*, int&, 
则都可用 ref int 对应 
138. //
双针指类型参数,可以用 ref IntPtr 
139. //
函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double fun_type1(double); 
140. //char* 
的操作c++: char*; 对应 c#:StringBuilder; 
141. //c#
中使用指针:在需要使用指针的地方  unsafe 
142. 
143. 
144. //unsigned char
对应public byte 
145. /* 
146. * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg); 
147. * typedef void (*CALLBACKFUN1A)(char*, void* pArg); 
148. * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg); 
149. * 
调用方式为 
150. * [UnmanagedFunctionPointer(CallingConvention.Cdecl)] 
151. * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg); 
152. * 
153. * 
154. */ 
/C++
中的DLL函数原型为
//extern "C" __declspec(dllexport) bool 
方法名一(const char* 变量名1, unsigned char* 变量名2)
//extern "C" __declspec(dllexport) bool 
方法名二(const unsigned char* 变量名1, char* 变量名2)

//C#调用C++DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试
//c++:HANDLE(void *) ---- c#:System.IntPtr
//c++:Byte(unsigned char) ---- c#:System.Byte
//c++:SHORT(short) ---- c#:System.Int16
//c++:WORD(unsigned short) ---- c#:System.UInt16
//c++:INT(int) ---- c#:System.Int16
//c++:INT(int) ---- c#:System.Int32
//c++:UINT(unsigned int) ---- c#:System.UInt16
//c++:UINT(unsigned int) ---- c#:System.UInt32
//c++:LONG(long) ---- c#:System.Int32
//c++:ULONG(unsigned long) ---- c#:System.UInt32
//c++:DWORD(unsigned long) ---- c#:System.UInt32
//c++:DECIMAL ---- c#:System.Decimal
//c++:BOOL(long) ---- c#:System.Boolean
//c++:CHAR(char) ---- c#:System.Char
//c++:LPSTR(char *) ---- c#:System.String
//c++:LPWSTR(wchar_t *) ---- c#:System.String
//c++:LPCSTR(const char *) ---- c#:System.String
//c++:LPCWSTR(const wchar_t *) ---- c#:System.String
//c++:PCAHR(char *) ---- c#:System.String
//c++:BSTR ---- c#:System.String
//c++:FLOAT(float) ---- c#:System.Single
//c++:DOUBLE(double) ---- c#:System.Double
//c++:VARIANT ---- c#:System.Object
//c++:PBYTE(byte *) ---- c#:System.Byte[]

//c++:BSTR ---- c#:StringBuilder
//c++:LPCTSTR ---- c#:StringBuilder
//c++:LPCTSTR ---- c#:string
//c++:LPTSTR ---- c#:[MarshalAs(UnmanagedType.LPTStr)] string
//c++:LPTSTR 
输出变量名 ---- c#:StringBuilder 输出变量名
//c++:LPCWSTR ---- c#:IntPtr
//c++:BOOL ---- c#:bool 
//c++:HMODULE ---- c#:IntPtr 
//c++:HINSTANCE ---- c#:IntPtr
//c++:
结构体 ---- c#:public struct 结构体{};
//c++:
结构体 **变量名 ---- c#:out 变量名 //C#中提前申明一个结构体实例化后的变量名
//c++:
结构体 &变量名 ---- c#:ref 结构体 变量名

//c++:WORD ---- c#:ushort
//c++:DWORD ---- c#:uint
//c++:DWORD ---- c#:int

//c++:UCHAR ---- c#:int
//c++:UCHAR ---- c#:byte
//c++:UCHAR* ---- c#:string
//c++:UCHAR* ---- c#:IntPtr

//c++:GUID ---- c#:Guid
//c++:Handle ---- c#:IntPtr
//c++:HWND ---- c#:IntPtr
//c++:DWORD ---- c#:int
//c++:COLORREF ---- c#:uint

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

//c++:unsigned char & ---- c#:ref byte
//c++:unsigned char 
变量名 ---- c#:byte 变量名
//c++:unsigned short 
变量名 ---- c#:ushort 变量名
//c++:unsigned int 
变量名 ---- c#:uint 变量名
//c++:unsigned long 
变量名 ---- c#:ulong 变量名

//c++:char 变量名 ---- c#:byte 变量名 //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示
//c++:char 
数组名[数组大小] ---- c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名; ushort

//c++:char * ---- c#:string //传入参数
//c++:char * ---- c#:StringBuilder//
传出参数
//c++:char *
变量名 ---- c#:ref string 变量名
//c++:char *
输入变量名 ---- c#:string 输入变量名
//c++:char *
输出变量名 ---- c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名

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

//c++:struct 结构体名 *变量名 ---- c#:ref 结构体名 变量名
//c++:
委托 变量名 ---- c#:委托 变量名

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

//c++:*int ---- c#:IntPtr
//c++:int32 PIPTR * ---- c#:int32[]
//c++:float PIPTR * ---- c#:float[]

//c++:double** 数组名 ---- c#:ref double 数组名
//c++:double*[] 
数组名 ---- c#:ref double 数组名
//c++:long ---- c#:int
//c++:ulong ---- c#:int

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

//c++:handle ---- c#:IntPtr
//c++:hwnd ---- c#:IntPtr


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


//c++:char, INT8, SBYTE, CHAR ---- c#:System.SByte 
//c++:short, short int, INT16, SHORT ---- c#:System.Int16 
//c++:int, long, long int, INT32, LONG32, BOOL , INT ---- c#:System.Int32 
//c++:__int64, INT64, LONGLONG ---- c#:System.Int64 
//c++:unsigned char, UINT8, UCHAR , BYTE ---- c#:System.Byte 
//c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t ---- c#:System.UInt16 
//c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT ---- c#:System.UInt32 
//c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG ---- c#:System.UInt64 
//c++:float, FLOAT ---- c#:System.Single 
//c++:double, long double, DOUBLE ---- c#:System.Double

//Win32 Types ---- CLR Type

//Struct需要在C#里重新定义一个Struct
//CallBack
回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str);

//unsigned char** ppImage替换成IntPtr ppImage
//int& nWidth
替换成ref int nWidth
//int*, int&, 
则都可用 ref int 对应
//
双针指类型参数,可以用 ref IntPtr
//
函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double fun_type1(double);
//char* 
的操作c++: char*; 对应 c#:StringBuilder;
//c#
中使用指针:在需要使用指针的地方  unsafe

//unsigned char对应public byte
/*
* typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg);
* typedef void (*CALLBACKFUN1A)(char*, void* pArg);
* bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg);
调用方式为
* [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
* public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg);
*
*
*/[csharp] view plaincopyprint?
01.C++ C# 
02.===================================== 
03.WORD ushort 
04.DWORD uint 
05.UCHAR int/byte 
大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte 
06.UCHAR* string/IntPtr 
07.unsigned char* [MarshalAs(UnmanagedType.LPArray)]byte[]/
?(Intptr 
08.char* string 
09.LPCTSTR string 
10.LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string 
11.long int 
12.ulong uint 
13.Handle IntPtr 
14.HWND IntPtr 
15.void* IntPtr 
16.int int 
17.int* ref int 
18.*int IntPtr 
19.unsigned int uint 
20.COLORREF uint 
21. 
22.API
C#的数据类型对应关系表 
23.API
数据类型 类型描述 C#类型 API数据类型 类型描述 C#类型 
24.WORD 16
位无符号整数 ushort CHAR 字符 char 
25.LONG 32
位无符号整数 int DWORDLONG 64位长整数 long 
26.DWORD 32
位无符号整数 uint HDC 设备描述表句柄 int 
27.HANDLE 
句柄,32位整数 int HGDIOBJ GDI 对象句柄 int 
28.UINT 32
位无符号整数 uint HINSTANCE 实例句柄 int 
29.BOOL 32
位布尔型整数 bool HWM 窗口句柄 int 
30.LPSTR 
指向字符的32位指针 string HPARAM 32位消息参数 int 
31.LPCSTR 
指向常字符的32位指针 String LPARAM 32位消息参数 int 
32.BYTE 
字节 byte WPARAM 32位消息参数 int 
33. 
34.BOOL=System.Int32 
35.BOOLEAN=System.Int32 
36.BYTE=System.UInt16 
37.CHAR=System.Int16 
38.COLORREF=System.UInt32 
39.DWORD=System.UInt32 
40.DWORD32=System.UInt32 
41.DWORD64=System.UInt64 
42.FLOAT=System.Float 
43.HACCEL=System.IntPtr 
44.HANDLE=System.IntPtr 
45.HBITMAP=System.IntPtr 
46.HBRUSH=System.IntPtr 
47.HCONV=System.IntPtr 
48.HCONVLIST=System.IntPtr 
49.HCURSOR=System.IntPtr 
50.HDC=System.IntPtr 
51.HDDEDATA=System.IntPtr 
52.HDESK=System.IntPtr 
53.HDROP=System.IntPtr 
54.HDWP=System.IntPtr 
55.HENHMETAFILE=System.IntPtr 
56.HFILE=System.IntPtr 
57.HFONT=System.IntPtr 
58.HGDIOBJ=System.IntPtr 
59.HGLOBAL=System.IntPtr 
60.HHOOK=System.IntPtr 
61.HICON=System.IntPtr 
62.HIMAGELIST=System.IntPtr 
63.HIMC=System.IntPtr 
64.HINSTANCE=System.IntPtr 
65.HKEY=System.IntPtr 
66.HLOCAL=System.IntPtr 
67.HMENU=System.IntPtr 
68.HMETAFILE=System.IntPtr 
69.HMODULE=System.IntPtr 
70.HMONITOR=System.IntPtr 
71.HPALETTE=System.IntPtr 
72.HPEN=System.IntPtr 
73.HRGN=System.IntPtr 
74.HRSRC=System.IntPtr 
75.HSZ=System.IntPtr 
76.HWINSTA=System.IntPtr 
77.HWND=System.IntPtr 
78.INT=System.Int32 
79.INT32=System.Int32 
80.INT64=System.Int64 
81.LONG=System.Int32 
82.LONG32=System.Int32 
83.LONG64=System.Int64 
84.LONGLONG=System.Int64 
85.LPARAM=System.IntPtr 
86.LPBOOL=System.Int16[] 
87.LPBYTE=System.UInt16[] 
88.LPCOLORREF=System.UInt32[] 
89.LPCSTR=System.String 
90.LPCTSTR=System.String 
91.LPCVOID=System.UInt32 
92.LPCWSTR=System.String 
93.LPDWORD=System.UInt32[] 
94.LPHANDLE=System.UInt32 
95.LPINT=System.Int32[] 
96.LPLONG=System.Int32[] 
97.LPSTR=System.String 
98.LPTSTR=System.String 
99.LPVOID=System.UInt32 
100.LPWORD=System.Int32[] 
101.LPWSTR=System.String 
102.LRESULT=System.IntPtr 
103.PBOOL=System.Int16[] 
104.PBOOLEAN=System.Int16[] 
105.PBYTE=System.UInt16[] 
106.PCHAR=System.Char[] 
107.PCSTR=System.String 
108.PCTSTR=System.String 
109.PCWCH=System.UInt32 
110.PCWSTR=System.UInt32 
111.PDWORD=System.Int32[] 
112.PFLOAT=System.Float[] 
113.PHANDLE=System.UInt32 
114.PHKEY=System.UInt32 
115.PINT=System.Int32[] 
116.PLCID=System.UInt32 
117.PLONG=System.Int32[] 
118.PLUID=System.UInt32 
119.PSHORT=System.Int16[] 
120.PSTR=System.String 
121.PTBYTE=System.Char[] 
122.PTCHAR=System.Char[] 
123.PTSTR=System.String 
124.PUCHAR=System.Char[] 
125.PUINT=System.UInt32[] 
126.PULONG=System.UInt32[] 
127.PUSHORT=System.UInt16[] 
128.PVOID=System.UInt32 
129.PWCHAR=System.Char[] 
130.PWORD=System.Int16[] 
131.PWSTR=System.String 
132.REGSAM=System.UInt32 
133.SC_HANDLE=System.IntPtr 
134.SC_LOCK=System.IntPtr 
135.SHORT=System.Int16 
136.SIZE_T=System.UInt32 
137.SSIZE_=System.UInt32 
138.TBYTE=System.Char 
139.TCHAR=System.Char 
140.UCHAR=System.Byte 
141.UINT=System.UInt32 
142.UINT32=System.UInt32 
143.UINT64=System.UInt64 
144.ULONG=System.UInt32 
145.ULONG32=System.UInt32 
146.ULONG64=System.UInt64 
147.ULONGLONG=System.UInt64 
148.USHORT=System.UInt16 
149.WORD=System.UInt16 
150.WPARAM=System.IntPtr 
151. 
152.Wtypes.h 
中的非托管类型 非托管语言类型 托管类名 说明 
153.HANDLE void* System.IntPtr 32 
 
154.BYTE unsigned char System.Byte 8 
 
155.SHORT short System.Int16 16 
 
156.WORD unsigned short System.UInt16 16 
 
157.INT int System.Int32 32 
 
158.UINT unsigned int System.UInt32 32 
 
159.LONG long System.Int32 32 
 
160.BOOL long System.Int32 32 
 
161.DWORD unsigned long System.UInt32 32 
 
162.ULONG unsigned long System.UInt32 32 
 
163.CHAR char System.Char 
 ANSI 修饰。 
164.LPSTR char* System.String 
 System.StringBuilder  ANSI 修饰。 
165.LPCSTR Const char* System.String 
 System.StringBuilder  ANSI 修饰。 
166.LPWSTR wchar_t* System.String 
 System.StringBuilder  Unicode 修饰。 
167.LPCWSTR Const wchar_t* System.String 
 System.StringBuilder  Unicode 修饰。 
168.FLOAT Float System.Single 32 
 
169.DOUBLE Double System.Double 64 
 
170. 
171.C/C++
中的结构类型数据在C#下的转换 
172. 
173.
在做项目移植的时候,经常会碰到数据类型的转换,而我这一次碰到的是C/C++中的结构怎样转换到C#。折腾了一个晚上终于有一个完美的方案。 
174.
例如我们在C/C++下的结构数据如下: 
175.typedef struct 
176.{ 
177. char sLibName[ 256 ]; 
178. char sPathToLibrary[ 256 ]; 
179. INT32 iEntries; 
180. INT32 iUsed; 
181. UINT16 iSort; 
182. UINT16 iVersion; 
183. BOOLEAN fContainsSubDirectories; 
184. INT32 iReserved; 
185.} LIBHEADER; 
186.
我们想把它转成C#下的结构类型如下: 
187. public struct LIBHEADER 
188. { 
189. public char[] sLibName; 
190. public char[] sPathToLibrary; 
191. public Int32 iEntries; 
192. public Int32 iUsed; 
193. public UInt16 iSort; 
194. public UInt16 iVersion; 
195. public Boolean fContainsSubDirectories; 
196. public Int32 iReserved; 
197. } 
198.
看上去好像没问题了,呵呵呵,其实这样是不行的,我们得再给C#编译器一些信息,告诉它一些字符数组的大小。然后它们在C#下面长得样子就变成这样: 
199. [StructLayout(LayoutKind.Sequential)] 
200. public struct LIBHEADER 
201. { 
202. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] 
203. public char[] sLibName; 
204. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] 
205. public char[] sPathToLibrary; 
206. public Int32 iEntries; 
207. public Int32 iUsed; 
208. public UInt16 iSort; 
209. public UInt16 iVersion; 
210. public Boolean fContainsSubDirectories; 
211. public Int32 iReserved; 
212. } 
213.
然后写一个函数负责转换。 
214.public StructType ConverBytesToStructure<StructType>(byte[] bytesBuffer) 
215. { 
216. // 
检查长度。 
217. if (bytesBuffer.Length != Marshal.SizeOf(typeof(StructType))) 
218. { 
219. throw new ArgumentException("bytesBuffer
参数和structObject参数字节长度不一致。"); 
220. } 
221. 
222. IntPtr bufferHandler = Marshal.AllocHGlobal(bytesBuffer.Length); 
223. for (int index = 0; index < bytesBuffer.Length; index++) 
224. { 
225. Marshal.WriteByte(bufferHandler, index, bytesBuffer[index]); 
226. } 
227. StructType structObject = (StructType)Marshal.PtrToStructure(bufferHandler, typeof(StructType)); 
228. Marshal.FreeHGlobal(bufferHandler); 
229. return structObject; 
230. } 
231.
然后我们的函数用例是这样: 
232. FileStream file = File.OpenRead(@"D:/Jagged Alliance 2 Gold/INSTALL.LOG"); 
233. byte[] buffer = new byte[Marshal.SizeOf(typeof(LIBHEADER))]; 
234. file.Read(buffer, 0, buffer.Length); 
235.LIBHEADER testValue = CommonTools.ConverBytesToStructure<LIBHEADER>(buffer); 
236.string libName = new string(testValue.sLibName); 
237.string pathToLibrary= new string(testValue.sPathToLibrary); 
238.OK,
搞定。 
239.
如果想去掉后面两句的char数组的转换哪代码如下 
240.C#
中的结构代码 
241. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 
242. public struct LIBHEADER 
243. { 
244. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
245. public string sLibName; 
246. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
247. public string sPathToLibrary; 
248. public Int32 iEntries; 
249. public Int32 iUsed; 
250. public UInt16 iSort; 
251. public UInt16 iVersion; 
252. public Boolean fContainsSubDirectories; 
253. public Int32 iReserved; 
254. } 
255.
其它代码不用作修改便可使用。

0 0
原创粉丝点击