private void initDevice(string devicePath, bool useAsyncReads) { deviceConnected = false; //create file handles using CT_CreateFile handle_read = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); handle_write = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(handle_read, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(handle_read, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(handle_read, buffer, 126)) productName = Marshal.PtrToStringAuto(buffer); if (HidD_GetSerialNumberString(handle_read, buffer, 126)) SN = Marshal.PtrToStringAuto(buffer); if (HidD_GetManufacturerString(handle_read, buffer, 126)) manfString = Marshal.PtrToStringAuto(buffer); Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //SetupDiDestroyDeviceInfoList(i); if (handle_read.IsInvalid) return; deviceConnected = true; //If connection was sucsessful, record the values in a global struct productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.serialNumber = Convert.ToInt32(SN); productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; //use a filestream object to bring this stuff into .NET FS_read = new FileStream(handle_read, FileAccess.ReadWrite, capabilities.OutputReportByteLength, false); FS_write = new FileStream(handle_write, FileAccess.ReadWrite, capabilities.InputReportByteLength, false); this.useAsyncReads = useAsyncReads; if (useAsyncReads) readAsync(); }
static public extern int HidD_GetAttributes(int HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
public static extern bool HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
public static interfaceDetails[] getConnectedDevices() { interfaceDetails[] devices = new interfaceDetails[0]; //Create structs to hold interface information SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA(); SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA(); devInfo.cbSize = (uint)Marshal.SizeOf(devInfo); devIface.cbSize = (uint)(Marshal.SizeOf(devIface)); Guid G = new Guid(); HidD_GetHidGuid(ref G); //Get the guid of the HID device class IntPtr i = SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); //Loop through all available entries in the device list, until false SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); if (IntPtr.Size == 8) // for 64 bit operating systems didd.cbSize = 8; else didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems int j = -1; bool b = true; int error; SafeFileHandle tempHandle; while (b) { j++; b = SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref G, (uint)j, ref devIface); error = Marshal.GetLastWin32Error(); if (b == false) break; uint requiredSize = 0; bool b1 = SetupDiGetDeviceInterfaceDetail(i, ref devIface, ref didd, 256, out requiredSize, ref devInfo); string devicePath = didd.DevicePath; //create file handles using CT_CreateFile tempHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData); if (ppdSucsess == false) continue; HIDP_CAPS capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(tempHandle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(tempHandle, buffer, 126)) productName = Marshal.PtrToStringAuto(buffer); if (HidD_GetSerialNumberString(tempHandle, buffer, 126)) SN = Marshal.PtrToStringAuto(buffer); if (HidD_GetManufacturerString(tempHandle, buffer, 126)) manfString = Marshal.PtrToStringAuto(buffer); Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //If connection was sucsessful, record the values in a global struct interfaceDetails productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; if (stringIsInteger(SN)) productInfo.serialNumber = Convert.ToInt32(SN); //Check that serial number is actually a number int newSize = devices.Length + 1; Array.Resize(ref devices, newSize); devices[newSize - 1] = productInfo; } SetupDiDestroyDeviceInfoList(i); return devices; }
protected static extern bool HidD_GetAttributes( IntPtr hFile, // IN HANDLE HidDeviceObject, out HIDD_ATTRIBUTES attributes);
private void InitializeAttributes() { HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); attributes.Size = Marshal.SizeOf(attributes); if (HidD_GetAttributes(m_hHandle, out attributes)) { m_VendorId = attributes.VendorID; m_ProductId = attributes.ProductID; m_VersionNumber = attributes.VersionNumber; } else { throw HidDeviceException.GenerateWithWinError("GetAttributes failed"); } }
public static List<string> GetAllDevices(int? vendorId, int? productId) { int index = 0; GUID guid = new GUID(); var ret = new List<string>(); HidD_GetHidGuid(ref guid); IntPtr devicesHandle = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT); var diData = new SP_DEVICE_INTERFACE_DATA(); diData.cbSize = Marshal.SizeOf(diData); while (SetupDiEnumDeviceInterfaces(devicesHandle, IntPtr.Zero, ref guid, index, ref diData)) { //Get the buffer size UInt32 size; SetupDiGetDeviceInterfaceDetail(devicesHandle, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero); // Uh...yeah. var diDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA(); diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5); //Get detailed information if (SetupDiGetDeviceInterfaceDetail(devicesHandle, ref diData, ref diDetail, size, out size, IntPtr.Zero)) { //Get a handle to this device var handle = CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, EFileAttributes.Overlapped, IntPtr.Zero); //Get this device's attributes var attrib = new HIDD_ATTRIBUTES(); attrib.Size = Marshal.SizeOf(attrib); if (HidD_GetAttributes(handle.DangerousGetHandle(), ref attrib)) { //See if this is one we care about if ((!vendorId.HasValue || ((attrib.VendorID & 0xFFFF) == vendorId.Value)) && (!productId.HasValue || ((attrib.ProductID & 0xFFFF) == productId.Value))) { ret.Add(diDetail.DevicePath); break; } } //Close the handle handle.Close(); } //Move on index++; } SetupDiDestroyDeviceInfoList(devicesHandle); return ret; }
private static extern bool HidD_GetAttributes( IntPtr hidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
public static InterfaceDetails[] getConnectedDevices() { InterfaceDetails[] devices = new InterfaceDetails[0]; //Create structs to hold interface information SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA(); devInfo.cbSize = (uint)Marshal.SizeOf(devInfo); SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA(); devIface.cbSize = (uint)(Marshal.SizeOf(devIface)); Guid G = new Guid(); HID.HidD_GetHidGuid(ref G); //Get the guid of the HID device class IntPtr deviceInfo = SetupAPI.SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, SetupAPI.DIGCF_DEVICEINTERFACE | SetupAPI.DIGCF_PRESENT); //Loop through all available entries in the device list, until false int j = 0; while (true) { if (!SetupAPI.SetupDiEnumDeviceInterfaces(deviceInfo, IntPtr.Zero, ref G, (uint)j, ref devIface)) { break; } uint requiredSize = 0; IntPtr detailMemory = Marshal.AllocHGlobal((int)requiredSize); SP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = (SP_DEVICE_INTERFACE_DETAIL_DATA)Marshal.PtrToStructure(detailMemory, typeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); functionClassDeviceData.cbSize = Marshal.SizeOf(functionClassDeviceData); if (!SetupAPI.SetupDiGetDeviceInterfaceDetail(deviceInfo, ref devIface, ref functionClassDeviceData, requiredSize, out requiredSize, ref devInfo)) { Marshal.FreeHGlobal(detailMemory); break; } string devicePath = functionClassDeviceData.DevicePath; Marshal.FreeHGlobal(detailMemory); //create file handles using CT_CreateFile SafeFileHandle tempHandle = Kernel32.CreateFile(devicePath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HID.HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData); if (!ppdSucsess) { continue; } HIDP_CAPS capabilities = new HIDP_CAPS(); HID.HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); HID.HidD_GetAttributes(tempHandle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; const int bufferLen = 128; IntPtr buffer = Marshal.AllocHGlobal(bufferLen); if (HID.HidD_GetProductString(tempHandle, buffer, bufferLen)) { productName = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetSerialNumberString(tempHandle, buffer, bufferLen)) { SN = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetManufacturerString(tempHandle, buffer, bufferLen)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HID.HidD_FreePreparsedData(ref ptrToPreParsedData); //If connection was sucsessful, record the values in a global struct InterfaceDetails productInfo = new InterfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; productInfo.serialNumber = SN; //Check that serial number is actually a number int newSize = devices.Length + 1; Array.Resize(ref devices, newSize); devices[newSize - 1] = productInfo; ++j; } SetupAPI.SetupDiDestroyDeviceInfoList(deviceInfo); return(devices); }
/// <summary> /// Private constructor. /// </summary> /// <param name="handleToRawInputDevice"></param> private void Construct(IntPtr handleToRawInputDevice) { this.PreParsedData = IntPtr.Zero; this.inputButtonCapabilities = null; this.inputValueCapabilities = null; // Fetch various information defining the given HID device this.Name = RawInputHelper.GetDeviceName(handleToRawInputDevice); // Fetch device info this.info = new RID_DEVICE_INFO(); if (!RawInputHelper.GetDeviceInfo(handleToRawInputDevice, ref this.info)) { throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString()); } // Open our device from the device name/path var handle = Win32.Win32CreateFile.NativeMethods.CreateFile( this.Name, FileAccess.NONE, FileShare.FILE_SHARE_READ | FileShare.FILE_SHARE_WRITE, IntPtr.Zero, CreationDisposition.OPEN_EXISTING, FileFlagsAttributes.FILE_FLAG_OVERLAPPED, IntPtr.Zero); // Check if CreateFile worked if (handle.IsInvalid) { throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString()); } // Get manufacturer string var manufacturerString = new StringBuilder(256); if (Win32.Win32Hid.NativeMethods.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity)) { this.Manufacturer = manufacturerString.ToString(); } // Get product string StringBuilder productString = new StringBuilder(256); if (Win32.Win32Hid.NativeMethods.HidD_GetProductString(handle, productString, productString.Capacity)) { this.Product = productString.ToString(); } // Get attributes HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); if (Win32.Win32Hid.NativeMethods.HidD_GetAttributes(handle, ref attributes)) { this.VendorId = attributes.VendorID; this.ProductId = attributes.ProductID; this.Version = attributes.VersionNumber; } handle.Close(); this.SetFriendlyName(); // Get our HID descriptor pre-parsed data this.PreParsedData = RawInputHelper.GetPreParsedData(handleToRawInputDevice); if (this.PreParsedData == IntPtr.Zero) { // We are done then. Some devices don't have pre-parsed data. return; } // Get capabilities var status = Win32.Win32Hid.NativeMethods.HidP_GetCaps(this.PreParsedData, ref this.capabilities); if (status != HidStatus.HIDP_STATUS_SUCCESS) { throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString()); } this.SetInputCapabilitiesDescription(); // Get input button caps if needed if (this.Capabilities.NumberInputButtonCaps > 0) { this.inputButtonCapabilities = new HIDP_BUTTON_CAPS[this.Capabilities.NumberInputButtonCaps]; ushort buttonCapabilitiesLength = this.Capabilities.NumberInputButtonCaps; status = Win32.Win32Hid.NativeMethods.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, this.inputButtonCapabilities, ref buttonCapabilitiesLength, this.PreParsedData); if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != this.Capabilities.NumberInputButtonCaps) { throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString()); } this.ComputeButtonCount(); } // Get input value caps if needed if (this.Capabilities.NumberInputValueCaps > 0) { this.inputValueCapabilities = new HIDP_VALUE_CAPS[this.Capabilities.NumberInputValueCaps]; ushort valueCapabilitiesLength = this.Capabilities.NumberInputValueCaps; status = Win32.Win32Hid.NativeMethods.HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, this.inputValueCapabilities, ref valueCapabilitiesLength, this.PreParsedData); if (status != HidStatus.HIDP_STATUS_SUCCESS || valueCapabilitiesLength != this.Capabilities.NumberInputValueCaps) { throw new Exception("HidDevice: HidP_GetValueCaps failed: " + status.ToString()); } } }
private void initDevice(string devicePath) { deviceConnected = false; //create file handles using CT_CreateFile handle = Kernel32.CreateFile(devicePath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); HID.HidD_GetPreparsedData(handle, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); HID.HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); HID.HidD_GetAttributes(handle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HID.HidD_GetProductString(handle, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetSerialNumberString(handle, buffer, 126)) { SN = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetManufacturerString(handle, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HID.HidD_FreePreparsedData(ref ptrToPreParsedData); if (handle.IsInvalid) { return; } deviceConnected = true; //If connection was sucsessful, record the values in a global struct productInfo = new InterfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.serialNumber = SN; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; }
internal static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
public static List <HIDDeviceEntry> GetAllDevices(Guid guid, int?vendorId = null, int?productId = null) { int index = 0; //GUID guid = new GUID(); var devices = new List <HIDDeviceEntry>(); if (guid == Guid.Empty) { HidD_GetHidGuid(ref guid); } IntPtr devicesHandle = SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT); var diData = new SP_DEVICE_INTERFACE_DATA(); diData.cbSize = Marshal.SizeOf(diData); while (SetupDiEnumDeviceInterfaces(devicesHandle, IntPtr.Zero, ref guid, index, ref diData)) { //Get the buffer size UInt32 size; SetupDiGetDeviceInterfaceDetail(devicesHandle, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero); // Uh...yeah. var diDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA(); diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5); //Get detailed information if (SetupDiGetDeviceInterfaceDetail(devicesHandle, ref diData, ref diDetail, size, out size, IntPtr.Zero)) { //Get a handle to this device var handle = CreateFile(diDetail.DevicePath, 4 /*GENERIC_WRITE*/, 3 /*FILE_SHARE_READ | FILE_SHARE_WRITE*/, IntPtr.Zero, 4 /*OPEN_EXISTING*/, 0, IntPtr.Zero); if (handle.IsInvalid == false) { //Get this device's attributes var attrib = new HIDD_ATTRIBUTES(); attrib.Size = Marshal.SizeOf(attrib); if (HidD_GetAttributes(handle.DangerousGetHandle(), ref attrib)) { int vid = attrib.VendorID & 0xFFFF; int pid = attrib.ProductID & 0xFFFF; //See if this is one we care about if ((!vendorId.HasValue || vid == vendorId.Value) && (!productId.HasValue || pid == productId.Value)) { devices.Add(new HIDDeviceEntry(diDetail.DevicePath, vid, pid)); break; } } } //Close the handle handle.Close(); //CloseHandle(handle); } //Move on index++; } SetupDiDestroyDeviceInfoList(devicesHandle); return(devices); }
//---#+************************************************************************ //---NOTATION: //- int CT_HidD_GetAttributes(int hObject)) //- //--- DESCRIPTION: //-- Get a handle to the HID device // // Autor: F.L. //-*************************************************************************+#* public unsafe int CT_HidD_GetAttributes(int hObject) { // Create an instance of HIDD_ATTRIBUTES myHIDD_ATTRIBUTES = new HIDD_ATTRIBUTES(); // Calculate its size myHIDD_ATTRIBUTES.Size = sizeof(HIDD_ATTRIBUTES); return HidD_GetAttributes( hObject, ref myHIDD_ATTRIBUTES); }
public static DeviceInfo[] GetInterfaces() { var list = new List <DeviceInfo>(); var hidGuid = Guid.Empty; NativeMethods.HidD_GetHidGuid(ref hidGuid); var requiredSize3 = 0; // serialNumbers and physicalDescriptors for debug purposes only. var serialNumbers = new List <string>(); var physicalDescriptors = new List <string>(); _EnumDeviceInterfaces((deviceInfoSet, interfaceData) => { bool success; var deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.Initialize(); // Call 1: Retrieve data size. Note: Returns ERROR_INSUFFICIENT_BUFFER = 122, which is normal. success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, IntPtr.Zero, 0, ref requiredSize3, ref deviceInfoData); // Allocate memory for results. var ptrDetails = Marshal.AllocHGlobal(requiredSize3); Marshal.WriteInt32(ptrDetails, IntPtr.Size == 4 ? 4 + Marshal.SystemDefaultCharSize : 8); // Call 2: Retrieve data. success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, ptrDetails, requiredSize3, ref requiredSize3, ref deviceInfoData); var interfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA)Marshal.PtrToStructure(ptrDetails, typeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); var di = GetDeviceInfo(deviceInfoSet, deviceInfoData); di.DevicePath = interfaceDetail.DevicePath; Marshal.FreeHGlobal(ptrDetails); // Note: Interfaces don't have vendor or product, therefore must get from parent device. // Open the device as a file so that we can query it with HID and read/write to it. var devHandle = NativeMethods.CreateFile( interfaceDetail.DevicePath, 0, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, //WinNT.Overlapped IntPtr.Zero ); if (devHandle.IsInvalid) { return(true); } // Get vendor product and version from device. var ha = new HIDD_ATTRIBUTES(); ha.Size = Marshal.SizeOf(ha); var success2 = NativeMethods.HidD_GetAttributes(devHandle, ref ha); di.VendorId = ha.VendorID; di.ProductId = ha.ProductID; di.Revision = ha.VersionNumber; // Get other options. if (success2) { var preparsedDataPtr = new IntPtr(); var caps = new HIDP_CAPS(); // Read out the 'pre-parsed data'. NativeMethods.HidD_GetPreparsedData(devHandle, ref preparsedDataPtr); // feed that to GetCaps. NativeMethods.HidP_GetCaps(preparsedDataPtr, ref caps); // Free the 'pre-parsed data'. NativeMethods.HidD_FreePreparsedData(ref preparsedDataPtr); // This could fail if the device was recently attached. // Maximum string length is 126 wide characters (2 bytes each) (not including the terminating NULL character). var capacity = (uint)(126 * Marshal.SystemDefaultCharSize + 2); var sb = new StringBuilder((int)capacity, (int)capacity); // Override manufacturer if found. if (NativeMethods.HidD_GetManufacturerString(devHandle, sb, sb.Capacity) && sb.Length > 0) { di.Manufacturer = sb.ToString(); } // Override ProductName if Found. if (NativeMethods.HidD_GetProductString(devHandle, sb, sb.Capacity) && sb.Length > 0) { di.Description = sb.ToString(); } // Get Serial number. var serialNumber = NativeMethods.HidD_GetSerialNumberString(devHandle, sb, sb.Capacity) ? sb.ToString() : ""; serialNumbers.Add(serialNumber); // Get physical descriptor. var physicalDescriptor = NativeMethods.HidD_GetPhysicalDescriptor(devHandle, sb, sb.Capacity) ? sb.ToString() : ""; physicalDescriptors.Add(physicalDescriptor); } list.Add(di); devHandle.Close(); return(true); }); return(list.ToArray()); }
internal static extern Boolean HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
private void initDevice(string devicePath, bool useAsyncReads) { deviceConnected = false; //create file handles using CT_CreateFile handle_read = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); handle_write = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(handle_read, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(handle_read, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(handle_read, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HidD_GetSerialNumberString(handle_read, buffer, 126)) { SN = Marshal.PtrToStringAuto(buffer); } if (HidD_GetManufacturerString(handle_read, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //SetupDiDestroyDeviceInfoList(i); if (handle_read.IsInvalid) { return; } deviceConnected = true; //If connection was sucsessful, record the values in a global struct productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.serialNumber = SN; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; //use a filestream object to bring this stuff into .NET FS_read = new FileStream(handle_read, FileAccess.ReadWrite, capabilities.OutputReportByteLength, false); FS_write = new FileStream(handle_write, FileAccess.ReadWrite, capabilities.InputReportByteLength, false); this.useAsyncReads = useAsyncReads; if (useAsyncReads) { readAsync(); } }
public static extern int HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
/// <summary> /// Creates an object to handle read/write functionality for a USB HID device /// asnychronous read /// </summary> public static interfaceDetails[] getConnectedDevices() { interfaceDetails[] devices = new interfaceDetails[0]; //Create structs to hold interface information SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA(); SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA(); devInfo.cbSize = (uint)Marshal.SizeOf(devInfo); devIface.cbSize = (uint)(Marshal.SizeOf(devIface)); Guid G = new Guid(); HidD_GetHidGuid(ref G); //Get the guid of the HID device class IntPtr i = SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); //Loop through all available entries in the device list, until false SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); if (IntPtr.Size == 8) // for 64 bit operating systems { didd.cbSize = 8; } else { didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems } int j = -1; bool b = true; int error; SafeFileHandle tempHandle; while (b) { j++; b = SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref G, (uint)j, ref devIface); error = Marshal.GetLastWin32Error(); if (b == false) { break; } uint requiredSize = 0; bool b1 = SetupDiGetDeviceInterfaceDetail(i, ref devIface, ref didd, 256, out requiredSize, ref devInfo); string devicePath = didd.DevicePath; //create file handles using CT_CreateFile tempHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData); if (ppdSucsess == false) { continue; } HIDP_CAPS capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(tempHandle, ref attributes); string productName = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(tempHandle, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HidD_GetManufacturerString(tempHandle, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //If connection was sucsessful, record the values in a global struct interfaceDetails productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; productInfo.FEATURE_reportByteLength = (int)capabilities.FeatureReportByteLength; int newSize = devices.Length + 1; Array.Resize(ref devices, newSize); devices[newSize - 1] = productInfo; } SetupDiDestroyDeviceInfoList(i); return(devices); }
public static extern bool HidD_GetAttributes(IntPtr handle, ref HIDD_ATTRIBUTES attributes);
private static extern Boolean HidD_GetAttributes(SafeFileHandle hObject, ref HIDD_ATTRIBUTES Attributes);
public static List<HIDDeviceEntry> GetAllDevices(Guid guid, int? vendorId = null, int? productId = null) { int index = 0; //GUID guid = new GUID(); var devices = new List<HIDDeviceEntry>(); if (guid == Guid.Empty) { HidD_GetHidGuid(ref guid); } IntPtr devicesHandle = SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT); var diData = new SP_DEVICE_INTERFACE_DATA(); diData.cbSize = Marshal.SizeOf(diData); while (SetupDiEnumDeviceInterfaces(devicesHandle, IntPtr.Zero, ref guid, index, ref diData)) { //Get the buffer size UInt32 size; SetupDiGetDeviceInterfaceDetail(devicesHandle, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero); // Uh...yeah. var diDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA(); diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5); //Get detailed information if (SetupDiGetDeviceInterfaceDetail(devicesHandle, ref diData, ref diDetail, size, out size, IntPtr.Zero)) { //Get a handle to this device var handle = CreateFile(diDetail.DevicePath, 4 /*GENERIC_WRITE*/, 3 /*FILE_SHARE_READ | FILE_SHARE_WRITE*/, IntPtr.Zero, 4 /*OPEN_EXISTING*/, 0, IntPtr.Zero); if (handle.IsInvalid == false) { //Get this device's attributes var attrib = new HIDD_ATTRIBUTES(); attrib.Size = Marshal.SizeOf(attrib); if (HidD_GetAttributes(handle.DangerousGetHandle(), ref attrib)) { int vid = attrib.VendorID & 0xFFFF; int pid = attrib.ProductID & 0xFFFF; //See if this is one we care about if ((!vendorId.HasValue || vid == vendorId.Value) && (!productId.HasValue || pid == productId.Value)) { devices.Add(new HIDDeviceEntry(diDetail.DevicePath, vid, pid)); break; } } } //Close the handle handle.Close(); //CloseHandle(handle); } //Move on index++; } SetupDiDestroyDeviceInfoList(devicesHandle); return devices; }
public static DeviceInfo[] GetInterfaces(string[] devicePaths = null) { var list = new List <DeviceInfo>(); Guid hidGuid = Guid.Empty; NativeMethods.HidD_GetHidGuid(ref hidGuid); int requiredSize3 = 0; List <string> devicePathNames3 = new List <string>(); var interfaceData = new SP_DEVICE_INTERFACE_DATA(); List <string> serials = new List <string>(); interfaceData.Initialize(); var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(hidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF.DIGCF_DEVICEINTERFACE); for (int i2 = 0; NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref hidGuid, i2, ref interfaceData); i2++) { var deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.Initialize(); bool success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, IntPtr.Zero, 0, ref requiredSize3, IntPtr.Zero); IntPtr ptrDetails = Marshal.AllocHGlobal(requiredSize3); Marshal.WriteInt32(ptrDetails, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, ptrDetails, requiredSize3, ref requiredSize3, ref deviceInfoData); var interfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA)Marshal.PtrToStructure(ptrDetails, typeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); var devicePath = interfaceDetail.DevicePath; if (devicePaths != null && !devicePaths.Contains(devicePath)) { continue; } var deviceId = GetDeviceId(deviceInfoData.DevInst); devicePathNames3.Add(devicePath); Marshal.FreeHGlobal(ptrDetails); var accessRights = WinNT.GENERIC_READ | WinNT.GENERIC_WRITE; var shareModes = WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE; // Open the device as a file so that we can query it with HID and read/write to it. var devHandle = NativeMethods.CreateFile( interfaceDetail.DevicePath, accessRights, shareModes, IntPtr.Zero, WinNT.OPEN_EXISTING, WinNT.Overlapped, IntPtr.Zero ); if (devHandle.IsInvalid) { continue; } var ha = new HIDD_ATTRIBUTES(); ha.Size = Marshal.SizeOf(ha); var success2 = NativeMethods.HidD_GetAttributes(devHandle, ref ha); string serial = ""; string vendor = ""; string product = ""; string phdesc = ""; if (success2) { IntPtr preparsedDataPtr = new IntPtr(); HIDP_CAPS caps = new HIDP_CAPS(); // Read out the 'preparsed data'. NativeMethods.HidD_GetPreparsedData(devHandle, ref preparsedDataPtr); // feed that to GetCaps. NativeMethods.HidP_GetCaps(preparsedDataPtr, ref caps); // Free the 'preparsed data'. NativeMethods.HidD_FreePreparsedData(ref preparsedDataPtr); // This could fail if the device was recently attached. uint capacity = 126; IntPtr buffer = Marshal.AllocHGlobal((int)capacity); serial = NativeMethods.HidD_GetSerialNumberString(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; vendor = NativeMethods.HidD_GetManufacturerString(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; product = NativeMethods.HidD_GetProductString(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; phdesc = NativeMethods.HidD_GetPhysicalDescriptor(devHandle, buffer, capacity) ? Marshal.PtrToStringAuto(buffer) : ""; // Free resources. Marshal.FreeHGlobal(buffer); } uint parentDeviceInstance = 0; string parentDeviceId = null; var CRResult = NativeMethods.CM_Get_Parent(out parentDeviceInstance, deviceInfoData.DevInst, 0); if (CRResult == CR.CR_SUCCESS) { parentDeviceId = GetDeviceId(parentDeviceInstance); } var di = new DeviceInfo(deviceId, parentDeviceId, devicePath, vendor, product, hidGuid, "", DeviceNodeStatus.DN_MANUAL, ha.VendorID, ha.ProductID, ha.VersionNumber); list.Add(di); serials.Add(phdesc); devHandle.Close(); } NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); deviceInfoSet = IntPtr.Zero; return(list.ToArray()); }
static extern void HidD_GetAttributes(SafeFileHandle hidDeviceObject, out HIDD_ATTRIBUTES attributes);
public static DeviceInfo[] GetInterfaces() { var list = new List <DeviceInfo>(); Guid hidGuid = Guid.Empty; NativeMethods.HidD_GetHidGuid(ref hidGuid); int requiredSize3 = 0; var interfaceData = new SP_DEVICE_INTERFACE_DATA(); interfaceData.Initialize(); List <string> serials = new List <string>(); var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(hidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF.DIGCF_DEVICEINTERFACE); for (int i2 = 0; NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref hidGuid, i2, ref interfaceData); i2++) { var deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.Initialize(); bool success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, IntPtr.Zero, 0, ref requiredSize3, IntPtr.Zero); IntPtr ptrDetails = Marshal.AllocHGlobal(requiredSize3); Marshal.WriteInt32(ptrDetails, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); success = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, ptrDetails, requiredSize3, ref requiredSize3, ref deviceInfoData); var interfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA)Marshal.PtrToStructure(ptrDetails, typeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); var di = GetDeviceInfo(deviceInfoSet, deviceInfoData); di.DevicePath = interfaceDetail.DevicePath; var deviceHandle = deviceInfoData.DevInst; Marshal.FreeHGlobal(ptrDetails); // Open the device as a file so that we can query it with HID and read/write to it. var devHandle = NativeMethods.CreateFile( interfaceDetail.DevicePath, 0, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, //WinNT.Overlapped IntPtr.Zero ); if (devHandle.IsInvalid) { continue; } var ha = new HIDD_ATTRIBUTES(); ha.Size = Marshal.SizeOf(ha); var success2 = NativeMethods.HidD_GetAttributes(devHandle, ref ha); string serial = ""; string phdesc = ""; if (success2) { IntPtr preparsedDataPtr = new IntPtr(); HIDP_CAPS caps = new HIDP_CAPS(); // Read out the 'pre-parsed data'. NativeMethods.HidD_GetPreparsedData(devHandle, ref preparsedDataPtr); // feed that to GetCaps. NativeMethods.HidP_GetCaps(preparsedDataPtr, ref caps); // Free the 'pre-parsed data'. NativeMethods.HidD_FreePreparsedData(ref preparsedDataPtr); // This could fail if the device was recently attached. // Maximum string length is 126 wide characters (2 bytes each) (not including the terminating NULL character). ulong capacity = (uint)(126 * Marshal.SystemDefaultCharSize + 2); var sb = new StringBuilder((int)capacity, (int)capacity); // Override manufacturer if found. if (NativeMethods.HidD_GetManufacturerString(devHandle, sb, sb.Capacity) && sb.Length > 0) { di.Manufacturer = sb.ToString(); } // Override ProductName if Found. if (NativeMethods.HidD_GetProductString(devHandle, sb, sb.Capacity) && sb.Length > 0) { di.Description = sb.ToString(); } serial = NativeMethods.HidD_GetSerialNumberString(devHandle, sb, sb.Capacity) ? sb.ToString() : ""; phdesc = NativeMethods.HidD_GetPhysicalDescriptor(devHandle, sb, sb.Capacity) ? sb.ToString() : ""; } uint parentDeviceInstance = 0; string parentDeviceId = null; var CRResult = NativeMethods.CM_Get_Parent(out parentDeviceInstance, deviceHandle, 0); if (CRResult == CR.CR_SUCCESS) { parentDeviceId = GetDeviceId(parentDeviceInstance); } list.Add(di); serials.Add(phdesc); devHandle.Close(); } NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); deviceInfoSet = IntPtr.Zero; return(list.ToArray()); }
private static extern bool HidD_GetAttributes( SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes );
static internal extern bool HidD_GetAttributes(IntPtr hidDeviceObject, ref HIDD_ATTRIBUTES attributes);
// private HidDeviceInfo[] GetDeviceList(bool isFindAll, int vendorID, int productID) { //デバイス情報セットを取得する Guid hidGuid = new Guid(); HidD_GetHidGuid(ref hidGuid); IntPtr hDevInfoSet = SetupDiGetClassDevs( ref hidGuid, null, IntPtr.Zero, DIGCF.DIGCF_DEVICEINTERFACE | DIGCF.DIGCF_PRESENT); //各デバイス情報からデバイスのパス名を取得する SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData); int MemberIndex = 0; StringCollection scDevicePath = new StringCollection(); while (SetupDiEnumDeviceInterfaces(hDevInfoSet, IntPtr.Zero, ref hidGuid, MemberIndex++, ref DeviceInterfaceData)) { string devicePath = GetDevicePath(hDevInfoSet, ref DeviceInterfaceData); if (string.IsNullOrEmpty(devicePath) || scDevicePath.Contains(devicePath)) { continue; } scDevicePath.Add(devicePath); } //デバイス情報セットを破棄する SetupDiDestroyDeviceInfoList(hDevInfoSet); //目的のデバイスを検索する HIDD_ATTRIBUTES HiddAttributes = new HIDD_ATTRIBUTES(); HiddAttributes.Size = Marshal.SizeOf(HiddAttributes); ArrayList alHidDevInfos = new ArrayList(); foreach (string devicePath in scDevicePath) { //問い合わせモードでデバイスをオープンする SafeFileHandle hHidDev = DeviceAccess.OpenQueryMode(devicePath); if (hHidDev.IsInvalid) { continue; } //デバイスの属性を取得する if (HidD_GetAttributes(hHidDev, ref HiddAttributes)) { //目的のデバイスであれば追加情報を取得する if (isFindAll || (HiddAttributes.VendorID == vendorID && HiddAttributes.ProductID == productID)) { string devVenderName, devDeviceName, devSerialNumber; GetAdditionalInfo(hHidDev, out devVenderName, out devDeviceName, out devSerialNumber); HidDeviceInfo hidDevInfo = new HidDeviceInfo(devicePath, ref HiddAttributes, devVenderName, devDeviceName, devSerialNumber); alHidDevInfos.Add(hidDevInfo); } } //クローズ hHidDev.Close(); } //取得したデバイス情報を配列に格納する(長さ0以上の配列となる) HidDeviceInfo[] hidDevInfos = new HidDeviceInfo[alHidDevInfos.Count]; alHidDevInfos.CopyTo(hidDevInfos); return(hidDevInfos); }
private static extern int HidD_GetAttributes( int hObject, // IN HANDLE HidDeviceObject, ref HIDD_ATTRIBUTES Attributes); // OUT PHIDD_ATTRIBUTES Attributes
public static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
internal static extern bool HidD_GetAttributes( IntPtr hDevice, out HIDD_ATTRIBUTES Attributes );
public static bool Found() { Guid hidGuid; HidD_GetHidGuid(out hidGuid); IntPtr hardwareDeviceInfo = SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = (uint)Marshal.SizeOf(deviceInterfaceData); UInt32 i = 0; while (SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, IntPtr.Zero, ref hidGuid, i, ref deviceInterfaceData)) { SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); if (IntPtr.Size == 8) // for 64 bit operating systems { didd.cbSize = 8; } else { didd.cbSize = 4 + (uint)Marshal.SystemDefaultCharSize; } UInt32 bufferSize = 0; UInt32 requiredSize = 0; bool result; int error; SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData); result = SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, ref deviceInterfaceData, IntPtr.Zero, bufferSize, out requiredSize, out devInfoData); error = Marshal.GetLastWin32Error(); didd.DevicePath = new string(char.MinValue, 256); uint nBytes = (uint)didd.DevicePath.Length; result = SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, ref deviceInterfaceData, ref didd, nBytes, out requiredSize, out devInfoData); if (result) { file = CreateFile(didd.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); HIDD_ATTRIBUTES ha = new HIDD_ATTRIBUTES(); result = HidD_GetAttributes(file, ref ha); if (result) { if (ha.VendorID == HIDMINI_VAL & ha.ProductID == HIDMINI_VAL & ha.VersionNumber == HIDMINI_VAL) { IntPtr preparsedDataPointer = new System.IntPtr(); if (HidD_GetPreparsedData(file, ref preparsedDataPointer)) { HIDP_CAPS hidCaps = new HIDP_CAPS(); if (HidP_GetCaps(preparsedDataPointer, ref hidCaps)) { if (hidCaps.UsagePage == HIDMINI_USAGE_PAGE) { SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); return(true); } } } } } } i++; } SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); file = null; return(false); }