我正在尝试在我的C#代码中导入C++函数。
此函数定义为:
int SetPointers(int* ID, int* BufferID, int** Pointer, double** Time, int NumberOfPointers);
ID为int的数组
BufferId是一个int数组,
指向一个int数组,
计时两次
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/