c#调c++dll参数转换.docx
- 文档编号:9351613
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:23
- 大小:21.90KB
c#调c++dll参数转换.docx
《c#调c++dll参数转换.docx》由会员分享,可在线阅读,更多相关《c#调c++dll参数转换.docx(23页珍藏版)》请在冰豆网上搜索。
c#调c++dll参数转换
C#调用C++DLL所有数据类型转换方式
1./C++中的DLL函数原型为
2.//extern"C"__declspec(dllexport)bool方法名一(constchar*变量名1,unsignedchar*变量名2)
3.//extern"C"__declspec(dllexport)bool方法名二(constunsignedchar*变量名1,char*变量名2)
4.
5.//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试
6.//c++:
HANDLE(void*)----c#:
System.IntPtr
7.//c++:
Byte(unsignedchar)----c#:
System.Byte
8.//c++:
SHORT(short)----c#:
System.Int16
9.//c++:
WORD(unsignedshort)----c#:
System.UInt16
10.//c++:
INT(int)----c#:
System.Int16
11.//c++:
INT(int)----c#:
System.Int32
12.//c++:
UINT(unsignedint)----c#:
System.UInt16
13.//c++:
UINT(unsignedint)----c#:
System.UInt32
14.//c++:
LONG(long)----c#:
System.Int32
15.//c++:
ULONG(unsignedlong)----c#:
System.UInt32
16.//c++:
DWORD(unsignedlong)----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(constchar*)----c#:
System.String
23.//c++:
LPCWSTR(constwchar_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#:
publicstruct结构体{};
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++:
unsignedchar----c#:
byte
62.//c++:
unsignedchar*----c#:
refbyte
63.//c++:
unsignedchar*----c#:
[MarshalAs(UnmanagedType.LPArray)]byte[]
64.//c++:
unsignedchar*----c#:
[MarshalAs(UnmanagedType.LPArray)]Intptr
65.
66.//c++:
unsignedchar&----c#:
refbyte
67.//c++:
unsignedchar变量名----c#:
byte变量名
68.//c++:
unsignedshort变量名----c#:
ushort变量名
69.//c++:
unsignedint变量名----c#:
uint变量名
70.//c++:
unsignedlong变量名----c#:
ulong变量名
71.
72.//c++:
char变量名----c#:
byte变量名//C++中一个字符用一个字节表示,C#中一个字符用两个字节表示
73.//c++:
char数组名[数组大小]----c#:
MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)]publicstring数组名;ushort
74.
75.//c++:
char*----c#:
string//传入参数
76.//c++:
char*----c#:
StringBuilder//传出参数
77.//c++:
char*变量名----c#:
refstring变量名
78.//c++:
char*输入变量名----c#:
string输入变量名
79.//c++:
char*输出变量名----c#:
[MarshalAs(UnmanagedType.LPStr)]StringBuilder输出变量名
80.
81.//c++:
char**----c#:
string
82.//c++:
char**变量名----c#:
refstring变量名
83.//c++:
constchar*----c#:
string
84.//c++:
char[]----c#:
string
85.//c++:
char变量名[数组大小]----c#:
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)]publicstring变量名;
86.
87.//c++:
struct结构体名*变量名----c#:
ref结构体名变量名
88.//c++:
委托变量名----c#:
委托变量名
89.
90.//c++:
int----c#:
int
91.//c++:
int----c#:
refint
92.//c++:
int&----c#:
refint
93.//c++:
int*----c#:
refint//C#中调用前需定义int变量名=0;
94.
95.//c++:
*int----c#:
IntPtr
96.//c++:
int32PIPTR*----c#:
int32[]
97.//c++:
floatPIPTR*----c#:
float[]
98.
99.
100.//c++:
double**数组名----c#:
refdouble数组名
101.//c++:
double*[]数组名----c#:
refdouble数组名
102.//c++:
long----c#:
int
103.//c++:
ulong----c#:
int
104.
105.//c++:
UINT8*----c#:
refbyte//C#中调用前需定义byte变量名=newbyte();
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#:
IntPtruser_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,shortint,INT16,SHORT----c#:
System.Int16
120.//c++:
int,long,longint,INT32,LONG32,BOOL,INT----c#:
System.Int32
121.//c++:
__int64,INT64,LONGLONG----c#:
System.Int64
122.//c++:
unsignedchar,UINT8,UCHAR,BYTE----c#:
System.Byte
123.//c++:
unsignedshort,UINT16,USHORT,WORD,ATOM,WCHAR,__wchar_t----c#:
System.UInt16
124.//c++:
unsigned,unsignedint,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,longdouble,DOUBLE----c#:
System.Double
128.
129.//Win32Types----CLRType
130.
131.
132.//Struct需要在C#里重新定义一个Struct
133.//CallBack回调函数需要封装在一个委托里,delegatestaticexternintFunCallBack(stringstr);
134.
135.//unsignedchar**ppImage替换成IntPtrppImage
136.//int&nWidth替换成refintnWidth
137.//int*,int&,则都可用refint对应
138.//双针指类型参数,可以用refIntPtr
139.//函数指针使用c++:
typedefdouble(*fun_type1)(double);对应c#:
publicdelegatedoublefun_type1(double);
140.//char*的操作c++:
char*;对应c#:
StringBuilder;
141.//c#中使用指针:
在需要使用指针的地方加unsafe
142.
143.
144.//unsignedchar对应publicbyte
145./*
146.*typedefvoid(*CALLBACKFUN1W)(wchar_t*,void*pArg);
147.*typedefvoid(*CALLBACKFUN1A)(char*,void*pArg);
148.*boolBIOPRINT_SENSOR_APIdllFun1(CALLBACKFUN1pCallbackFun1,void*pArg);
149.*调用方式为
150.*[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
151.*publicdelegatevoidCallbackFunc1([MarshalAs(UnmanagedType.LPWStr)]StringBuilderstrName,IntPtrpArg);
152.*
153.*
154.*/
/C++中的DLL函数原型为
//extern"C"__declspec(dllexport)bool方法名一(constchar*变量名1,unsignedchar*变量名2)
//extern"C"__declspec(dllexport)bool方法名二(constunsignedchar*变量名1,char*变量名2)
//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试
//c++:
HANDLE(void*)----c#:
System.IntPtr
//c++:
Byte(unsignedchar)----c#:
System.Byte
//c++:
SHORT(short)----c#:
System.Int16
//c++:
WORD(unsignedshort)----c#:
System.UInt16
//c++:
INT(int)----c#:
System.Int16
//c++:
INT(int)----c#:
System.Int32
//c++:
UINT(unsignedint)----c#:
System.UInt16
//c++:
UINT(unsignedint)----c#:
System.UInt32
//c++:
LONG(long)----c#:
System.Int32
//c++:
ULONG(unsignedlong)----c#:
System.UInt32
//c++:
DWORD(unsignedlong)----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(constchar*)----c#:
System.String
//c++:
LPCWSTR(constwchar_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#:
publicstruct结构体{};
//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++:
unsignedchar----c#:
byte
//c++:
unsignedchar*----c#:
refbyte
//c++:
unsignedchar*----c#:
[MarshalAs(UnmanagedType.LPArray)]byte[]
//c++:
unsignedchar*----c#:
[MarshalAs(UnmanagedType.LPArray)]Intptr
//c++:
unsignedchar&----c#:
refbyte
//c++:
unsignedchar变量名----c#:
byte变量名
//c++:
unsignedshort变量名----c#:
ushort变量名
//c++:
unsignedint变量名----c#:
uint变量名
//c++:
unsignedlong变量名----c#:
ulong变量名
//c++:
char变量名----c#:
byte变量名//C++中一个字符用一个字节表示,C#中一个字符用两个字节表示
//c++:
char数组名[数组大小]----c#:
MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)]publicstring数组名;ushort
//c++:
char*----c#:
string//传入参数
//c++:
char*----c#:
StringBuilder//传出参数
//c++:
char*变量名----c#:
refstring变量名
//c++:
char*输入变量名----c#:
string输入变量名
//c++:
char*输出变量名----c#:
[MarshalAs(UnmanagedType.LPStr)]StringBuilder输出变量名
//c++:
char**----c#:
string
//c++:
char**变量名----c#:
refstring变量名
//c++:
constchar*----c#:
string
//c++:
char[]----c#:
string
//c++:
char变量名[数组大小]----c#:
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)]publicstring变量名;
//c++:
struct结构体名*变量名----c#:
ref结构体名变量名
//c++:
委托变量名----c#:
委托变量名
//c++:
int----c#:
int
//c++:
int----c#:
refint
//c++:
int&----c#:
refint
//c++:
int*----c#:
refint//C#中调用前需定义int变量名=0;
//c++:
*int----c#:
IntPtr
//c++:
int32PIPTR*----c#:
int32[]
//c++:
floatPIPTR*----c#:
float[]
//c++:
double**数组名----c#:
refdouble数组名
//c++:
double*[]数组名----c#:
refdouble数组名
//c++:
long----c#:
int
//c++:
ulong----c#:
int
//c++:
UINT8*----c#:
refbyte//C#中调用前需定义byte
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c# dll 参数 转换