将带有嵌入字符串的结构传递给 C dll 时,C# P/Invoke 不起作用
我有一个旧的 C dll,需要用 C# 代码包装。我使用 P/Invoke 签名 takeit 获得以下结构定义:
[StructLayout(LayoutKind.Sequential)]
internal struct SDRMSG
{
public uint Stream;
public uint Function;
public uint Wbit;
public int Length;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Buffer;
public int Error;
public int Next;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Txtp;
public int Txtc;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Wtxtp;
public int Wtxtc;
}
从原始 C 定义:
typedef struct {
unsigned int stream;
unsigned int function;
unsigned int wbit;
SDRLENGTH length;
unsigned char * buffer;
SDRLENGTH error;
int next;
unsigned char * txtp;
SDRLENGTH txtc;
unsigned char * wtxtp;
SDRLENGTH wtxtc;
} SDRMSG;
发生的情况是我需要初始化此结构并将其发送到 C .dll,其中 .dll 将用数据填充缓冲区字段。原始 C 结构中的缓冲区字段只是一个指向我在客户端代码中初始化的 char 数组的指针。有了指针,C .dll 就能够直接写入缓冲区。我试图使用 C# 代码获得相同的结果,但无论我尝试什么,我似乎都无法正确初始化缓冲区字段,以便 C .dll 可以用数据填充它。它总是返回到我的客户端代码为空。这是一种似乎不起作用的尝试(还有其他几种我不太记得的尝试)。
secsMessage.Stream = 1;
secsMessage.Function = 13;
secsMessage.Wbit = 1;
secsMessage.Length = 8000;
secsMessage.Buffer = new string('\0', 8000);
谁能帮我弄清楚如何初始化该结构以便 C .dll 可以写入缓冲区?
I have an old C dll that I need to wrap with C# code. I used the P/Invoke signature tookit to get the following structure definition:
[StructLayout(LayoutKind.Sequential)]
internal struct SDRMSG
{
public uint Stream;
public uint Function;
public uint Wbit;
public int Length;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Buffer;
public int Error;
public int Next;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Txtp;
public int Txtc;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Wtxtp;
public int Wtxtc;
}
from the original C definition:
typedef struct {
unsigned int stream;
unsigned int function;
unsigned int wbit;
SDRLENGTH length;
unsigned char * buffer;
SDRLENGTH error;
int next;
unsigned char * txtp;
SDRLENGTH txtc;
unsigned char * wtxtp;
SDRLENGTH wtxtc;
} SDRMSG;
What happens is I need to initialize this structure and send it to a C .dll where the .dll will fill the buffer field with data. The buffer field in the original C struct was just a pointer to a char array that I initialized in my client code. Armed with the pointer, the C .dll was able to write directly into the buffer. I am trying to get the same result with C# code but no matter what I try, I can't seem to get the buffer field initialized correctly so that the C .dll can fill it with data. It always comes back to my client code empty. Here is one attempt (among several others that I can't quite recall) that does not seem to work.
secsMessage.Stream = 1;
secsMessage.Function = 13;
secsMessage.Wbit = 1;
secsMessage.Length = 8000;
secsMessage.Buffer = new string('\0', 8000);
Can anyone help me figure out how to initialize the structure so the C .dll can write to the buffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此示例应该会为您指明正确的道路。
This example should set you down the right path.