C#调用C、C++的lib(类型对应)

C++,C 与 C# 类型对应

C++ C#
HANDLE(void *) System.IntPtr
Byte(unsigned char) System.Byte
SHORT(short) System.Int16
WORD(unsigned short) System.UInt16
INT(int) System.Int16
INT(int) System.Int32
UINT(unsigned int) System.UInt16
UINT(unsigned int) System.UInt32
LONG(long) System.Int32
ULONG(unsigned long) System.UInt32
DWORD(unsigned long) System.UInt32
DECIMAL System.Decimal
BOOL(long) System.Boolean
CHAR(char) System.Char
LPSTR(char *) System.String
LPWSTR(wchar_t *) System.String
LPCSTR(const char *) System.String
LPCWSTR(const wchar_t *) System.String
PCAHR(char *) System.String
BSTR System.String
FLOAT(float) System.Single
DOUBLE(double) System.Double
VARIANT System.Object
PBYTE(byte *) System.Byte[]
BSTR StringBuilder
LPCTSTR StringBuilder
LPCTSTR string
LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string
LPTSTR 输出变量名 StringBuilder 输出变量名
LPCWSTR IntPtr
BOOL bool
HMODULE IntPtr
HINSTANCE IntPtr
结构体 public struct 结构体{};
结构体 **变量名 out 变量名 //C#中提前申明一个结构体实例化后的变量名
结构体 &变量名 ref 结构体 变量名
WORD ushort
DWORD uint
DWORD int
UCHAR int
UCHAR byte
UCHAR* string
UCHAR* IntPtr
GUID Guid
Handle IntPtr
HWND IntPtr
DWORD int
COLORREF uint
unsigned char byte
unsigned char * ref byte
unsigned char * [MarshalAs(UnmanagedType.LPArray)] byte[]
unsigned char * [MarshalAs(UnmanagedType.LPArray)] Intptr
unsigned char & ref byte
unsigned char 变量名 byte 变量名
unsigned short 变量名 ushort 变量名
unsigned int 变量名 uint 变量名
unsigned long 变量名 ulong 变量名
char 变量名 byte 变量名 //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示
char 数组名[数组大小] MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名; ushort
char * string //传入参数
char * StringBuilder//传出参数
char *变量名 ref string 变量名
char *输入变量名 string 输入变量名
char *输出变量名 [MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名
char ** string
char **变量名 ref string 变量名
const char * string
char[] string
char 变量名[数组大小] [MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名;
struct 结构体名 *变量名 ref 结构体名 变量名
委托 变量名 委托 变量名
int int
int ref int
int & ref int
int * ref int //C#中调用前需定义int 变量名 = 0;
*int IntPtr
int32 PIPTR * int32[]
float PIPTR * float[]
double** 数组名 ref double 数组名
double*[] 数组名 ref double 数组名
long int
ulong int
UINT8 * ref byte //C#中调用前需定义byte 变量名 = new byte();
handle IntPtr
hwnd IntPtr
void * IntPtr
void * user_obj_param IntPtr user_obj_param
void * 对象名称 [MarshalAs(UnmanagedType.AsAny)]Object 对象名称
char, INT8, SBYTE, CHAR System.SByte
short, short int, INT16, SHORT System.Int16
int, long, long int, INT32, LONG32, BOOL , INT System.Int32
__int64, INT64, LONGLONG System.Int64
unsigned char, UINT8, UCHAR , BYTE System.Byte
unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t System.UInt16
unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT System.UInt32
unsigned __int64, UINT64, DWORDLONG, ULONGLONG System.UInt64
float, FLOAT System.Single
double, long double, DOUBLE System.Double
unsigned char public byte
char* StringBuilder
typedef double (fun_type1)(double) public delegate double fun_type1(double);
双针指类型参数 ref IntPtr
int*, int& ref int
int& nWidth ref int nWidth
unsigned char** ppImage IntPtr ppImage
Struct 重新定义一个Struct
CallBack回调函数 delegate static extern int FunCallBack(string str);
  • 公共方法
// __stdcall表示调用约定:参数都是从右向左通过堆栈传递, 函数调用在返回前要由被调用者清理堆栈。
extern “C” __declspec(dllexport) int __stdcall Add(int n1, int n2);

调用方式为

[DllImport(“SampleCppWrapper.dll”)]
private static extern int Add(int n1, int n2);
  • 回调函数
typedef void (__stdcall *LoopCallbackWrapper)(void* pContext);
extern "C" __declspec(dllexport) void __stdcall SetCallbackFunc(LoopCallbackWrapper callback);
extern "C" __declspec(dllexport) void __stdcall SetCallbackContext(void* pContext);
extern "C" __declspec(dllexport) void __stdcall Loop();

调用方式为

private delegate void LoopCallbackHandler(IntPtr pContext);
private static LoopCallbackHandler callback = LoopCallback;

[DllImport("SampleCppWrapper.dll")]
private static extern void SetCallbackFunc(LoopCallbackHandler callback);
[DllImport("SampleCppWrapper.dll")]
private static extern void SetCallbackContext(IntPtr pContext);
[DllImport("SampleCppWrapper.dll")]
private static extern void Loop();

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

💰

×

Help us with donation