C#與C++?dll之間傳遞字符串string?wchar_t*?char*?IntPtr問題
C#與C++ dll之間傳遞字符串string wchar_t* char* IntPtr
1、由C#向C++ dll 傳入字符串時,參數(shù)直接用string,設置編碼格式 CharSet.Unicode CharSet.Ansi。
C++ dll接收使用wchar_t* 或 char*。
2、由C++ dll返回字符串,使用 wchar_t 或char*。
- .net 4.0 C#可以直接使用string接收,很方便。
- .net 4.0+ C# 用 IntPtr 接收,使用string接收調試不行。
dll代碼如下:
extern "C" _declspec(dllexport)const wchar_t* Diagnoser(wchar_t* inputText) { ? ? delete diagnoser; ? ? diagnoser = new FireEye::Diagnoser(); ? ? diagnoser->diagnose(inputText); ? ? diagnoser->report(); ? ? return diagnoser->reportText.c_str(); }
C#代碼如下:
//聲明 [DllImport("FireEyeDll.dll", CharSet = CharSet.Unicode , CallingConvention = CallingConvention.Cdecl)] //public static extern string Diagnoser(string inputText); //.net 4.0 public static extern IntPtr Diagnoser(string inputText); ? ? ? //調用 //outputBox.Text = Diagnoser(inputBox.Text); //.net 4.0 IntPtr outPtr = Diagnoser(inputBox.Text); outputBox.Text = Marshal.PtrToStringUni(outPtr);
C#調用C++ DLL的步驟以及遇到的亂碼等問題
C++ DLL動態(tài)庫Lib_LR.dll
函數(shù)聲明:
DLL_API_LR void GetAuthInfo(char* strCode);
函數(shù)定義:
兩個入?yún)ⅲ瑑蓚€出參
void GetAuthInfo(int inPrama1, char *inParam2, int outParam1, char *outParam2) { ? ? char buff[2048]; ? ? memset(buff, 0, 2048); ? ? std::ifstream inFile("license.lr", std::ios::binary); ? ? if (inFile.fail()) ? ? { ? ? ? ? return; ? ? } ? ? else ? ? { ? ? ? ? inFile.read(buff, sizeof(temp)); ? ? ? ? inFile.close(); ? ? } ? ? memcpy(outParam2, buff, outParam2.length()); ? ? string strCode=outParam2; ? ? outParam1=strCode.length(); }
C#調用流程
函數(shù)引用:
[DllImport("Lib_LR.dll", EntryPoint = "GetAuthInfo", CallingConvention = CallingConvention.Cdecl)] public static extern void GetAuthInfo(ref int InParam1, string nInPrama2, StringBuilder strCode);
函數(shù)調用:
int inParam1=0; int outParam1=0; string inParam2="測試"; StringBuilder outParam2= new StringBuilder(1024); //注意:調用函數(shù)的時候,int型的出口參數(shù)要加ref,出口字符串參數(shù)要聲明StringBuilder類型 GetAuthInfo(inParam1,inParam2,ref outParam1,outParam2);
遇到的問題
1、平臺不匹配:
C#工程是基于.net core的webapi項目,默認編譯平臺選擇的是Any CPU,而C++ DLL工程默認的平臺是x86,這就產(chǎn)生了調用方(C#工程)和被調用方(DLL)編譯平臺不匹配的問題??梢試L試通過以下方法來解決:C++ DLL和C#工程編譯的時候都選擇x86或者x64(前提操作系統(tǒng)是64位才行)。
2、亂碼問題:
調用GetAuthInfo函數(shù)的時候,獲取到的strCode出現(xiàn)亂碼,網(wǎng)上給出的解決方法是改變函數(shù)參數(shù)聲明方式public static extern voidGetAuthInfo([MarshalAs(UnmanagedType.LPStr)] StringBuilder strCode)。但是這種方法我試了,沒有效果。最后通過修改DLL源碼的方式得到解決:在GetAuthInfo函數(shù)的最后加上outParam2[strCode.length()] = '\0';這一行代碼,也就是在字符串數(shù)組末尾添加上結束符。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
C#實現(xiàn)DataGridView控件行列互換的方法
這篇文章主要介紹了C#實現(xiàn)DataGridView控件行列互換的方法,涉及C#中DataGridView控件元素遍歷與添加操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C# 批量生成隨機密碼必須包含數(shù)字和字母并用加密算法加密
這篇文章主要介紹了C# 批量生成隨機密碼必須包含數(shù)字和字母并用加密算法加密,需要的朋友參考下2017-01-01