C 到Csharp String与Dllimport通过,丢失的口音
c++ to csharp string passing with dllimport , lost accents
我试图将消息从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);
通过此链接解决了问题(该链接说明问题要复杂得多,而某些人可能会想到....(:
http://blog.kutulu.org/2012/04/marshaling-utf-8-harder-than-iat-ught.html
C 代码:
extern "C" {
DLL_API char* __cdecl getResults(char* entry);
}
char* getResults(char* entry)
{
std::string Stringresult= "hàé";
char *cstr = new char[Stringresult.length() + 1];
strcpy(cstr, Stringresult.c_str());
return cstr;
}
c#代码:
[DllImport("libface.dll" , EntryPoint = "getResults")]
private static extern IntPtr getResults([MarshalAs(UnmanagedType.LPStr)] string entry);
static void Main()
{
var data = new List<byte>();
var ptr = getResults("p");
var off = 0;
while (true)
{
var ch = Marshal.ReadByte(ptr, off++);
if (ch == 0)
{
break;
}
data.Add(ch);
}
string sptr = Encoding.UTF8.GetString(data.ToArray());
Console.WriteLine(sptr);
相关文章:
- 通过方法访问结构
- 通过递归进行因子分解
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 尝试通过多个向量访问变量时,向量下标超出范围
- C++如何通过用户输入删除列表元素
- 通过多个头文件使用常量变量
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何通过派生类函数更改基类中的向量
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- Ctypes wstring通过引用传递
- 变量没有改变?通过向量的函数调用
- 如何通过cpp程序运行shell脚本
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- QT通过C++添加映射QML项目
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- 如何通过 getter 函数删除矢量的元素?
- C 到Csharp String与Dllimport通过,丢失的口音