/* get device path */ private string GetPath(IntPtr hInfoSet, ref DeviceInterfaceData iface) { /* detailed interface information */ var detIface = new DeviceInterfaceDetailData(); /* required size */ uint reqSize = (uint)Marshal.SizeOf(detIface); /* set size. The cbSize member always contains the size of the * fixed part of the data structure, not a size reflecting the * variable-length string at the end. */ /* now stay with me and look at that x64/x86 maddness! */ detIface.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5; /* get device path */ bool status = HIDNativeAPIs.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref iface, ref detIface, reqSize, ref reqSize, IntPtr.Zero); /* whops */ if (!status) { /* fail! */ throw new Win32Exception(); } /* return device path */ return(detIface.DevicePath); }
/* browse all HID class devices */ public static List <HIDInfo> BrowseHID() { /* hid device class guid */ Guid gHid; /* list of device information */ List <HIDInfo> info = new List <HIDInfo>(); /* obtain hid guid */ HIDNativeAPIs.HidD_GetHidGuid(out gHid); /* get list of present hid devices */ //var hInfoSet = GetClassDevs(gHid, (uint)DIGCF.DIGCF_PRESENT | (uint)DIGCF.DIGCF_DEVICEINTERFACE); var hInfoSet = GetClassDevs(Guid.Empty, (uint)DIGCF.DIGCF_ALLCLASSES | (uint)DIGCF.DIGCF_DEVICEINTERFACE); /* allocate mem for interface descriptor */ var iface = new DeviceInterfaceData(); /* set size field */ iface.Size = Marshal.SizeOf(iface); /* interface index */ uint index = 0; /* iterate through all interfaces */ while (HIDNativeAPIs.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface)) { bool isWork = false; HIDInfo hidInfo = new HIDInfo(hInfoSet, iface, out isWork); if (isWork) { info.Add(hidInfo); } /* next, please */ index++; } /* clean up */ if (HIDNativeAPIs.SetupDiDestroyDeviceInfoList(hInfoSet) == false) { /* fail! */ //throw new Win32Exception(); } /* return list */ return(info); }
public HIDInfo(IntPtr _hidInfoSet, DeviceInterfaceData _iface, out bool isWork) { hidInfoSet = _hidInfoSet; iface = _iface; //Get HW Path string devPath = GetPath(hidInfoSet, ref iface); //Open HID HW hidHWDev = new HIDHWDev(devPath); isWork = false; if (hidHWDev.OpenAsync()) { hidInfoStruct = new HIDInfoStruct() { Manufacturer = GetManufacturer(hidHWDev.HIDHandel), Product = GetProduct(hidHWDev.HIDHandel), SerialNumber = GetSerialNumber(hidHWDev.HIDHandel), HIDFullPath = devPath, }; hidInfoStruct.HIDCompareStr = devPath.Split('&')[2]; HiddAttributtes hidAttr = new HiddAttributtes(); try { hidAttr = GetVidPid(hidHWDev.HIDHandel); } catch (Exception ex) { Utilities.Logger(HIDAPIs.LogHIDHWDev, $"GetVidPid Error {ex.Message}"); } hidInfoStruct.Vid = hidAttr.VendorID; hidInfoStruct.Pid = hidAttr.ProductID; hidInfoStruct.OutputBuffSize = hidHWDev.OutputBuffSize; hidInfoStruct.InputBuffSize = hidHWDev.InputBuffSize; isWork = true; } }
public static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, IntPtr lpDeviceInfoData);
public static extern bool SetupDiEnumDeviceInterfaces( IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref DeviceInterfaceData oInterfaceData);