PINVOKE 查询(无符号长*)
我正在尝试调用一个采用以下结构作为参数的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C 中的
unsigned long* p1
只是一个指针,您通常在 C# 中将其声明为IntPtr
。这是该过程中最简单的部分。困难的部分是如何分配给指针。指向内存的指针是否属于 C 代码?或者它是 C# 代码拥有的内存吗?谁写入该内存,是 C 代码还是 C# 代码?在取得进展之前,您需要知道这些问题的答案。unsigned long* p1
in C is simply a pointer which you would typically declare asIntPtr
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.我发现 P/Invoke 签名生成器很有用,它可以免费使用 下载(通过 MSDN 杂志和 此博客)
它生成的c#代码是:
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: