我发现了一些关于类似问题的帖子,但仍然无法解决我的问题。我有一个dll,我试图使用PInvoke包装.Net并且我要么得到不平衡的堆栈或受保护的内存错误。我正在尝试执行的函数具有以下签名:
niUSRP_API_Function niUSRP_FindDevices(int32_t devicesArraySize, niUSRP_Device* devices, int32_t* numberOfDevicesInSystem);
typedef int32_t niUSRP_Status;
typedef struct niUSRP_Device_struct
{
char address[16];
char serialNumber[16];
char model[36];
} niUSRP_Device;
#define niUSRP_API_Function __declspec(dllexport) niUSRP_Status __stdcall
这些是我尝试的相应C#签名:
[DllImport("niusrp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "niUSRP_FindDevices")]
public static extern int FindDevices(int devicesArraySize, Device[] devices, ref int devicesFound);
[DllImport("niusrp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "niUSRP_FindDevices")]
public static extern int FindDevices(int devicesArraySize, ref Device[] devices, ref int devicesFound);
[DllImport("niusrp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "niUSRP_FindDevices")]
public static extern int FindDevices(int devicesArraySize, ref Device devices, ref int devicesFound);
[DllImport("niusrp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "niUSRP_FindDevices")]
public static extern int FindDevices(int devicesArraySize, IntPtr devices, ref int devicesFound);
[DllImport("niusrp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "niUSRP_FindDevices")]
public static extern void FindDevices(int devicesArraySize, IntPtr devices, ref int devicesFound);
[StructLayout(LayoutKind.Sequential, Size = 68, CharSet = CharSet.Ansi)]
public struct Device
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string Address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string SerialNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
public string Model;
}
我尝试过的其他几件事
感谢任何帮助。