private bool SetDevice(DeviceControlFlags deviceControlFlag, ushort vendorid, ushort productid) { Guid myGuid = Guid.Empty; var deviceInfoData = new UsbApi.SpDevinfoData1 { CbSize = 28, DevInst = 0, ClassGuid = Guid.Empty, Reserved = 0 }; UInt32 i = 0; StringBuilder deviceName = new StringBuilder() { Capacity = UsbApi.MaxBufferSize }; //The SetupDiGetClassDevs function returns a handle to a device information set that contains requested device information elements for a local machine. IntPtr theDevInfo = UsbApi.SetupDiGetClassDevs(ref myGuid, 0, 0, UsbApi.DigcfAllclasses | UsbApi.DigcfPresent | UsbApi.DigcfProfile); for (; UsbApi.SetupDiEnumDeviceInfo(theDevInfo, i, deviceInfoData);) { if (UsbApi.SetupDiGetDeviceRegistryProperty(theDevInfo, deviceInfoData, (uint)UsbApi.Spdrp.SpdrpHardwareid, 0, deviceName, UsbApi.MaxBufferSize, IntPtr.Zero)) { if (deviceName.ToString().Contains(@"USB\Vid_") && deviceName.ToString().Contains(vendorid.ToString("x"))) { StateChange( deviceControlFlag == DeviceControlFlags.Disable ? UsbApi.DicsDisable : UsbApi.DicsEnable, (int)i, theDevInfo); UsbApi.SetupDiDestroyDeviceInfoList(theDevInfo); break; } } i++; } return(true); }
private void Initalise(uint index) { IntPtr ptr = IntPtr.Zero; IntPtr deviceInfoHandle = IntPtr.Zero; try { ptr = Marshal.AllocHGlobal(UsbApi.MaxBufferSize); deviceInfoHandle = UsbApi.SetupDiGetClassDevs(ref _guid, 0, IntPtr.Zero, UsbApi.DigcfPresent | UsbApi.DigcfDeviceinterface); // Create a device interface data structure UsbApi.SpDeviceInterfaceData deviceInterfaceData = new UsbApi.SpDeviceInterfaceData(); deviceInterfaceData.CbSize = Marshal.SizeOf(deviceInterfaceData); // Start the enumeration. Boolean success = UsbApi.SetupDiEnumDeviceInterfaces(deviceInfoHandle, IntPtr.Zero, ref _guid, (int)index, ref deviceInterfaceData); if (success) { // Build a DevInfo data structure. UsbApi.SpDevinfoData deviceInfoData = new UsbApi.SpDevinfoData(); deviceInfoData.CbSize = Marshal.SizeOf(deviceInfoData); // Build a device interface detail data structure. UsbApi.SpDeviceInterfaceDetailData deviceInterfaceDetailData = new UsbApi.SpDeviceInterfaceDetailData { CbSize = UIntPtr.Size == 8 ? 8 : (int)(4 + (uint)Marshal.SystemDefaultCharSize) }; // Now we can get some more detailed informations. int nRequiredSize = 0; int nBytes = UsbApi.MaxBufferSize; if (UsbApi.SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, ref deviceInterfaceData, ref deviceInterfaceDetailData, nBytes, ref nRequiredSize, ref deviceInfoData)) { DevicePath = deviceInterfaceDetailData.DevicePath; ParseDevicePath(DevicePath); // Get the device description and driver key name. int requiredSize = 0; int regType = UsbApi.RegSz; if (UsbApi.SetupDiGetDeviceRegistryProperty(deviceInfoHandle, ref deviceInfoData, (int)UsbApi.Spdrp.SpdrpDevicedesc, ref regType, ptr, UsbApi.MaxBufferSize, ref requiredSize)) { DeviceDescription = Marshal.PtrToStringAuto(ptr); } if (UsbApi.SetupDiGetDeviceRegistryProperty( deviceInfoHandle, ref deviceInfoData, (int)UsbApi.Spdrp.SpdrpDriver, ref regType, ptr, UsbApi.MaxBufferSize, ref requiredSize)) { DriverKey = Marshal.PtrToStringAuto(ptr); } } IntPtr hostControllerHandle = IntPtr.Zero; try { hostControllerHandle = KernelApi.CreateFile(deviceInterfaceDetailData.DevicePath, UsbApi.GenericWrite, UsbApi.FileShareWrite, IntPtr.Zero, UsbApi.OpenExisting, 0, IntPtr.Zero); if (deviceInfoHandle.ToInt64() != UsbApi.InvalidHandleValue) { GetHostControllerPowerMap(hostControllerHandle); GetHostControllerInfo(hostControllerHandle); } } finally { if (hostControllerHandle != IntPtr.Zero || deviceInfoHandle.ToInt64() != UsbApi.InvalidHandleValue) { KernelApi.CloseHandle(hostControllerHandle); } } try { Devices.Add(new UsbHub(null, DevicePath)); } catch (Exception ex) { CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbControllerSourceId, "Unhandled exception occurred: {0}", ex); throw new UsbControllerException("Unhandled exception occurred", ex); } } else { throw new UsbControllerException("No usb controller found!"); } } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } if (deviceInfoHandle != IntPtr.Zero) { UsbApi.SetupDiDestroyDeviceInfoList(deviceInfoHandle); } } }
private void GetComPort() { IntPtr regValue = IntPtr.Zero; IntPtr regKey = IntPtr.Zero; IntPtr handle = IntPtr.Zero; IntPtr ptr = IntPtr.Zero; try { handle = UsbApi.SetupDiGetClassDevs(0, UsbApi.RegstrKeyUsb, IntPtr.Zero, UsbApi.DigcfPresent | UsbApi.DigcfAllclasses); if (handle.ToInt64() != UsbApi.InvalidHandleValue) { ptr = Marshal.AllocHGlobal(UsbApi.MaxBufferSize); var success = true; for (var i = 0; success; i++) { var deviceInfoData = new UsbApi.SpDevinfoData(); deviceInfoData.CbSize = Marshal.SizeOf(deviceInfoData); success = UsbApi.SetupDiEnumDeviceInfo(handle, i, ref deviceInfoData); if (success) { var requiredSize = -1; var regType = UsbApi.RegSz; var driverKey = string.Empty; if (UsbApi.SetupDiGetDeviceRegistryProperty(handle, ref deviceInfoData, (int)UsbApi.Spdrp.SpdrpDriver, ref regType, ptr, UsbApi.MaxBufferSize, ref requiredSize)) { driverKey = Marshal.PtrToStringAuto(ptr); } if (DriverKey == driverKey) { regKey = UsbApi.SetupDiOpenDevRegKey(handle, ref deviceInfoData, UsbApi.DicsFlag.Global, 0, UsbApi.DiReg.Dev, (uint)WinNtApi.KeyRead); int size = 0; Advapi32.RegValueKind kind = Advapi32.RegValueKind.None; // Get the size of buffer we will need uint retVal = Advapi32.RegQueryValueEx(regKey, "PortName", 0, ref kind, IntPtr.Zero, ref size); if (size == 0) { break; } regValue = Marshal.AllocHGlobal(size); if (Advapi32.RegQueryValueEx(regKey, "PortName", 0, ref kind, regValue, ref size) == 0) { if (kind == Advapi32.RegValueKind.Sz) { _comPort = Marshal.PtrToStringAnsi(regValue); } } } } } } } finally { if (regValue != IntPtr.Zero) { Marshal.FreeHGlobal(regValue); } if (regKey != IntPtr.Zero) { Marshal.FreeHGlobal(regKey); } if (handle != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } if (handle != IntPtr.Zero) { UsbApi.SetupDiDestroyDeviceInfoList(handle); } } }