DLLImport Int **-如果可以的话如何做 [英] DLLImport Int** - How to do this if it can be done

查看:94
本文介绍了DLLImport Int **-如果可以的话如何做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用希望将int **用作该方法的参数之一的第三方DLL.它将参数描述为指向内存分配的指针的地址.

I am trying to use a third party DLL that wants an int** as one of the parameters to the method. It describes the parameter as the address of the pointer that will point to the memory allocation.

很抱歉造成任何混乱.我认为该参数是双向的. DLL用于与FPGA板通信,方法是在主机PC和PCI板之间设置DMA传输.

Sorry for any confusion. The parameter is two-way I think. The DLL is for talking to an FPGA board and the method is setting up DMA transfer between the host PC and the PCI board.

推荐答案

使用by-ref System.IntPtr .

Use a by-ref System.IntPtr.

 [DllImport("thirdparty.dll")]
 static extern long ThirdPartyFunction(ref IntPtr arg);

 long f(int[] array)
  { long retval = 0;
    int  size   = Marshal.SizeOf(typeof(int));
    var  ptr    = IntPtr.Zero;

    try 
     { ptr = Marshal.AllocHGlobal(size * array.Length);

       for (int i= 0; i < array.Length; ++i) 
        { IntPtr tmpPtr = new IntPtr(ptr.ToInt64() + (i * size));
          Marshal.StructureToPtr(array, tmpPtr, false);
        }

       retval = ThirdPartyFunction(ref ptr);
     }
    finally 
     { if (ptr != IntPtr.Zero) Marshal.FreeHGlobal(ptr);
     }

    return retval;
  }

这篇关于DLLImport Int **-如果可以的话如何做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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