简体   繁体   English

编组结构数组的指针

[英]Marshalling a Pointer to Array of Structs

I am trying to invoke a driver dll for a force sensor thats been written for c/cpp. 我正在尝试为为c / cpp编写的力传感器调用驱动程序dll。 The working Cpp code looks like this: 可用的Cpp代码如下所示:

I the header file that was delivered with the dll, the struct is defined like this 我随dll一起提供的头文件,结构定义如下

typedef struct
{
  DWORD usb_hid_idx;
  int open;
  char vid_pid[256];
  char dev_info[256];
  char sn_info[256];
  int hw_info;
  unsigned char hw_var;
  int fw_vers;
} t_DeviceInfo;

And the function I need to call is defined like this: 我需要调用的函数定义如下:

extern "C" DLL_API int Search(t_DeviceInfo *p_dev_info);

In the main code I just create an array of the former defined t_DeviceInfo struct and a pointer to the first element and call the function Search with this pointer: 在主代码中,我只创建了一个先前定义的t_DeviceInfo结构的数组和一个指向第一个元素的指针,并使用此指针调用函数Search

t_DeviceInfo deviceInfo[16];
t_DeviceInfo* deviceInfoPtr = &deviceInfo[0];
int ret = search(deviceInfoPtr);

At least there, everything works fine. 至少在那里,一切正常。 With C# it currently looks like this: 使用C#,当前看起来像这样:

unsafe public struct t_DeviceInfo
{
    long usb_hid_idx;
    int open;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    char[] vid_pid;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    char[] dev_info;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    char[] sn_info;
    int hw_info;
    char hw_var;
    int fw_vers;
}

unsafe public class ASTAS
{
    [DllImport("ASTAS_DLL.dll")]
    public extern static int Search(t_DeviceInfo* devInfPtr);
}

And in the main: 在主要方面:

t_DeviceInfo[] devInfo = new t_DeviceInfo[16];

But thats about it. 就是这样。 How do I marshal the array of structs to a fixed memory location and pass the corresponding pointer to Search() ? 如何将结构数组编组到固定的内存位置,并将相应的指针传递给Search()

There's no need for unsafe here. 这里不需要unsafe地方。 You may as well remove that. 您也可以删除它。 And your struct declaration is incorrect. 并且您的结构声明不正确。 Some of the types are wrong. 有些类型是错误的。 An C++ DWORD is a unsigned 32 bit value. C ++ DWORD是无符号的32位值。 AC# long is a signed 64 bit value. AC# long是一个带符号的64位值。 A C++ unsigned char is an unsigned 8 bit value. C ++ unsigned char是无符号的8位值。 AC# char is a 16 bit character. AC#char是一个16位字符。 Typically unsigned char maps to byte . 通常, unsigned char映射到byte

The struct should be: 该结构应为:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct t_DeviceInfo
{
    uint usb_hid_idx;
    int open;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    string vid_pid;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    string dev_info;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    string sn_info;
    int hw_info;
    byte hw_var;
    int fw_vers;
}

The function declaration should then be: 函数声明应为:

[DllImport("ASTAS_DLL.dll")]
public extern static int Search([In] t_DeviceInfo[] devInfPtr);

This assumes that the calling convention is stdcall. 假定调用约定为stdcall。 It looks to me as though it is actually cdecl. 在我看来,它实际上是cdecl。 In which case you need: 在这种情况下,您需要:

[DllImport("ASTAS_DLL.dll", CallingConvention=CallingConvention.Cdecl)]
public extern static int Search([In] t_DeviceInfo[] devInf);

I've used [In] marshalling. 我用过[In]编组[In] You may need to modify that depending on the semantics, which I cannot discern from the information provided. 您可能需要根据语义进行修改,我无法从提供的信息中辨别出该语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM