【发布时间】:2019-02-14 17:47:06
【问题描述】:
我有一个 .NET Core 2.2 C# 应用程序,它使用 DllImport 在 CentOS 7.5 上提取本机共享库(使用 gcc 编译的 C++ extern "C" 接口)。 C++ 库中的函数需要 wchar_t * 参数,但这些参数似乎被编组为 UTF16 字符串,而不是 gcc/glibc 中实现的 UTF32 字符串。这是(我的)程序员错误还是应该向 .NET Core 团队提出?
这是我试图调用的高度复杂的方法:
void wchar_tTest(const wchar_t *arg1, const wchar_t *arg2)
{
std::wcout << L"wchar_tTest: arg1: " << arg1 << L", arg2: " << arg2 << std::endl;
char *s = (char *)arg1;
for (int i = 0; i < 12; i++)
{
printf("%d: %c\n", i, s[i]);
}
}
我尝试在托管端的 DllImport 上使用 MarshalAs(UnmanagedType.LPWSTR) 和/或 CharSet.Unicode 无济于事。这些都产生相似的结果:
[DllImport("cover", EntryPoint = "wchar_tTest", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern void LPWSTRStringTest([MarshalAs(UnmanagedType.LPWStr)] string arg1, [MarshalAs(UnmanagedType.LPWStr)] string arg2);
[DllImport("cover", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern void wchar_tTest(string arg1, string arg2);
调用看起来像这样(stringTest() 是一个类似的调用,但调用的是带有 char * 参数的函数):
string arg1 = "Hello!";
string arg2 = "Goodbye!";
stringTest(arg1, arg2);
wchar_tTest(arg1, arg2);
LPWSTRStringTest(arg1, arg2);
当通过wcout转储参数时,Hello!变为Hlo,Goodbye!变为Gobe。当您逐个字符地遍历时,输出看起来很像 UTF16...看起来wchar_t * 跳过了所有其他 UTF16 字符(我假设将其视为 UTF32 字符串)。
wchar_tTest: arg1: Hlo, arg2: Gobe
0: H
1:
2: e
3:
4: l
5:
6: l
7:
8: o
9:
10: !
11:
有没有办法在不进行自定义编组的情况下解决这个问题?毕竟我读过这似乎应该是一个简单的任务,但我在这里。
【问题讨论】:
标签: c# linux gcc .net-core pinvoke