P/Invoke 用于填充托管缓冲区的 C++ DLL

P/Invoke C++ DLL for filling a managed buffer

基本上,我有一个 DLL 和一个头文件 (C++),用于我用于研究的数据采集设备。该设备有 8 个传感器,每个传感器都有 x、y、z 等数据点。我想在 WPF 中为此设备创建一个 UI 程序。到目前为止,我已经能够通过不安全的代码实现与 .NET 中的设备成功通信,并将数据读入结构数组(其中每个结构对应一个传感器)。但是,我想看看我是否可以使用严格安全的代码实现来实现它,但我已经碰壁一段时间了。这是我第一次使用非托管代码与托管代码,所以请多多包涵。我在网上搜索了无数线程,但兔子洞越来越深,所以我想听听有经验的人的一些建议。基本上 API 头文件有一个定义如下的函数:

int GetSynchronousRecord(USHORT sensorID, void *pRecord, int recordSize);

本质上,我们通过引用向函数传递一个缓冲区,然后它会填满它。根据我传递的 sensorID 参数,我可以选择是获取单个传感器的数据,还是一次获取所有传感器的数据。到目前为止(在托管世界中)HAS 对我有用的是,如果我执行以下操作:

[StructLayout(LayoutKind.Sequential)]
public struct DOUBLE_POSITION_ANGLES_TIME_Q_RECORD
{
    public double x;     
    public double y; 
    public double z;  
    public double a; 
    public double e;    
    public double r;     
    public double time;
    public ushort quality;
};  

...

[DllImport("ATC3DG64.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetSynchronousRecord(ushort sensorID, ref DOUBLE_POSITION_ANGLES_TIME_Q_RECORD record, int recordSize);

...

DOUBLE_POSITION_ANGLES_TIME_Q_RECORD record = new DOUBLE_POSITION_ANGLES_TIME_Q_RECORD();
// Get the data from SENSOR_1 only
errorCode = GetSynchronousRecord(1, ref record, Marshal.SizeOf(record));

所以这个实现工作正常,我可以以非常快的速度获取所有坐标数据。但是,我想一次获得所有传感器。在 C++ API 代码示例中,它们向 GetSynchronousRecord 函数传递了一个结构数组,每个传感器一个结构。我尝试在 C# 中做同样的事情,如下所示:

[DllImport("ATC3DG64.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetSynchronousRecord(ushort sensorID, ref DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[] record, int recordSize);

// Define Array of Struct for each sensor
DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[] record = new DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[8];
while(recording) {
...
// Get data from ALL sensors (0xffff)
errorCode = GetSynchronousRecord(0xffff, ref record, Marshal.SizeOf(record)*8);
...
}

但这直接导致我的程序崩溃并出现 System.ExecutionEngineException 错误。我已经读到,因为我的函数需要一个 void* 指针,所以我应该使用 IntPtr 参数,但老实说,这种方法似乎很混乱。我尝试的另一件事是实际遍历每个传感器并调用传感器的函数,但这将速度疯狂地降低到几乎 1 record/second(而不是 100 records/second)。 stackexchange 上的许多其他类似线程说使用 out 参数,或在函数定义中使用 [In, Out] 属性,但这些建议中的 none 有效。

TL;DR: 如果我正确理解我的情况,我有一个 MANAGED 结构数组,我需要将其作为参数正确传递给 C++ 函数(通过引用传递), 然后该函数将用来自数据采集设备的数据填充我的结构。

对于text/code的墙,我深表歉意,非常感谢有经验的人为我提供信息。

编辑:澄清一下,GetSynchronousRecord 函数在一个 while 循环中,在每次迭代中我为每个结构获取新的数据点。

最好的方法可能是使用 IntPtr 而不是 ref DOUBLE_POSITION_ANGLES_TIME_Q_RECORD

我会将 P/Invoke 签名更改为:

[DllImport("ATC3DG64.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetSynchronousRecord(ushort sensorID, IntPtr record, int recordSize);

创建一个指向所需内存的 IntPtr space:

IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<DOUBLE_POSITION_ANGLES_TIME_Q_RECORD>() * 8);

调用P/Invoke函数(应该用结构填充内存):

errorCode = GetSynchronousRecord(0xffff, ptr, Marshal.SizeOf<DOUBLE_POSITION_ANGLES_TIME_Q_RECORD>() * 8);

从内存块中获取结构:

DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[] records = new DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[8];
for (i = 0; i < 8; i++) {
     records[i] = Marshal.PtrToStructure<DOUBLE_POSITION_ANGLES_TIME_Q_RECORD>(IntPtr.Add(ptr, i * Marshal.SizeOf<DOUBLE_POSITION_ANGLES_TIME_Q_RECORD>()));
}
Marshal.FreeHGlobal(ptr);

您的第二个 p/invoke 声明是错误的。你有

[DllImport("ATC3DG64.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetSynchronousRecord(
    ushort sensorID, 
    ref DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[] record, 
    int recordSize
);

问题是数组参数。因为您通过 ref 传递该数组,这实际上使它成为双指针。相反,您只想删除 ref 并像这样声明导入:

[DllImport("ATC3DG64.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetSynchronousRecord(
    ushort sensorID, 
    [Out] DOUBLE_POSITION_ANGLES_TIME_Q_RECORD[] record, 
    int recordSize
);

[Out]属性告诉封送拆收器数据正在流出函数。没有它,默认假设是数据流入。

当您调用该函数时,请这样做:

errorCode = GetSynchronousRecord(0xffff, record, Marshal.SizeOf(record)*record.Length);