我想操作USB设备,但 SP_DEVICE_INTERFACE_DETAIL_DATA不知道如何定义,它的定义跟函数SetupDiGetDeviceInterfaceDetail有关,我定义如下:
[StructLayout( LayoutKind.Sequential )]
public class SP_DEVICE_INTERFACE_DETAIL_DATA
{
public uint  cbSize;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 263 )] 
public string DevicePath;
};
函数定义如下:
[DllImport("setupapi.dll", SetLastError=true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr DeviceInfoSet,
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
uint DeviceInterfaceDetailDataSize,
            ref uint RequiredSize,
SP_DEVINFO_DATA DeviceInfoData);
函数是调用成功了,可是DeviceInterfaceDetailData.DevicePath 为 null 不知道为什么,请高手指点。

解决方案 »

  1.   

    SP_DEVICE_INTERFACE_DETAIL_DATA
    An SP_DEVICE_INTERFACE_DETAIL_DATA structure contains the path for a device interface.typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA {
      DWORD  cbSize;
      TCHAR  DevicePath[ANYSIZE_ARRAY];
    } SP_DEVICE_INTERFACE_DETAIL_DATA, *PSP_DEVICE_INTERFACE_DETAIL_DATA;
    Members
    cbSize 
    The size, in bytes, of the fixed portion of the SP_DEVICE_INTERFACE_DETAIL_DATA structure. 
    DevicePath 
    A NULL-terminated string that contains the device interface path. This path can be passed to Win32 functions such as CreateFile. Headers
    Defined in setupapi.h. Include setupapi.h.
      

  2.   

    算了,用最原始的方法了,用个IntPtr.所以定义就变成:
    [DllImport("setupapi.dll", SetLastError=true)]
    public static extern bool SetupDiGetDeviceInterfaceDetail(
    IntPtr DeviceInfoSet,
    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
    IntPtr DeviceInterfaceDetailData,
    //SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
    uint DeviceInterfaceDetailDataSize,
                ref uint RequiredSize,
    SP_DEVINFO_DATA DeviceInfoData);
    这样是可以,谢谢各位关注