public static HIDDevice OpenDevice(string device_path, Type oType) { try { HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType); oNewDevice.Initialise(device_path); return(oNewDevice); } catch (Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); } }
/// <summary> /// Finds a device given its PID and VID /// </summary> /// <param name="nVid">Vendor id for device (VID)</param> /// <param name="nPid">Product id for device (PID)</param> /// <param name="oType">Type of device class to create</param> /// <returns>A new device class of the given type or null</returns> public static HIDDevice FindDevice(int nVid, int nPid, Type oType) { string strPath = string.Empty; string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); Guid gHid = HIDGuid; HidD_GetHidGuid(out gHid); IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); try { int nIndex = 0; DeviceInterfaceData oInterface = new DeviceInterfaceData(); oInterface.Size = Marshal.SizeOf(oInterface); while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) { string strDevicePath = GetDevicePath(hInfoSet, ref oInterface); if (strDevicePath.IndexOf(strSearch) >= 0) { HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType); oNewDevice.Initialise(strDevicePath); return(oNewDevice); } nIndex++; } } catch (Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); } finally { SetupDiDestroyDeviceInfoList(hInfoSet); } return(null); }