c# - 如何将 IntPtr 转换为数组?

标签 c#

如何转换IntPtr到一个数组。实际上我从非托管 dll 调用了该函数。它返回 IntPtr .现在我需要将其转换为数组。请任何人给出一个想法。代码片段如下。

Unmanaged function declared

[DllImport("NLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe IntPtr N_AllocPt1dArray(NL_INDEX n, ref stacks S); 

调用函数
void Function1()
{
     IntPtr PPtr=N_AllocPt1dArray(n, ref S);
}

现在我需要转换 PPtr到一个数组(数组是 demo[] ),其中演示由
public unsafe struct demo 
{            
    public int x ;
    public int y ;
    public int z ;
}demo DEMO;

最佳答案

尝试这个:

array[0] = (demo)System.Runtime.InteropServices.Marshal.PtrToStructure(PPtr , typeof(demo));

更新 :

this的解决方案2 page是你所需要的。

关于c# - 如何将 IntPtr 转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31854195/

相关文章:

c# - IndexOf 与自定义 StringComparer

c# - 检查是否是 bool 值?在动态 Lambda 中为真

c# - 如何在 C# 中使用 MS Interop 将数据值从 Excel 插入数据库

c# - 使用 C# 获取特定 XML 元素值的问题

c# - 为什么 for 循环初始化中的条件比循环内部更快?

C#:List<T>.ForEach(...) 比普通 foreach 循环有什么好处?

c# - 在 "null"引用(即没有订阅者的事件)上调用扩展方法是邪恶的吗?

c# - 按钮上的 onMouseDown 效果

c# - VS 2013 数据库项目中的 EF

c# - 尝试运行项目 :The module was expected to contain an assembly manifest 时出错