【发布时间】:2018-07-18 09:00:34
【问题描述】:
我正在尝试从 c++ 向 csharp 发送一条消息,但我的一些口音在途中丢失了(不是全部??) ps:用意大利语写作
这是我的工作: c++:
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
extern "C" {
DLL_API void __cdecl getResults(char* entry, wchar_t* result);
}
[...]
void getResults(char* entry,wchar_t* result)
{
std::string str(entry);
std::string Stringresult= "héà" ;
std::wstring wsTmp(Stringresult.begin(), Stringresult.end());
const wchar_t* constChar = wsTmp.c_str();
swprintf(result, Stringresult.length(), constChar);
c#:
[DllImport("libface.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void getResults([MarshalAs(UnmanagedType.LPStr)] string entry, StringBuilder res);
static void Main()
{
StringBuilder result = new StringBuilder(2000);
string entry = Console.ReadLine();
getResults( entry,result);
Console.WriteLine(result);
【问题讨论】:
-
您需要开始使用正确的文本编码。通常首选 Unicode 编码 UTF8。 UTF16 可能更方便。
-
你能解释一下怎么做吗?谢谢
-
周围有很多例子。尝试一些研究。
标签: c# c++ dll utf-8 dllimport