在一些应用系统中经常会用到C#调用C++的一些代码,特别是结构体、指针、引用等类型的传递。总结一下常用的一些类型传递。官方关于C#与C++互操作的文档
http://msdn.microsoft.com/en-us/library/ms173184.aspx,官方的一个例子:http://msdn.microsoft.com/en-us/library/ms173187.aspx。
1。 C#与 C/C++的常用等效类型
参考:http://msdn.microsoft.com/en-us/library/ms235282.aspx
wtypes.h | Visual C++ | Visual C++ with /clr | Common language runtime |
---|---|---|---|
HANDLE | void * | void * | IntPtr, UIntPtr |
BYTE | unsigned char | unsigned char | Byte |
SHORT | short | short | Int16 |
WORD | unsigned short | unsigned short | UInt16 |
INT | int | int | Int32 |
UINT | unsigned int | unsigned int | UInt32 |
LONG | long | long | Int32 |
BOOL | long | bool | Boolean |
DWORD | unsigned long | unsigned long | UInt32 |
ULONG | unsigned long | unsigned long | UInt32 |
CHAR | char | char | Char |
LPCSTR | char * | String ^ [in], StringBuilder ^ [in, out] | String ^ [in], StringBuilder ^ [in, out] |
LPCSTR | const char * | String ^ | String |
LPWSTR | wchar_t * | String ^ [in], StringBuilder ^ [in, out] | String ^ [in], StringBuilder ^ [in, out] |
LPCWSTR | const wchar_t * | String ^ | String |
FLOAT | float | float | Single |
DOUBLE | double | double | Double |
1) 创建C/C++动态链接库,导出接口函数;
2) 在C#中创建接口的声明,导入接口函数;
3) 调用函数。
(待续)