关于C# 调用 C++ 的DLL 的问题! 5 分!

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

};

/*usb relay board info structure*/
struct usb_relay_device_info
{
    unsigned char *serial_number;
    char *device_path;
    usb_relay_device_type type;
};
C++ 调用方法名称:
struct usb_relay_device_info EXPORT_API * usb_relay_device_enumerate(void);
============================================
C# 构造体代码:
public class Cstruct
{
   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  
};
   [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct usb_relay_device_info
 {
     public IntPtr serial_number;
     public IntPtr device_path;
     public usb_relay_device_type type;
            }
}
C# 调用C++ DLL 方法

             private void button1_Click(object sender, EventArgs e)
    {
         //查找设备
    [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern Cstruct.usb_relay_device_info usb_relay_device_enumerate();
        //查找设备
        Cstruct.usb_relay_device_info cs1 = usb_relay_device_enumerate();       
    }

问:1 我现在的问题是 cs1 取到的值都为0,不会报错
2是不是我C# 的构造体返回类型不应该是 IntPtr。因为在C++ 中是指针的形式,所以在C# 不知道如何定义

查看全部
hjw1197052444
hjw1197052444
2016/03/29 02:53
  • 继电器
  • c#
  • c++
  • 硬件
  • 软件
  • 点赞
  • 收藏
  • 回答
    私信

2个回复