P /调用C函数返回指针结构 [英] p/invoke C function that returns pointer to a struct

查看:120
本文介绍了P /调用C函数返回指针结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在C#中声明,返回一个指向一个结构的C函数?



我相信下面是做到这一点,其次是单向的 Marshal.PtrToStructure 以获得实际结构值。

  // C函数
SimpleStruct *功能(无效);

// C#进口
函数[DllImport(MYDLL.DLL)]
公共静态外部的IntPtr功能();




  1. 我是正确看法?

  2. 是否有其他的方法来完成相同的? (这将是确定度日值结构体背面)


解决方案

由于该函数返回一个指针(希望不是本地分配的呢?)你最好的赌注是手动元帅它(通过的 Marshal.PtrToStructure )。



如果它是你可以创建一个使用的PInvoke结构的托管版本的参数然后互操作助理通过ref或out通过。


How do I declare in C# a C function that returns a pointer to a structure?

I believe following is one way to do that, followed by Marshal.PtrToStructure to get actual structure value.

// C-function
SimpleStruct * Function(void);

// C# import
[DllImport("MyDll.dll")]
public static extern IntPtr Function();

  1. Am I correct about that?
  2. Are there other ways to accomplish the same? (It would be OK to get struct back by value)

解决方案

Since the function returns a pointer (hopefully not a locally allocated one?) your best bet is to manually marshal it (via Marshal.PtrToStructure).

If it were a parameter you could create a managed version of the structure using the PInvoke Interop Assistant then pass it via ref or out.

这篇关于P /调用C函数返回指针结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆