C#调用C++的DLL 所有数据类型转换方式-程序员宅基地

技术标签: 编程语言  



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.
其它代码不用作修改便可使用。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ZuoLongJing/article/details/23815679

智能推荐

sql内连接中,等值连接与自然连接的区别-程序员宅基地

文章浏览阅读892次。1. 等值连接中不要求相等属性值的属性名相同,而自然连接要求相等属性值的属性名必须相同,即两关系只有在同名属性才能进行自然连接。如上例R中的C列和S中的D列可进行等值连接,但因为属性名不同,不能进行自然连接。 2. 等值连接不将重复属性去掉,而自然连接去掉重复属性,也可以说,自然连接是去掉重复列的等值连接。如上例R中的B列和S中的B列进行等值连接时,结果有两个重复的属性列B,而进行自然连..._自然连接的属性名可以不同吗

understand 代码解析工具的使用_understand代码_提出问题 解决问题的博客-程序员宅基地

文章浏览阅读7.2k次,点赞14次,收藏66次。understand 常用操作文章目录understand 常用操作简单介绍软件下载常用基本操作新建工程并添加现有文件如何找到自己当前想要去编辑的文件?如何在当前文件中找到你要编辑的函数?如何跳转到定义?查看当前文件的函数列表如何查看函数都被谁调用了?查看函数的调用逻辑如何查找如何找到函数的被调用图除此之外可以分析出代码的各种结构文本的编辑格式设置双屏一边看代码,一遍看代码地图简单介绍understand对分析代码有非常强的能力,完全可以代替sourceinsight,并且可以在linux上mac上使_understand代码

jom.exe ERROR 2 问题解决办法_jom退出 qtcreator 错误代码2_skyyyyy_的博客-程序员宅基地

文章浏览阅读575次。别的解法不再赘述,注意检查资源文件.qrc里是不是有东西丢失了_jom退出 qtcreator 错误代码2

计算机与plc链接通信协议,实现上位计算机与PLC的上位链接系统的通信设计_空瓶记的博客-程序员宅基地

文章浏览阅读1.2k次。描述1、引 言PLC是专为工业控制而设计的专用计算机,其体积小,具有高可靠性和很强的抗干扰能力,因而在工业控制中得到了广泛的使用。随着工业的自动化程度的提高,对PLC的应用提出了更高的要求:更快的处理速度,更高的可靠性,控制与管理功能一体化。控制与管理一体化也就是将计算机信息处理技术,网络通信技术应用于PLC,使PLC用于下位分散控制,用计算机提供图形显示界面,同时对下位机进行监控。本文讨论的是上..._cpm2a可以用上位机控制外部i/0么

将给定的字符串划分为所有可能的IP地址 Restore IP Addresses-程序员宅基地

文章浏览阅读796次。为什么80%的码农都做不了架构师?>>> ...

Qos入门-程序员宅基地

文章浏览阅读81次。Qos分三种模型: 1.Best-effort service(默认网路都工作在此模式下) 2.Integrated Service(需要提前向网络申请程序所需的带宽,现有网络中很少用) 3.Differentiated Service(根据不同数据提供不同的服务,现有网络中通常采用的)Qos组件: 1.Classification and mar...

随便推点

pygame编写打地鼠游戏(6)_打地鼠pygame background image-程序员宅基地

文章浏览阅读767次。对上一次代码进一步修改,为适应背景修改了部分颜色。'''打地鼠游戏v1.5''''''1. 添加背景图为适应背景图,更改了原来设置的一些颜色的值2. 创建锤子类并实现锤子跟随鼠标功能'''# 导包导库导模块import pygame, sysfrom pygame import *from math import sin, cos, radiansfrom datetime ..._打地鼠pygame background image

C# 后端发送GET\POST请求(以企业微信发送消息为例)_后端请求getsection-程序员宅基地

文章浏览阅读2.1k次。用过企业微信的人应该都知道,如果我们要发送应用消息,首先需要用get方法获取到token,下面是后端发送get请求获取企业微信应用消息的方法:string url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;//corpid和corpsecret需要从企业微信后台管理里面拿到,具体方法可以看官方文档 HttpWebReq_后端请求getsection

C# sqlhelp-程序员宅基地

文章浏览阅读483次。// ===============================================================================// Microsoft Data Access Application Block for .NET// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp...

greenplum维护中的一些技巧_gp_autostats_mode-程序员宅基地

文章浏览阅读2.5k次。1.如果能用greenplum3.3.X,就不要使用greenplum4.X。原因: a. greenplum4.x看起当primary节点出现问题时,可以切换到mirror节点,继续提供服务,当mirror节点恢复后,可以做增量同步。增量同步是一个大的亮点。但实际上,greenplum4.x大量的的bug导致的的不稳定完全抵消了这个优点。当机器内存紧张时,mirror经常与主库不同步。而_gp_autostats_mode

我第一次技术直播,给了B站!-程序员宅基地

文章浏览阅读392次。昨晚,做了一次直播,讲了三个话题,关于程序员职场的,学生思维,职场发展方向,以及35岁危机怎么破除。三个话题,讲了一个半小时,从晚上八点~晚上九点四十,感谢捧场的朋友,以及直播间大家的支...