将带有嵌入字符串的结构传递给 C dll 时,C# P/Invoke 不起作用

发布于 2024-10-09 19:46:52 字数 1476 浏览 3 评论 0原文

我有一个旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-10-16 19:46:52

示例应该会为您指明正确的道路。

This example should set you down the right path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文