C# 调用C++问题
小枪 2016-11-22 03:36:24 不懂C++
有个东西需要用到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;
};
int EXPORT_API usb_relay_init(void);
struct usb_relay_device_info EXPORT_API * usb_relay_device_enumerate(void);
我改成C#程序如下
[DllImport("usb_relay_device")]
extern static int usb_relay_init();
[DllImport("usb_relay_device")]
extern static usb_relay_device_info 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
};
// public static usb_relay_device_info usb=new usb_relay_device_info();
/*usb relay board info structure*/
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct usb_relay_device_info
{
public byte[] serial_number;
public string device_path;
public usb_relay_device_type type;
public usb_relay_device_info[] next;
};
现测试init方法可以调试通 调试正确 但返回结构体方法 usb_relay_device_enumerate 返回异常
方法的类型签名与 pinvoke 不兼容
好费解 求大神解答