我正在尝试在C#中使用C ++ DLL。
我只能使用一种功能。
这是C ++标头中的函数声明:
struct usb_relay_device_info EXPORT_API * usb_relay_device_enumerate(void);
这是C ++中的结构:
enum usb_relay_device_type
{
USB_RELAY_DEVICE_ONE_CHANNEL = 1,
USB_RELAY_DEVICE_TWO_CHANNEL = 2,
USB_RELAY_DEVICE_FOUR_CHANNEL = 4,
USB_RELAY_DEVICE_EIGHT_CHANNEL = 8
};
struct usb_relay_device_info
{
unsigned char *serial_number;
char *device_path;
usb_relay_device_type type;
usb_relay_device_info* next;
};
这就是我在C#中导入的方式:
[DllImport("usb_relay_device.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr usb_relay_device_enumerate();
public enum usb_relay_device_type
{
USB_RELAY_DEVICE_ONE_CHANNEL = 1,
USB_RELAY_DEVICE_TWO_CHANNEL = 2,
USB_RELAY_DEVICE_FOUR_CHANNEL = 4,
USB_RELAY_DEVICE_EIGHT_CHANNEL = 8,
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct usb_relay_device_info
{
/// unsigned char*
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string serial_number;
/// char*
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string device_path;
/// usb_relay_device_type
public usb_relay_device_type type;
/// usb_relay_device_info*
public System.IntPtr next;
}
使用如下:
IntPtr intPtr = usb_relay_device_enumerate();
,但始终返回0。
需要帮助以了解我在做什么错以及如何纠正它。
非常感谢。