handle---------IntPtr hwnd-----------IntPtr char *----------string int * -----------ref int int &-----------ref int void *----------IntPtr unsigned char *-----ref byte Struct需要在C#里重新定义一个Struct CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str); 注意在每个函数的前面加上public static extern +返回的数据类型,如果不加public ,函数默认为私有函数,调用就会出错。 在C#调用C++ DLL封装库时会出现两个问题: 1. 数据类型转换问题 2. 指针或地址参数传送问题 首先是数据类型转换问题。因为C#是.NET语言,利用的是.NET的基本数据类型,所以实际上是将C++的数据类型与.NET的基本数据类型进行对应。 例如C++的原有函数是: int __stdcall FunctionName(unsigned char param1, unsigned short param2) 其中的参数数据类型在C#中,必须转为对应的数据类型。如: [DllImport(“ COM DLL path/file ”)] extern static int FunctionName(byte param1, ushort param2) 因为调用的是__stdcall函数,所以使用了P/Invoke的调用方法。其中的方法FunctionName必须声明为静态外部函数,即加上extern static声明头。我们可以看到,在调用的过程中,unsigned char变为了byte,unsigned short变为了ushort。变换后,参数的数据类型不变,只是声明方式必须改为.NET语言的规范。 我们可以通过下表来进行这种转换:
Win32 Types | CLR Type | 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 |
之后再将CLR的数据类型表示方式转换为C#的表示方式。这样一来,函数的参数类型问题就可以解决了。 现在,我们再来考虑下一个问题,如果要调用的函数参数是指针或是地址变量,怎么办? 对于这种情况可以使用C#提供的非安全代码来进行解决,但是,毕竟是非托管代码,垃圾资源处理不好的话对应用程序是很不利的。所以还是使用C#提供的ref以及out修饰字比较好。 同上面一样,我们也举一个例子: int __stdcall FunctionName(unsigned char ¶m1, unsigned char *param2) 在C#中对其进行调用的方法是: [DllImport(“ file ”)] extern static int FunctionName(ref byte param1, ref byte param2) 看到这,可能有人会问,&是取地址,*是传送指针,为何都只用ref就可以了呢?一种可能的解释是ref是一个具有重载特性的修饰符,会自动识别是取地址还是传送指针。 在实际的情况中,我们利用参数传递地址更多还是用在传送数组首地址上。 如:byte[] param1 = new param1(6); 在这里我们声明了一个数组,现在要将其的首地址传送过去,只要将param1数组的第一个元素用ref修饰。具体如下: [DllImport(“ file ”)] extern static int FunctionName(ref byte param1[1], ref byte param2)
Wtypes.h 中的非托管类型 | 非托管 C语言类型 | 托管类名 | 说明 | HANDLE | void* | System.IntPtr | 32 位 | BYTE | short | System.Int16 | 16 位 | WORD | unsigned short | System.UInt16 | 16 位 | INT | int | System.Int32 | 32 位 | UINT | unsigned int | System.UInt32 | 32 位 | LONG | long | System.Int32 | 32 位 | BOOL | long | System.Int32 | 32 位 | DWORD | unsigned long | System.UInt32 | 32 位 | ULONG | unsigned long | System.UInt32 | 32 位 | CHAR | char | System.Char | 用 ANSI 修饰。 | LPSTR | char* | System.String或 System.StringBuilder | 用 ANSI 修饰。 | LPCSTR | Const char* | System.String 或 System.StringBuilder | 用 ANSI 修饰。 | LPWSTR | wchar_t* | System.String 或 System.StringBuilder | 用 Unicode 修饰。 | LPCWSTR | Const wchar_t* | System.String 或 System.StringBuilder | 用 Unicode 修饰。 | FLOAT | Float | System.Single | 32 位 | DOUBLE | Double | System.Double | 64 位 | |