网站页面策划,做行业导航网站好,美妆网站制作教程,网站开发的案例分析模板在合作开发时#xff0c;C#时常需要调用CDLL#xff0c;当传递参数时时常遇到问题#xff0c;尤其是传递和返回字符串是#xff0c;现总结一下#xff0c;分享给大家#xff1a;VC中主要字符串类型为#xff1a;LPSTR,LPCSTR, LPCTSTR, string, CString, LPCWSTR, LPWST… 在合作开发时C#时常需要调用CDLL当传递参数时时常遇到问题尤其是传递和返回字符串是现总结一下分享给大家VC中主要字符串类型为LPSTR,LPCSTR, LPCTSTR, string, CString, LPCWSTR, LPWSTR等但转为C#类型却不完全相同。类型对照BSTR --------- StringBuilderLPCTSTR --------- StringBuilderLPCWSTR --------- IntPtrhandle---------IntPtrhwnd-----------IntPtrchar *----------stringint * -----------ref intint -----------ref intvoid *----------IntPtrunsigned char *-----ref byteStruct需要在C#里重新定义一个StructCallBack回调函数需要封装在一个委托里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变为了byteunsigned short变为了ushort。变换后参数的数据类型不变只是声明方式必须改为.NET语言的规范。我们可以通过下表来进行这种转换 Win32 Types CLR Typechar, INT8, SBYTE, CHAR System.SByteshort, short int, INT16, SHORT System.Int16int, long, long int, INT32, LONG32, BOOL , INT System.Int32__int64, INT64, LONGLONG System.Int64unsigned char, UINT8, UCHAR , BYTE System.Byteunsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t System.UInt16unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT System.UInt32unsigned __int64, UINT64, DWORDLONG, ULONGLONG System.UInt64float, FLOAT System.Singledouble, 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 param1, unsigned char *param2)在C#中对其进行调用的方法是dllImport(“ file ”)] public extern static int FunctionName(ref byte param1, ref byte param2)最后个问题如果cdll接口需要传回调函数呢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);} 参考博文http://www.cnblogs.com/82767136/articles/2517457.html