/// <summary> /// This method will filter the messages that are passed for USB device change messages only. /// And parse them and take the appropriate action /// </summary> /// <param name="msg"> /// The Message. /// </param> /// <param name="windowParam"> /// The w parameter. /// </param> /// <param name="pointerParam"> /// The pointer parameter. /// </param> public static void ParseMessages(int msg, IntPtr windowParam, IntPtr pointerParam) { // we got a device change message! A USB device was inserted or removed if (!msg.Equals(NativeMethods.WindowsMessageDeviceChange) || UsbChangedEvent == null || windowParam == IntPtr.Zero || pointerParam == IntPtr.Zero) { return; } var di = (NativeMethods.DevBroadcastDeviceInterface) Marshal.PtrToStructure(pointerParam, typeof(NativeMethods.DevBroadcastDeviceInterface)); if (string.IsNullOrEmpty(di.Path)) { return; } if (di.ClassGuid.Equals(NativeMethods.WinUsbGuid)) { // WinUSB Device. // force the information to always be non null. CurrentState.Information = UsbHelpers.GetWinUsbAddresses(di.Path, UsbHelpers.FilterTypes.DeviceName) .FirstOrDefault() .Information ?? new HidInformation(); } else { // Non WinUSB device. // force the information to always be non null. CurrentState.Information = UsbHelpers.GetHidAddresses(di.Path, UsbHelpers.FilterTypes.DeviceName).FirstOrDefault().Information ?? new HidInformation(); } CurrentState.Path = di.Path; // Check the window parameter to see if a device was inserted or removed switch (windowParam.ToInt64()) { // inserted case NativeMethods.DeviceArrival: CurrentState.State = UsbStates.Arrived; break; // removed case NativeMethods.DeviceRemoveComplete: CurrentState.State = UsbStates.Removed; break; default: return; } UsbChangedEvent.RaiseEvent(null, CurrentState); }
public static void Main() { const short VendorId = 0x45e; const byte TackpadUsage = 0x05; const byte MaxContactFeatureReportId = 0x05; const string HostAddress = "localhost"; // find the Hid device path to the Precision Trackpad. var trackpadPath = UsbHelpers.GetHidAddresses(VendorId, UsbHelpers.FilterTypes.VendorId) .FirstOrDefault(x => x.Information.Usage == TackpadUsage).DevicePath; if (string.IsNullOrEmpty(trackpadPath)) { Console.WriteLine("Cannot find PTP Trackpad."); return; } // create our buffers to store the repsonses from the feature calls. byte[] featureHid; byte[] featureNet = { 1, 2, 3 }; using (var hid = new HidTransportBase(trackpadPath)) { if (!hid.Connect()) { Console.WriteLine("Failed to connect to PTP device."); return; } // get the feature report Over the hid transport. featureHid = hid.GetFeature(MaxContactFeatureReportId); // initialize and start our remote parser. // this allows remote communications from other clients to // our hid transport. var remote = new Remote(hid); remote.Start(); // initalize our network transport. using (var network = new NetworkTransport(HostAddress, Remote.PortNumber)) { // connect to our runnign remote parser. if (network.Connect()) { // get the feature report Over the network transport. featureNet = network.GetFeature(MaxContactFeatureReportId); } } } // check that the two responses are equal. Console.WriteLine("Test {0}....", featureNet.SequenceEqual(featureHid) ? "Passed" : "Failed"); Console.ReadKey(true); }
public static void Main(string[] args) { var usbDevices = UsbHelpers.GetConnectedUSBDevices(); foreach (var usbDevice in usbDevices) { Console.WriteLine($"Device ID: {usbDevice.DeviceId}, PNP Device ID: {usbDevice.PnpDeviceId}, Description: {usbDevice.Description}"); } Console.Read(); }