将 C# 字符串传递给 Windows Mobile 中的非托管 C DLL
我有一个非托管 c++ DLL,需要从 Windows Mobile C# 应用程序 调用。
我有 C# 包装器,它在桌面上运行得很好。我可以从 C# 桌面程序调用 DLL 函数并毫无问题地传递字符串。
但是,当我编译移动平台的库和包装器时,我在 DllImport 行中收到错误,指出无法识别 CharSet.ANSI。我允许编写的唯一选项是 CharSet.Auto 和 CharSet.Unicode。
问题是,无论此设置如何,c++ 函数中接收到的字符串都是宽字符字符串,而不是它们所期望的纯 char* 字符串。
我们可以使用 wcstombs() 来翻译每个 C++ 函数开头的所有字符串,但我不想将 lib 修改到这样的程度...
有没有办法修复 C# 和 C 之间的编组可以与 .NET Compact Framework 一起使用吗?
I've got an unmanaged c++ DLL that I need to call from a Windows Mobile C# app.
I've got the C# wrapper and it works nicely in desktop. I can call the DLL functions from a C# desktop program and pass strings around with no problem.
However, when I compile the lib and the wrapper for the mobile platform, I get an error in the DllImport lines saying that the CharSet.ANSI is not recognized. The only options I'm allowed to write are CharSet.Auto and CharSet.Unicode.
The problem is that, regardless of this setting, the strings that are received in the c++ functions are wide char strings, and not plain char* strings that is what they expect.
We can use wcstombs() to translate all strings at the beginning of each c++ function, but I'd rather not modify the lib to such an extent...
Is there a way to fix the marshalling between C# and C that works with the .NET Compact Framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,没有。
Microsoft 文档 指定:
解决方案是:
DLL中的函数
包装器
调用代码
No, there isn't.
Microsoft documentation specifies that:
The solution is:
Functions in the DLL
Wrapper
Calling Code
Windows CE 严重偏向 Unicode(大多数 Win32 API 甚至没有 ANSI 等效项)。因此,CF 在 ANSI 方面的表现也不是很好,它需要一点“帮助”来使其正确。
您可以使用 MarshalAs 属性 (MSDN 文档清楚地表明它在 CF 中受支持),大致如下:
Windows CE is heavily biased toward Unicode (most Win32 APIs don't even have ANSI equivalents). As such, the CF doesn't really do well with ANSI either and it needs a little "help" in getting it right.
You can tell the marshaler that you want to pass the data as single-byte, null terminated values by using the MarshalAs attribute (the MSDN docs clearly show it is supported in the CF), something along these lines:
我发现这个marshal编译器很有用,尽管它有点错误。
I find this marshal compiler useful even thou it is a bit buggy.