/// <summary> /// Requests to stop receiving notification messages when a device in an /// interface class is attached or removed. /// </summary> /// /// <param name="deviceNotificationHandle"> handle returned previously by /// RegisterDeviceNotification. </param> internal void StopReceivingDeviceNotifications(IntPtr deviceNotificationHandle) { try { // *** // API function // summary // Stop receiving notification messages. // parameters // Handle returned previously by RegisterDeviceNotification. // returns // True on success. // *** // Ignore failures. DeviceManagement.UnregisterDeviceNotification(deviceNotificationHandle); } catch (Exception ex) { throw; } }
/** Code taken from www.endurance-rc.com/software.php **/ private void FindHid() { if (hid.deviceDetected) return; DeviceManagement deviceManager = new DeviceManagement(); Boolean deviceFound = false; String[] devicePathName = new String[128]; Guid hidGuid = Guid.Empty; Int32 memberIndex = 0; Int32 myProductID = 0x1299; Int32 myVendorID = 0x0925; Boolean success = false; try { Hid.HidD_GetHidGuid(ref hidGuid); // Fill an array with the device path names of all attached HIDs. deviceFound = deviceManager.FindDeviceFromGuid(hidGuid, ref devicePathName); // If there is at least one HID, attempt to read the Vendor ID and Product ID // of each device until there is a match or all devices have been examined. if (deviceFound) { memberIndex = 0; do { hid.hidHandle = FileIO.CreateFile(devicePathName[memberIndex], 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); if (!hid.hidHandle.IsInvalid) { hid.DeviceAttributes.Size = Marshal.SizeOf(hid.DeviceAttributes); success = Hid.HidD_GetAttributes(hid.hidHandle, ref hid.DeviceAttributes); if (success) { // Find out if the device matches the one we're looking for. if ((hid.DeviceAttributes.VendorID == myVendorID) && (hid.DeviceAttributes.ProductID == myProductID)) { hid.deviceDetected = true; // Save the DevicePathName for OnDeviceChange(). hid.devicePathName = devicePathName[memberIndex]; } else { // It's not a match, so close the handle. hid.deviceDetected = false; hid.hidHandle.Close(); } } else { // There was a problem in retrieving the information. hid.deviceDetected = false; hid.hidHandle.Close(); } } // Keep looking until we find the device or there are no devices left to examine. memberIndex = memberIndex + 1; } while (!((hid.deviceDetected || (memberIndex == devicePathName.Length)))); } if (hid.deviceDetected) { // Learn the capabilities of the device. hid.Capabilities = hid.GetDeviceCapabilities(hid.hidHandle); // Get handles to use in requesting Input and Output reports. hid.readHandle = FileIO.CreateFile(hid.devicePathName, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0); hid.writeHandle = FileIO.CreateFile(hid.devicePathName, FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); // Flush any waiting reports in the input buffer. (optional) hid.FlushQueue(hid.readHandle); } return; } catch (Exception ex) { return; } }