private bool FindRockey4ND(int pos, ref int count, ref string OutPath) { int hardwareDeviceInfo; SP_INTERFACE_DEVICE_DATA DeviceInfoData = new SP_INTERFACE_DEVICE_DATA(); int i; GUID HidGuid = new GUID(); SP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = new SP_DEVICE_INTERFACE_DETAIL_DATA(); int requiredLength; int d_handle; HIDD_ATTRIBUTES Attributes = new HIDD_ATTRIBUTES(); i = 0; HidD_GetHidGuid(ref HidGuid); hardwareDeviceInfo = SetupDiGetClassDevsA(ref HidGuid, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (hardwareDeviceInfo == INVALID_HANDLE_VALUE) return false; DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); while (SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, ref HidGuid, i, ref DeviceInfoData)) { if (GetLastError() == ERROR_NO_MORE_ITEMS) break; functionClassDeviceData.cbSize = Marshal.SizeOf(functionClassDeviceData) - 255;// 5; requiredLength = 0; if (!SetupDiGetDeviceInterfaceDetailA(hardwareDeviceInfo, ref DeviceInfoData, ref functionClassDeviceData, 300, ref requiredLength, 0)) { SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); return false; } OutPath = ByteConvertString(functionClassDeviceData.DevicePath); d_handle = CreateFile(OutPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); if (INVALID_HANDLE_VALUE != d_handle) { if (HidD_GetAttributes(d_handle, ref Attributes)) { if ((Attributes.ProductID == PID) && (Attributes.VendorID == VID)) { if (pos == count) { SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); CloseHandle(d_handle); return true; } count = count + 1; } } CloseHandle(d_handle); } i = i + 1; } SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); return false; }
private static extern int SetupDiGetClassDevsA(ref GUID ClassGuid, int Enumerator, int hwndParent, int Flags);
private static extern bool SetupDiEnumDeviceInterfaces(int DeviceInfoSet, int DeviceInfoData, ref GUID InterfaceClassGuid, int MemberIndex, ref SP_INTERFACE_DEVICE_DATA DeviceInterfaceData);
private static extern int HidD_GetHidGuid(ref GUID HidGuid);