c#调用c++dll接口及回调函数

c#调用c++dll接口及回调函数
c#调用c++dll接口及回调函数

c#调用c++dll接口及回调函数

在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传递和返回字符串是,现总结一下,分享给大家:

VC++中主要字符串类型为:LPSTR,LPCSTR, LPCTSTR, string, CString, LPCWSTR, LPWSTR等

但转为C#类型却不完全相同。

类型对照:

BSTR --------- StringBuilder

LPCTSTR --------- StringBuilder

LPCWSTR --------- IntPtr

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封装库时会遇到以下问题:

首先是数据类型转换问题。因为C#是.NET语言,利用的是.NET的基本数据类型,所以实际上是将C++的数据类型与.NET的基本数据类型进行对应。

因为调用的是__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

例如C++的原有函数是:int __stdcall FunctionName(unsigned char param1, unsigned short param2)

其中的参数数据类型在C#中,必须转为对应的数据类型。

[DllImport("test.dll")]

public extern static int FunctionName(byte param1, ushort param2)

然后下一个问题,如果要调用的函数参数是指针或是地址变量,怎么办?

对于这种情况可以使用C#提供的非安全代码来进行解决,但是,毕竟是非托管代码,垃圾资源处理不好的话对应用程序是很不利的。所以还是使用C#提供的ref以及out修饰字比较好。

例如:int __stdcall FunctionName(unsigned char ¶m1, unsigned char *param2)

在C#中对其进行调用的方法是:

dllImport(“ file ”)]

public extern static int FunctionName(ref byte param1, ref byte param2)

最后个问题,如果c++dll接口需要传回调函数呢,c#该怎么传入。实际上c#只需要定义静态函数,并且参数类型转化为c#类型,然后传入即可。

例如:

c++文件

#define DLL_API extern "C" __declspec(dllexport)

typedef void(__stdcall* CallbackResult)(int progress, char *result);

DLL_API int StartBrush(char* comName, char* p1, char* p2, CallbackResult pFunc);

c#文件

public delegate void CallBack(int progress, string lParam); //定义委托函数类型

[DllImport("filename")]

public static extern int StartBrush(string comName, string p1, string p2, CallBack func); public static void ReportView(int progress, string lParam)

{

sb.Append(progress);

sb.Append(" ");

sb.Append(lParam);

}

private void ThreadProcSafePost()

{

CallBack myCallBack = new CallBack(Form1.ReportView);

StartBrush(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, myCallBack); }

相关主题
相关文档
最新文档