日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#與C++?dll之間傳遞字符串string?wchar_t*?char*?IntPtr問題_C#教程

作者:tinghe17 ? 更新時間: 2022-12-08 編程語言

C#與C++ dll之間傳遞字符串string wchar_t* char* IntPtr

1、由C#向C++ dll 傳入字符串時,參數直接用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動態庫Lib_LR.dll

函數聲明:

DLL_API_LR void GetAuthInfo(char* strCode);

函數定義:

兩個入參,兩個出參

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#調用流程

函數引用:

[DllImport("Lib_LR.dll", EntryPoint = "GetAuthInfo", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetAuthInfo(ref int InParam1, string nInPrama2, StringBuilder strCode);

函數調用:

int inParam1=0;
int outParam1=0;
string inParam2="測試";
StringBuilder outParam2= new StringBuilder(1024);
//注意:調用函數的時候,int型的出口參數要加ref,出口字符串參數要聲明StringBuilder類型
GetAuthInfo(inParam1,inParam2,ref outParam1,outParam2);

遇到的問題

1、平臺不匹配:

C#工程是基于.net core的webapi項目,默認編譯平臺選擇的是Any CPU,而C++ DLL工程默認的平臺是x86,這就產生了調用方(C#工程)和被調用方(DLL)編譯平臺不匹配的問題。可以嘗試通過以下方法來解決:C++ DLL和C#工程編譯的時候都選擇x86或者x64(前提操作系統是64位才行)。

2、亂碼問題:

調用GetAuthInfo函數的時候,獲取到的strCode出現亂碼,網上給出的解決方法是改變函數參數聲明方式public static extern voidGetAuthInfo([MarshalAs(UnmanagedType.LPStr)] StringBuilder strCode)。但是這種方法我試了,沒有效果。最后通過修改DLL源碼的方式得到解決:在GetAuthInfo函數的最后加上outParam2[strCode.length()] = '\0';這一行代碼,也就是在字符串數組末尾添加上結束符。

原文鏈接:https://blog.csdn.net/tinghe17/article/details/114381046tinghe17

欄目分類
最近更新