我正在尝试在我的 C# 代码中导入 C++ 函数。
该函数定义为:
int SetPointers(int* ID, int* BufferID, int** Pointer, double** Time, int NumberOfPointers);
ID 是一个 int 数组, BufferId 一个 int 数组, 指针为int数组, 对 double 组进行计时,并且 NumberOfPointers 一个整数。
我尝试使用 IntPtr 但没有成功。
这是我尝试过的最新代码:
[DllImport("open.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int SetPointers(int* ID, int* BufferID, ref IntPtr Pointer, ref IntPtr Time, int NumberOfPointers);
public unsafe int _SetPointers(int[] ID, int[] BufferID, ref int[] Pointer, ref double[] Time, int NumberOfPointers)
{
IntPtr fQueue = IntPtr.Zero;
IntPtr fTime = IntPtr.Zero;
int breturn = -1;
fixed (int* fId = ID)
fixed (int* fBufferID = BufferID)
fQueue = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * Pointer.Length);
fTime = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * Timestamp.Length);
breturn = SetPointers(fId, fBufferID, ref fQueue, ref fTime, NumberOfPointers);
return breturn;
}
我该怎么做?
最佳答案
首先,您可能希望使用 IntPtr 作为参数而不是 int[]。
此后,我还没有尝试过,但要在指针上编码指针,“ref IntPtr”或“out IntPtr”会起作用。
public unsafe int _SetPointers(IntPtr ID, IntPtr BufferID, ref IntPtr Pointer, ref IntPtr Time, int NumberOfPointers);
另请查看另一个问题: How do I marshall a pointer to a pointer of an array of structures?
关于c# - 如何导入带有 int** 和 double** 参数的 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20221747/