我是c#的初学者,为了使用特定的DLL函数,我必须将c ++结构转换为c#。
这是C ++函数头:
CYWINEXPORT CY_RETURN_STATUS CyGetDeviceInfoVidPid (
CY_VID_PID vidPid, /*VID and PID of device of interest*/
UINT8 *deviceIdList, /*Array of device ID's returned*/
CY_DEVICE_INFO *deviceInfoList, /*Array of pointers to device info list*/
UINT8 *deviceCount, /*Count of devices with specified VID PID*/
UINT8 infoListLength /*Total length of the deviceInfoList allocated
(Size of deviceInfoList array)*/
);
这是所需的C ++结构:
typedef struct _CY_DEVICE_INFO {
CY_VID_PID vidPid; /*VID and PID*/
UCHAR numInterfaces; /*Number of interfaces supported*/
UCHAR manufacturerName [CY_STRING_DESCRIPTOR_SIZE]; /*Manufacturer name*/
UCHAR productName [CY_STRING_DESCRIPTOR_SIZE]; /*Product name*/
UCHAR serialNum [CY_STRING_DESCRIPTOR_SIZE]; /*Serial number*/
UCHAR deviceFriendlyName [CY_STRING_DESCRIPTOR_SIZE]; /*Device friendly name : Windows only*/
CY_DEVICE_TYPE deviceType [CY_MAX_DEVICE_INTERFACE]; /*Type of the device each interface has(Valid only
for USB Serial Device) and interface in vendor class*/
CY_DEVICE_CLASS deviceClass [CY_MAX_DEVICE_INTERFACE]; /*Interface class of each interface*/
CY_DEVICE_SERIAL_BLOCK deviceBlock; /* On Windows, each USB Serial device interface is associated with a
separate driver instance. This variable represents the present driver
interface instance that is associated with a serial block. */
} CY_DEVICE_INFO,*PCY_DEVICE_INFO;
typedef struct _CY_VID_PID {
UINT16 vid; /*Holds the VID of the device*/
UINT16 pid; /*Holds the PID of the device*/
} CY_VID_PID, *PCY_VID_PID;
这是我初始化结构和调用函数的方式:
cyReturnStatus = CyGetDeviceInfoVidPid (cyVidPid, deviceID, (PCY_DEVICE_INFO)&cyDeviceInfoList, &cyNumDevices, 16);
我试图用C#编写等效的代码。
这是我的函数头:
[DllImport("cyusbserial.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern enum_CY_Return_Status CyGetDeviceInfoVidPid(
Struct_CY_VID_PID vidPid, //VID and PID of device of interest
byte[] deviceIdList, //Array of device ID's returned
Struct_CY_DEVICE_INFO[] deviceInfoList, //Array of pointers to device info list
byte* deviceCount, //Count of devices with specified VID PID
byte infoListLength //Total length of the deviceInfoList allocated
(Size of deviceInfoList array)
);
这是我的C#结构:
public struct Struct_CY_VID_PID
{
public ushort vid; //Holds the VID of the device
public ushort pid; //Holds the PID of the device
}
public struct Struct_CY_DEVICE_INFO
{
public Struct_CY_VID_PID vidPid;
public byte numInterfaces;
public byte[] manufacturerName;
public byte[] productName; //Product name
public byte[] serialNum; //Serial number
public byte[] deviceFriendlyName; //Device friendly name : Windows only
public enum_CY_DEVICE_TYPE[] deviceType; //Type of the device each interface has(Valid only
//for USB Serial Device) and interface in vendor class
public enum_CY_DEVICE_CLASS[] deviceClass; //Interface class of each interface
public enum_CY_DEVICE_SERIAL_BLOCK deviceBlock; // On Windows, each USB Serial device interface is associated with a
separate driver instance. This variable represents the present driver
interface instance that is associated with a serial block.
}
这是我初始化结构和调用函数的方式:
for (int i = 0; i < 16; i++)
{
DeviceInfoList[i].manufacturerName = new byte[CY_STRING_DESCRIPTOR_SIZE];
DeviceInfoList[i].productName = new byte[CY_STRING_DESCRIPTOR_SIZE];
DeviceInfoList[i].serialNum = new byte[CY_STRING_DESCRIPTOR_SIZE];
DeviceInfoList[i].deviceFriendlyName = new byte[CY_STRING_DESCRIPTOR_SIZE];
DeviceInfoList[i].deviceType = new C_USBtoUART.enum_CY_DEVICE_TYPE[CY_MAX_DEVICE_INTERFACE];
DeviceInfoList[i].deviceClass = new C_USBtoUART.enum_CY_DEVICE_CLASS[CY_MAX_DEVICE_INTERFACE];
}
fixed (byte* pDevice_Count = &Device_Count)
{
Return_Status_1 = C_USBtoUART.CyGetDeviceInfoVidPid(VIDPID, DeviceID,DeviceInfoList, pDevice_Count, 16);
}
我认为我的错误来自于我如何发送“ DeviceInfoList”。我尝试使用“ ref”,但效果不佳。
谢谢您的帮助。