DLLImport Int** - How to do this if it can be done

狂风中的少年 提交于 2020-01-01 10:33:36

问题


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.

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.


回答1:


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;
  }



回答2:


You will have to make use of the Marshal class or go unsafe in this case.

It could also just be a pointer to an array, so a ref int[] list might work.




回答3:


An int** would be and array of IntPtr, from your description I think you might want to look into using C++/CLI to help you with the conversions e.g from an unmanaged int** to a managed array^>^



来源:https://stackoverflow.com/questions/209258/dllimport-int-how-to-do-this-if-it-can-be-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!