I am trying to invoke a driver dll for a force sensor thats been written for c/cpp. The working Cpp code looks like this:
I the header file that was delivered with the dll, the struct is defined like this
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_DeviceInfostruct and a pointer to the first element and call the function Searchwith this pointer:
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:
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()?
[StructLayout(LayoutKind.Sequential)]to your structure.struct, how would that change anything?LayoutKind.Auto(docs), and my comment didn't mean to solve the question by itself.