using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace USBList { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto,Pack=4)] public class DEV_BROADCAST_DEVICEINTERFACE { public int dbcc_size; public int dbcc_devicetype; public int dbcc_reserved; public Guid dbcc_classguid; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string dbcc_name; // tchar[1] }; [StructLayout(LayoutKind.Sequential)] public class SP_DEVINFO_DATA { public int cbSize; public Guid ClassGuid; public int DevInst; // DEVINST handle public IntPtr Reserved; }; public struct SP_DEVICE_INTERFACE_DATA { public int cbSize; public Guid ClassGuid; public int DevInst; // DEVINST handle public IntPtr Reserved; } [StructLayout(LayoutKind.Sequential)] public struct SP_DRVINFO_DATA { public int cbSize; public int DriverType; public IntPtr Reserved; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string Description; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string MfgName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string ProviderName; public System.Runtime.InteropServices.ComTypes.FILETIME DriverDate; public long DriverVersion; } [StructLayout(LayoutKind.Sequential, Pack=4, CharSet = CharSet.Unicode)] public struct SP_DRVINFO_DETAIL_DATA { public int cbSize; public System.Runtime.InteropServices.ComTypes.FILETIME InfDate; public int CompatIDsOffset; public int CompatIDsLength; public ulong Reserved; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string SectionName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string InfFileName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string DrvDescription; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string HardwareID; } public class DeviceInformation { private const int DBT_DEVTYP_DEVICEINTERFACE = 5; private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000; private const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 0x00000004; public const int DIGCF_PRESENT = (0x00000002); public const int DIGCF_ALLCLASSES = (0x00000004); public const int DIGCF_PROFILE = (0x00000008); public const int DIGCF_DEVICEINTERFACE = (0x00000010); public const int MAX_DEV_LEN = 1000; public const int SPDRP_FRIENDLYNAME = (0x0000000C); // FriendlyName (R/W) public const int SPDRP_DEVICEDESC = (0x00000000); // DeviceDesc (R/W) public const int SPDRP_HARDWAREID = (0x00000001); // HardwareID (R/W) public const int SPDRP_DRIVER = (0x00000009); // Driver (R/W) public const int SPDRP_CLASS = (0x00000007); // Class (R--tied to ClassGUID) private IntPtr hDev; private IntPtr mDeviceNotifyHandle; [DllImport("setupapi.dll")]// public static extern Boolean SetupDiClassGuidsFromNameA(string ClassN, ref Guid guids, UInt32 ClassNameSize, ref UInt32 ReqSize); [DllImport("setupapi.dll")] public static extern IntPtr //result HDEVINFO SetupDiGetClassDevsA(ref Guid ClassGuid, StringBuilder Enumerator, IntPtr hwndParent, UInt32 Flags); [DllImport("setupapi.dll", SetLastError = true)] public static extern Boolean SetupDiOpenDeviceInterfaceA( IntPtr DeviceInfoSet, IntPtr DevicePath, int OpenFlags, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData); [DllImport("setupapi.dll")] public static extern Boolean SetupDiGetDeviceInterfaceDetailA( IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, ref SP_DEVICE_INTERFACE_DATA DeviceInfoData); [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern bool SetupDiGetDriverInfoDetail(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_DRVINFO_DATA DriverInfoData, ref SP_DRVINFO_DETAIL_DATA DriverInfoDetailData, int DriverInfoDetailDataSize, ref int RequiredSize); [DllImport("setupapi.dll")] public static extern Boolean SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex, SP_DEVINFO_DATA DeviceInfoData); [DllImport("setupapi.dll")] public static extern Boolean SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); [DllImport("setupapi.dll")] public static extern Boolean SetupDiGetDeviceRegistryPropertyA(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, UInt32 Property, UInt32 PropertyRegDataType, StringBuilder PropertyBuffer, UInt32 PropertyBufferSize, IntPtr RequiredSize); // HDEVNOTIFY RegisterDeviceNotification(HANDLE hRecipient,LPVOID NotificationFilter,DWORD Flags); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, uint Flags); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern uint UnregisterDeviceNotification(IntPtr hHandle); public bool DoRegisterDeviceInterface(IntPtr mHandle,Guid InterfaceClassGuid) { DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = new DEV_BROADCAST_DEVICEINTERFACE(); NotificationFilter.dbcc_size = Marshal.SizeOf(NotificationFilter); NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; NotificationFilter.dbcc_classguid = InterfaceClassGuid; IntPtr buffer = Marshal.AllocHGlobal(NotificationFilter.dbcc_size); Marshal.StructureToPtr(NotificationFilter, buffer, true); mDeviceNotifyHandle = RegisterDeviceNotification(mHandle, buffer, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); return true; } public bool DoUnRegisterDeviceInterface() { UnregisterDeviceNotification(mDeviceNotifyHandle); return true; } public bool GetUSBDevices() { Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); hDev = SetupDiGetClassDevsA( ref GUID_DEVINTERFACE_USB_DEVICE, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PROFILE ); if (hDev.ToInt32() == -1) return false; return true; } public SP_DEVINFO_DATA SetupDiGetDeviceInterfaceDetail(DEV_BROADCAST_DEVICEINTERFACE dvi) { SP_DEVICE_INTERFACE_DATA DeviceInfoData; // Lazy coding: Actually it's SP_DEVICE_INTERFACE_DATA structure, but they are equivalent. DeviceInfoData.cbSize = 28; DeviceInfoData.DevInst = 0; DeviceInfoData.ClassGuid = System.Guid.Empty; DeviceInfoData.Reserved = IntPtr.Zero; if (!SetupDiOpenDeviceInterfaceA( hDev, Marshal.StringToHGlobalAnsi(dvi.dbcc_name), //@"\\?\USB#VID_07AB&PID_FCCA#FW520_200000000000B703#{a5dcbf10-6530-11d2-901f-00c04fb951ed}", 0, ref DeviceInfoData)) { // Get the last error and display it. int error = Marshal.GetLastWin32Error(); SetupDiDestroyDeviceInfoList(hDev); return (SP_DEVINFO_DATA)null; } SP_DEVICE_INTERFACE_DATA dd; dd.cbSize = 28; dd.DevInst = 0; dd.ClassGuid = System.Guid.Empty; dd.Reserved = IntPtr.Zero; int val = 0; if (!SetupDiGetDeviceInterfaceDetailA( hDev, ref DeviceInfoData, IntPtr.Zero, 0, ref val, ref dd)) { int error = Marshal.GetLastWin32Error(); //if (error !=0) //{ // //no such device: // SetupDiDestroyDeviceInfoList(hDev); // return (SP_DEVINFO_DATA)null; //} } SP_DEVINFO_DATA retval = new SP_DEVINFO_DATA(); retval.cbSize = 28; retval.ClassGuid = dd.ClassGuid; retval.DevInst = dd.DevInst; retval.Reserved = dd.Reserved; return retval; } public bool GetAllDevice() { Guid guid = Guid.Empty; Guid[] guids = new Guid[1]; //get device info set for our device class hDev = SetupDiGetClassDevsA(ref guids[0], new StringBuilder("USB"), IntPtr.Zero, DIGCF_ALLCLASSES); //DIGCF_PRESENT if (hDev.ToInt32() != -1) return true; return false; } public bool GetInstalledDevice() { Guid guid = Guid.Empty; Guid[] guids = new Guid[1]; //get device info set for our device class hDev = SetupDiGetClassDevsA(ref guids[0], new StringBuilder("USB"), IntPtr.Zero, DIGCF_ALLCLASSES | DIGCF_PRESENT); //DIGCF_PRESENT if (hDev.ToInt32() != -1) return true; return false; } public SP_DEVINFO_DATA GetDeviceInfoData(uint dwIndex) { SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA(); DeviceInfoData.cbSize = 28; //is devices exist for class DeviceInfoData.DevInst = 0; DeviceInfoData.ClassGuid = System.Guid.Empty; DeviceInfoData.Reserved = IntPtr.Zero; if (!SetupDiEnumDeviceInfo(hDev, dwIndex, DeviceInfoData)) { //no such device: SetupDiDestroyDeviceInfoList(hDev); } return DeviceInfoData; } public string GetDescription( SP_DEVINFO_DATA DeviceInfoData) { StringBuilder DeviceName = new StringBuilder(); DeviceName.Capacity = MAX_DEV_LEN; if (SetupDiGetDeviceRegistryPropertyA(hDev, DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero) ) { return DeviceName.ToString(); } return string.Empty; } public string GetHardwareID(SP_DEVINFO_DATA DeviceInfoData) { StringBuilder DeviceName = new StringBuilder(); DeviceName.Capacity = MAX_DEV_LEN; if (SetupDiGetDeviceRegistryPropertyA(hDev, DeviceInfoData, SPDRP_HARDWAREID, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero) ) { return DeviceName.ToString(); } int dwRequireSize = 0; SP_DRVINFO_DATA DriverInfoData = new SP_DRVINFO_DATA(); DriverInfoData.cbSize = 800; SP_DRVINFO_DETAIL_DATA DriverInfoDetailData = new SP_DRVINFO_DETAIL_DATA(); DriverInfoDetailData.cbSize = 2084; if (!SetupDiGetDriverInfoDetail(hDev, ref DeviceInfoData, ref DriverInfoData, ref DriverInfoDetailData, 2048, ref dwRequireSize)) { int error = Marshal.GetLastWin32Error(); // string test = DriverInfoDetailData.HardwareID; } return string.Empty; } public string GetInfo(SP_DEVINFO_DATA DeviceInfoData, uint descID) { StringBuilder DeviceName = new StringBuilder(); DeviceName.Capacity = MAX_DEV_LEN; if (SetupDiGetDeviceRegistryPropertyA(hDev, DeviceInfoData, descID, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero) ) { return DeviceName.ToString(); } return string.Empty; } public string GetClassGuid(SP_DEVINFO_DATA DeviceInfoData) { StringBuilder DeviceName = new StringBuilder(); DeviceName.Capacity = MAX_DEV_LEN; if (SetupDiGetDeviceRegistryPropertyA(hDev, DeviceInfoData, SPDRP_DRIVER, //0x00000001D, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero) ) { return DeviceName.ToString(); } return string.Empty; } public string GetClass(SP_DEVINFO_DATA DeviceInfoData) { StringBuilder DeviceName = new StringBuilder(); DeviceName.Capacity = MAX_DEV_LEN; if (SetupDiGetDeviceRegistryPropertyA(hDev, DeviceInfoData, SPDRP_CLASS, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero) ) { return DeviceName.ToString(); } return string.Empty; } } }