C#调用C++总结
一、C/C++语言的基础
C#调用C++ DLL时类型转换 :C# 与 C++ 数据类型对照
//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byte(unsigned char) ---- c#:System.Byte //c++:SHORT(short) ---- c#:System.Int16 //c++:WORD(unsigned short) ---- c#:System.UInt16 //c++:INT(int) ---- c#:System.Int16 //c++:INT(int) ---- c#:System.Int32 //c++:UINT(unsigned int) ---- c#:System.UInt16 //c++:UINT(unsigned int) ---- c#:System.UInt32 //c++:LONG(long) ---- c#:System.Int32 //c++:ULONG(unsigned long) ---- c#:System.UInt32 //c++:DWORD(unsigned long) ---- c#:System.UInt32 //c++:DECIMAL ---- c#:System.Decimal //c++:BOOL(long) ---- c#:System.Boolean //c++:CHAR(char) ---- c#:System.Char //c++:LPSTR(char *) ---- c#:System.String //c++:LPWSTR(wchar_t *) ---- c#:System.String //c++:LPCSTR(const char *) ---- c#:System.String //c++:LPCWSTR(const wchar_t *) ---- c#:System.String //c++:PCAHR(char *) ---- c#:System.String //c++:BSTR ---- c#:System.String //c++:FLOAT(float) ---- c#:System.Single //c++:DOUBLE(double) ---- c#:System.Double //c++:VARIANT ---- c#:System.Object //c++:PBYTE(byte *) ---- c#:System.Byte[] //c++:BSTR ---- c#:StringBuilder //c++:LPCTSTR ---- c#:StringBuilder //c++:LPCTSTR ---- c#:string //c++:LPTSTR ---- c#:[MarshalAs(UnmanagedType.LPTStr)] string //c++:LPTSTR 输出变量名 ---- c#:StringBuilder 输出变量名 //c++:LPCWSTR ---- c#:IntPtr //c++:BOOL ---- c#:bool //c++:HMODULE ---- c#:IntPtr //c++:HINSTANCE ---- c#:IntPtr //c++:结构体 ---- c#:public struct 结构体{}; //c++:结构体 **变量名 ---- c#:out 变量名 //C#中提前申明一个结构体实例化后的变量名 //c++:结构体 &变量名 ---- c#:ref 结构体 变量名 //c++:WORD ---- c#:ushort //c++:DWORD ---- c#:uint //c++:DWORD ---- c#:int //c++:UCHAR ---- c#:int //c++:UCHAR ---- c#:byte //c++:UCHAR* ---- c#:string //c++:UCHAR* ---- c#:IntPtr //c++:GUID ---- c#:Guid //c++:Handle ---- c#:IntPtr //c++:HWND ---- c#:IntPtr //c++:DWORD ---- c#:int //c++:COLORREF ---- c#:uint //c++:unsigned char ---- c#:byte //c++:unsigned char * ---- c#:ref byte //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] byte[] //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] Intptr //c++:unsigned char & ---- c#:ref byte //c++:unsigned char 变量名 ---- c#:byte 变量名 //c++:unsigned short 变量名 ---- c#:ushort 变量名 //c++:unsigned int 变量名 ---- c#:uint 变量名 //c++:unsigned long 变量名 ---- c#:ulong 变量名 //c++:char 变量名 ---- c#:byte 变量名 //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示 //c++:char 数组名[数组大小] ---- c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名; ushort //c++:char * ---- c#:string //传入参数 //c++:char * ---- c#:StringBuilder//传出参数 //c++:char *变量名 ---- c#:ref string 变量名 //c++:char *输入变量名 ---- c#:string 输入变量名 //c++:char *输出变量名 ---- c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名 //c++:char ** ---- c#:string //c++:char **变量名 ---- c#:ref string 变量名 //c++:const char * ---- c#:string //c++:char[] ---- c#:string //c++:char 变量名[数组大小] ---- c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名; //c++:struct 结构体名 *变量名 ---- c#:ref 结构体名 变量名 //c++:委托 变量名 ---- c#:委托 变量名 //c++:int ---- c#:int //c++:int ---- c#:ref int //c++:int & ---- c#:ref int //c++:int * ---- c#:ref int //C#中调用前需定义int 变量名 = 0; //c++:*int ---- c#:IntPtr //c++:int32 PIPTR * ---- c#:int32[] //c++:float PIPTR * ---- c#:float[] //c++:double** 数组名 ---- c#:ref double 数组名 //c++:double*[] 数组名 ---- c#:ref double 数组名 //c++:long ---- c#:int //c++:ulong ---- c#:int //c++:UINT8 * ---- c#:ref byte //C#中调用前需定义byte 变量名 = new byte(); //c++:handle ---- c#:IntPtr //c++:hwnd ---- c#:IntPtr //c++:void * ---- c#:IntPtr //c++:void * user_obj_param ---- c#:IntPtr user_obj_param //c++:void * 对象名称 ---- c#:([MarshalAs(UnmanagedType.AsAny)]Object 对象名称 //c++:char, INT8, SBYTE, CHAR ---- c#:System.SByte //c++:short, short int, INT16, SHORT ---- c#:System.Int16 //c++:int, long, long int, INT32, LONG32, BOOL , INT ---- c#:System.Int32 //c++:__int64, INT64, LONGLONG ---- c#:System.Int64 //c++:unsigned char, UINT8, UCHAR , BYTE ---- c#:System.Byte //c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t ---- c#:System.UInt16 //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT ---- c#:System.UInt32 //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG ---- c#:System.UInt64 //c++:float, FLOAT ---- c#:System.Single //c++:double, long double, DOUBLE ---- c#:System.Double //Win32 Types ---- CLR Type //Struct需要在C#里重新定义一个Struct //CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str); //unsigned char** ppImage替换成IntPtr ppImage //int& nWidth替换成ref int nWidth //int*, int&, 则都可用 ref int 对应 //双针指类型参数,可以用 ref IntPtr //函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double fun_type1(double); //char* 的操作c++: char*; 对应 c#:StringBuilder; //c#中使用指针:在需要使用指针的地方 加 unsafe //unsigned char对应public byte /* * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg); * typedef void (*CALLBACKFUN1A)(char*, void* pArg); * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg); * 调用方式为 * [UnmanagedFunctionPointer(CallingConvention.Cdecl)] * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg); * * */
dada
C# 调用C++ DLL DLLImport说明
C#调用C++ Dll 结构体
C# 非托管内存中分配内存注意 Marshal.AllocHGlobal
三、常见问题及处理
1、字符串乱码问题
C# 调用C++ dll 返回char*调用方式(StringBuilder乱码) 使用MarshalAs(UnmanagedType.LPStr)可解决英文字符串乱码问题,但不能完全解决中文乱码问题。
由于C++默认字符串为utf-8编码,C#使用[MarshalAs(UnmanagedType.LPStr)] StringBuilder param对应 char * param处理后需要进行utf-8转Unicode的处理,实际开发过程中发现调用Dll取得的字符串可能出现“?”(偶数个汉字正常,奇数个汉字乱码
),这对返回Json字符串的函数影响很大。
推荐使用.net Framework4.7提供的MarshalAs(UnmanagedType.LPUTF8Str)代替MarshalAs(UnmanagedType.LPStr)。使用LPUTF8Str取到的字符串不需转码处理。参见:https://www.e-learn.cn/content/wangluowenzhang/841422
还可以使用自定义封送的方法解决奇数中文乱码的问题
public class UTF8MarshalerForStringBuilder : ICustomMarshaler { private static UTF8MarshalerForStringBuilder instance; object oo; public static ICustomMarshaler GetInstance(string cookie) { if (null == instance) { instance = new UTF8MarshalerForStringBuilder(); } return instance; } public IntPtr MarshalManagedToNative(object managedObj) { if (object.ReferenceEquals(managedObj, null)) return IntPtr.Zero; if (!(managedObj is StringBuilder)) throw new MarshalDirectiveException("UTF8Marshaler must be used on a StringBuilder."); //throw new InvalidOperationException(); oo = managedObj; StringBuilder sb = managedObj as StringBuilder; byte[] utf8bytes = new byte[sb.Capacity]; IntPtr ptr = Marshal.AllocHGlobal(utf8bytes.Length + 1); Marshal.Copy(utf8bytes, 0, ptr, utf8bytes.Length); Marshal.WriteByte(ptr, utf8bytes.Length, 0); return ptr; } public object MarshalNativeToManaged(IntPtr pNativeData) { if (pNativeData == IntPtr.Zero) return null; List<byte> bytes = new List<byte>(); for (int offset = 0; ; offset++) { byte b = Marshal.ReadByte(pNativeData, offset); if (b == 0) break; else bytes.Add(b); } string str = Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count); ((StringBuilder)oo).Append(str); return oo; } public void CleanUpManagedData(object managedObj) { } public void CleanUpNativeData(IntPtr pNativeData) { Marshal.FreeHGlobal(pNativeData); } public int GetNativeDataSize() { return -1; } }
调用方法与LPStr类似:
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8MarshalerForStringBuilder))]StringBuilder json
string封送:
public class UTF8MarshalerForString : ICustomMarshaler { private static UTF8MarshalerForString instance; public static ICustomMarshaler GetInstance(string cookie) { if (null == instance) { instance = new UTF8MarshalerForString(); } return instance; } public void CleanUpManagedData(object managedObj) { } public void CleanUpNativeData(IntPtr pNativeData) { Marshal.FreeHGlobal(pNativeData); } public int GetNativeDataSize() { return -1; } public IntPtr MarshalManagedToNative(object managedObj) { if (object.ReferenceEquals(managedObj, null)) return IntPtr.Zero; if (!(managedObj is string)) throw new MarshalDirectiveException("UTF8Marshaler must be used on a string."); //throw new InvalidOperationException(); byte[] utf8bytes = Encoding.UTF8.GetBytes(managedObj as string); IntPtr ptr = Marshal.AllocHGlobal(utf8bytes.Length + 1); Marshal.Copy(utf8bytes, 0, ptr, utf8bytes.Length); Marshal.WriteByte(ptr, utf8bytes.Length, 0); return ptr; } public object MarshalNativeToManaged(IntPtr pNativeData) { if (pNativeData == IntPtr.Zero) return null; List<byte> bytes = new List<byte>(); for (int offset = 0; ; offset++) { byte b = Marshal.ReadByte(pNativeData, offset); if (b == 0) break; else bytes.Add(b); } return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count); } }
2、C#项C++ DLL 传递“函数指针”(委托)引起程序崩溃的问题
【C#】对“xxxx::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用。 此错误由于传递给C++的委托方法被回收引起,可使用GC.KeepAlive(callback) 可解决这个错误。