C# PInvoke 输出字符串声明
在 C# PInvoke 中,如何传递字符串缓冲区以便 C DLL 填充它并返回? PInvoke 声明是什么?
C 函数声明是
int GetData(char* data, int buflength);
在 C# 中,我已将其声明为
[DllImport(DllName)]
static extern Int32 GetData([MarshalAs(UnmanagedType.LPStr)]StringBuilder receiveddata, Int32 buflen);
是否正确?我像这样传递 StringBuilder 变量
int bufferLength = 32;
StringBuilder data = new StringBuilder(bufferLength);
int result = GetData(data, bufferLength);
我想知道它是否正确?
谢谢
In C# PInvoke, how do I pass a string buffer so that the C DLL fills it and returns? What will be the PInvoke declaration?
The C function declaration is
int GetData(char* data, int buflength);
In C#, I have declared it as
[DllImport(DllName)]
static extern Int32 GetData([MarshalAs(UnmanagedType.LPStr)]StringBuilder receiveddata, Int32 buflen);
Is it correct? I'm passing the StringBuilder variable like this
int bufferLength = 32;
StringBuilder data = new StringBuilder(bufferLength);
int result = GetData(data, bufferLength);
I would like to know is it correct or not?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这是正确的。
它是这样调用的:
我曾经想对我的函数返回的字节有更多的控制,并这样做:
像这样使用:
I believe it's correct.
which is called like this:
I once wanted to have more control over the bytes returned by my function and did it like this:
used like this:
我认为这里没有必要使用 MarshalAs 属性。 StringBuilder 是 char* 输出的正确选择。
我想添加 CharSet 属性会很好,因为您在这里处理字符串。
像这样:
I don't think that using MarshalAs attribute necessary here. StringBuilder is a right choice for char* out.
I guess it will be good to add the CharSet property since you are dealing with strings here.
Like this: