将 StringBuilder 传递给 PInvoke 函数
在一篇标题为“从 C# 调用返回字符串的 C++ 方法”的帖子中,
它说,要使以下 Pinvoke 工作,请将 C++ 签名更改为
extern "C" REGISTRATION_API void calculate(LPSTR msg)
C++ 代码
extern "C" REGISTRATION_API void calculate(char* msg)
C# 代码
[DllImport("thecpp.dll", CharSet=CharSet.Ansi)]
static extern void calculate(StringBuilder sMsg);
作为一个类的 stringBuilder 如何转换为long ptr 到 string 。(但这是公认的答案)
我们是否应该使用如下所示的 use IntPtr ?
extern "C" REGISTRATION_API void calculate(Intptr msg)
In one of the post Titled "Call a c++ method that returns a string, from c#"
Its said that , to make the following Pinvoke to work change the C++ signature to as
extern "C" REGISTRATION_API void calculate(LPSTR msg)
C++ code
extern "C" REGISTRATION_API void calculate(char* msg)
C# code
[DllImport("thecpp.dll", CharSet=CharSet.Ansi)]
static extern void calculate(StringBuilder sMsg);
How can stringBuilder which is a class ,convertd to long ptr to string .(but this is the accepted answer)
Shouldnt we use use IntPtr as below ?
extern "C" REGISTRATION_API void calculate(Intptr msg)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查找标记为“传递字符串”的部分,封送拆收器具有一些额外的智能来执行此操作。
http:// msdn.microsoft.com/en-us/library/aa446536.aspx
Look for the section marked "Passing Strings", the marshaler has got some added smarts to do this trick.
http://msdn.microsoft.com/en-us/library/aa446536.aspx