internal static string GetDevicePath(string device) { Main.SendDebug(string.Format("Getting Drive path for Device: {0}", device)); var deviceNumber = GetDeviceNumber(device); var diskGUID = new Guid("53F56307-B6BF-11D0-94F2-00A0C91EFB8B"); var handle = SetupDiGetClassDevs(ref diskGUID, IntPtr.Zero, IntPtr.Zero, (uint) (DIGCF.DIGCFPresent | DIGCF.DIGCFDeviceinterface)); try { if(handle != new IntPtr(-1)) { var success = true; uint i = 0; while(success) { var devinterfacedata = new SpDeviceInterfaceData(); devinterfacedata.cbSize = (uint) Marshal.SizeOf(devinterfacedata); success = SetupDiEnumDeviceInterfaces(handle, IntPtr.Zero, ref diskGUID, i, ref devinterfacedata); if(!success) continue; var devinfodata = new SpDevinfoData(); devinfodata.cbSize = (uint) Marshal.SizeOf(devinfodata); var devinterfacedetaildata = new SpDeviceInterfaceDetailData { cbSize = IntPtr.Size == 8 ? 8 : (uint) (4 + Marshal.SystemDefaultCharSize) }; uint nRequiredSize = 0; if(!SetupDiGetDeviceInterfaceDetail(handle, ref devinterfacedata, ref devinterfacedetaildata, 1000, ref nRequiredSize, ref devinfodata)) SetupDiGetDeviceInterfaceDetail(handle, ref devinterfacedata, ref devinterfacedetaildata, nRequiredSize, ref nRequiredSize, ref devinfodata); var devnum = GetDeviceNumber(devinterfacedetaildata.DevicePath); if(devnum == deviceNumber) { SetupDiDestroyDeviceInfoList(handle); return devinterfacedetaildata.DevicePath; } i++; } throw new X360NANDManagerException(X360NANDManagerException.ErrorLevels.NoDeviceFound); } } finally { SetupDiDestroyDeviceInfoList(handle); } throw new Win32Exception(Marshal.GetLastWin32Error()); }
public static extern Boolean SetupDiGetDeviceInterfaceDetail( IntPtr hDevInfo, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, out UInt32 requiredSize, ref SpDevinfoData deviceInfoData );
public static extern bool SetupDiEnumDeviceInfo( IntPtr hDevInfo, UInt32 memberIndex, ref SpDevinfoData devInfo);
private static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, ref SpPropchangeParams classInstallParams, int classInstallParamsSize);
static internal extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, ref Guid interfaceClassGuid, int memberIndex, ref SpDeviceInterfaceData deviceInterfaceData);
private static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, UInt32 property, ref UInt32 propertyRegDataType, IntPtr propertyBuffer, UInt32 propertyBufferSize, ref UInt32 requiredSize);
static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SpDevinfoData deviceInfoData);
internal static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SpDevinfoData deviceInfoData);
static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, ref SpDevinfoData deviceInfoData);
static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, int iProperty, ref int propertyRegDataType, IntPtr propertyBuffer, int propertyBufferSize, ref int requiredSize);
/// <summary> /// Return a list of USB Host Controllers /// </summary> /// <returns>List of USB Host Controllers</returns> static IEnumerable <UsbController> GetHostControllers() { List <UsbController> hostList = new List <UsbController>(); Guid hostGuid = new Guid(GUID_DEVINTERFACE_HUBCONTROLLER); // We start at the "root" of the device tree and look for all // devices that match the interface GUID of a Hub Controller IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (h == INVALID_HANDLE_VALUE) { return(new ReadOnlyCollection <UsbController>(hostList)); } IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); bool success; int i = 0; do { UsbController host = new UsbController { ControllerIndex = i }; // create a Device Interface Data structure SpDeviceInterfaceData dia = new SpDeviceInterfaceData(); dia.cbSize = Marshal.SizeOf(dia); // start the enumeration success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGuid, i, ref dia); if (success) { // build a DevInfo Data structure SpDevinfoData da = new SpDevinfoData(); da.cbSize = Marshal.SizeOf(da); // build a Device Interface Detail Data structure SpDeviceInterfaceDetailData didd = new SpDeviceInterfaceDetailData { cbSize = 4 + Marshal.SystemDefaultCharSize }; // trust me :) // now we can get some more detailed information int nRequiredSize = 0; const int N_BYTES = BUFFER_SIZE; if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, N_BYTES, ref nRequiredSize, ref da)) { host.ControllerDevicePath = didd.DevicePath; // get the Device Description and DriverKeyName int requiredSize = 0; int regType = REG_SZ; if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf, BUFFER_SIZE, ref requiredSize)) { host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf); } if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE, ref requiredSize)) { host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf); } } hostList.Add(host); } i++; }while(success); Marshal.FreeHGlobal(ptrBuf); SetupDiDestroyDeviceInfoList(h); // convert it into a Collection return(new ReadOnlyCollection <UsbController>(hostList)); }
static extern bool SetupDiGetDeviceInstanceId(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, StringBuilder deviceInstanceId, int deviceInstanceIdSize, out int requiredSize);
/// <summary> /// Find a device based upon a Device Number /// </summary> /// <param name="devNum">Device Number</param> /// <param name="deviceGuid">Device GUID</param> /// <returns>USB device</returns> static UsbDevice FindDeviceNumber(int devNum, string deviceGuid) { UsbDevice foundDevice = null; string instanceId = ""; Guid diskGuid = new Guid(deviceGuid); // We start at the "root" of the device tree and look for all // devices that match the interface GUID of a disk IntPtr h = SetupDiGetClassDevs(ref diskGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (h != INVALID_HANDLE_VALUE) { bool success; int i = 0; do { // create a Device Interface Data structure SpDeviceInterfaceData dia = new SpDeviceInterfaceData(); dia.cbSize = Marshal.SizeOf(dia); // start the enumeration success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref diskGuid, i, ref dia); if (success) { // build a DevInfo Data structure SpDevinfoData da = new SpDevinfoData(); da.cbSize = Marshal.SizeOf(da); // build a Device Interface Detail Data structure SpDeviceInterfaceDetailData didd = new SpDeviceInterfaceDetailData { cbSize = 4 + Marshal.SystemDefaultCharSize }; // trust me :) // now we can get some more detailed information int nRequiredSize = 0; const int N_BYTES = BUFFER_SIZE; if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, N_BYTES, ref nRequiredSize, ref da)) { if (GetDeviceNumber(didd.DevicePath) == devNum) { // current InstanceID is at the "USBSTOR" level, so we // need up "move up" one level to get to the "USB" level CM_Get_Parent(out IntPtr ptrPrevious, da.DevInst, 0); // Now we get the InstanceID of the USB level device IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(N_BYTES); CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, N_BYTES, 0); instanceId = Marshal.PtrToStringAuto(ptrInstanceBuf); Marshal.FreeHGlobal(ptrInstanceBuf); //System.Console.WriteLine("InstanceId: {0}", instanceId); //break; } } } i++; }while(success); SetupDiDestroyDeviceInfoList(h); } // Did we find an InterfaceID of a USB device? if (instanceId?.StartsWith("USB\\", StringComparison.Ordinal) == true) { foundDevice = FindDeviceByInstanceId(instanceId); } return(foundDevice); }
public static extern bool SetupDiGetDeviceProperty(IntPtr deviceInfo, ref SpDevinfoData deviceInfoData, ref Devpropkey propkey, ref ulong propertyDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize, uint flags);
internal static bool IsDeviceConnected(int vendorID, int productID) { var handle = SetupDiGetClassDevs(IntPtr.Zero, "USB", IntPtr.Zero, (int) (DIGCF.DIGCFPresent | DIGCF.DIGCFAllclasses)); try { if(handle == new IntPtr(-1)) throw new Win32Exception(Marshal.GetLastWin32Error()); var success = true; uint i = 0; while(success) { var dia = new SpDevinfoData(); dia.cbSize = (uint) Marshal.SizeOf(dia); success = SetupDiEnumDeviceInfo(handle, i, ref dia); if(success) { uint requiredSize = 0; uint regType = 0; SetupDiGetDeviceRegistryProperty(handle, ref dia, 1, ref regType, IntPtr.Zero, 0, ref requiredSize); var err = Marshal.GetLastWin32Error(); if(err != 0x7A) throw new Win32Exception(err); // Expected error didn't occur, something went wrong (we want to know how big buffer we need) var intPtrBuffer = Marshal.AllocHGlobal((int) requiredSize); if(!SetupDiGetDeviceRegistryProperty(handle, ref dia, 1, ref regType, intPtrBuffer, requiredSize, ref requiredSize)) throw new Win32Exception(Marshal.GetLastWin32Error()); // UHOH! Something went horribly wrong!! var hardwareID = Marshal.PtrToStringAuto(intPtrBuffer); Marshal.FreeHGlobal(intPtrBuffer); if(hardwareID == null) throw new Exception("hardwareID is null!"); hardwareID = hardwareID.ToUpper(); if(hardwareID.Contains("VID_" + vendorID.ToString("X04")) && hardwareID.Contains("PID_" + productID.ToString("X04"))) return true; //W00t job well done! we found it!!! :D } i++; } return false; } finally { SetupDiDestroyDeviceInfoList(handle); } }
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, int propertyVal, ref int propertyRegDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize);
private static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, UInt32 memberIndex, ref SpDevinfoData deviceInfoData);
internal static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, ref Guid interfaceClassGuid, int memberIndex, ref SpDeviceInterfaceData deviceInterfaceData);