DLLimport函数返回指向结构的指针数组

DLLimport函数返回指向结构的指针数组

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

问题描述

你好,

我需要调用一个函数,该函数返回指向结构的指针数组:

这是C ++声明:

Hello,

I need to call a function which returns an array of pointer to structures :

here is the c++ declaration :

const VDXPluginInfo *const *VDXAPIENTRY VDGetPluginInfo();



它返回一个指向VDXAPIENTRY的指针数组,该数组由指向零的指针终止.

如何将其转换为从c#调用它?

当然,我在网上搜索,发现了很多关于参数中的指针数组的东西,但从未从函数返回.


谢谢.



it returns an array of pointers to a VDXAPIENTRY, which is terminated by a pointer to zero

How can I translate this to call it from c# ?

Of course, I searched the web, and found many things about pointer arrays in arguments, but never returned from function.


Thank you.

推荐答案

[DllImport("**YOURDLL**"]
private static extern int VDGetPluginInfo();

[StructLayout(LayoutKind.Sequential)]
private struct VDPointer
{
    public int VDXPlugin;
}

public List<intptr> GetPointers()
{
   VDPointer Result = new VDPointer();
   Result.VDXPlugin = -1; //we want our first run to go
   List<intptr> ToFill = new List<intptr>();
   int Retrieve = VDGetPluginInfo; //We should have gotten start adres of the array on our int

   while (Result. VDXPlugin != 0)
   {
      Result = (VDPointer)Marshal.PtrToStructure(new IntPtr(Retrieve), typeof(VDPointer));
      if (Result.VDXPlugin != 0)
      {
          Retrieve += 4; //Let's step to next pointer entry, assuming pointer is 32 bit int
          ToFill.Add(new IntPtr(Result.VDXPlugin));
      }
   }

   return ToFill;
}


我认为这应该可以解决问题,从那里您可以检索指针的内容相同:


I think this should do the trick, and from there u could retrieve the contents of the pointers the same:

VDXPluginInfo GetMe = (VDXPluginInfo)Marshal.PtrToStructure(List[0], typeof(VDXPluginInfo)); //etc etc


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

08-21 19:59