PINVOKE 查询(无符号长*)

发布于 2024-11-06 01:47:33 字数 247 浏览 1 评论 0原文

我正在尝试调用一个采用以下结构作为参数的 C 函数:

typedef struct CSTRUCT
{
    unsigned long* p1;
    unsigned long* p2;
    unsigned long* p3;
    unsigned long* p4;
} CSTRUCT;

我需要从 C# 调用该函数,并且需要知道该 C 结构的 pinvoke C# 等效项。有人可以帮忙吗?

I'm trying to call into a C function that takes the following struct as a parameter:

typedef struct CSTRUCT
{
    unsigned long* p1;
    unsigned long* p2;
    unsigned long* p3;
    unsigned long* p4;
} CSTRUCT;

I need to call the function from C# and need to know the pinvoke C# equivalent for this C struct. Can anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深居我梦 2024-11-13 01:47:33

C 中的 unsigned long* p1 只是一个指针,您通常在 C# 中将其声明为 IntPtr。这是该过程中最简单的部分。困难的部分是如何分配给指针。指向内存的指针是否属于 C 代码?或者它是 C# 代码拥有的内存吗?谁写入该内存,是 C 代码还是 C# 代码?在取得进展之前,您需要知道这些问题的答案。

unsigned long* p1 in C is simply a pointer which you would typically declare as IntPtr in C#. That's the easy part of the process. The hard part is how to assign to the pointer. Is the pointer to memory owned by the C code? Or is it memory owned by the C# code? And who writes to that memory, the C code or the C# code? You need to know the answers to those questions before you can make forward progress.

无妨# 2024-11-13 01:47:33

我发现 P/Invoke 签名生成器很有用,它可以免费使用 下载(通过 MSDN 杂志和 此博客

它生成的c#代码是:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct CSTRUCT {

    /// unsigned int*
    public System.IntPtr p1;

    /// unsigned int*
    public System.IntPtr p2;

    /// unsigned int*
    public System.IntPtr p3;

    /// unsigned int*
    public System.IntPtr p4;
}

One tool I find useful is the P/Invoke signature generator, available as a free download (via MSDN Magazine and this blog)

The c# code it generates is:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct CSTRUCT {

    /// unsigned int*
    public System.IntPtr p1;

    /// unsigned int*
    public System.IntPtr p2;

    /// unsigned int*
    public System.IntPtr p3;

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