/// <summary> /// Helper method to return the device path given a DeviceInterfaceData structure and an InfoSet handle. /// Used in 'FindDevice' so check that method out to see how to get an InfoSet handle and a DeviceInterfaceData. /// </summary> /// <param name="infoSetPointer">Handle to the InfoSet</param> /// <param name="deviceInterfaceData">DeviceInterfaceData structure</param> /// <returns>The device path or null if there was some problem</returns> internal static string GetDevicePath(IntPtr infoSetPointer, ref Win32USB.DeviceInterfaceData deviceInterfaceData) { uint requiredSize = 0; // Get the device interface details if (Win32USB.SetupDiGetDeviceInterfaceDetail(infoSetPointer, ref deviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero) == false) { var deviceInterfaceDetailData = new Win32USB.DeviceInterfaceDetailData(); if (IntPtr.Size == 8) { deviceInterfaceDetailData.Size = 8; } else { deviceInterfaceDetailData.Size = 5; } if (Win32USB.SetupDiGetDeviceInterfaceDetail(infoSetPointer, ref deviceInterfaceData, ref deviceInterfaceDetailData, requiredSize, ref requiredSize, IntPtr.Zero)) { return(deviceInterfaceDetailData.DevicePath); } } return(null); }
protected override void OnHandleDestroyed(EventArgs e) { base.OnHandleDestroyed(e); Win32USB.UnregisterDeviceNotification(messagePump.usbEventsSubscription); messagePump.Handle = IntPtr.Zero; }
protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); messagePump.Handle = Handle; messagePump.usbEventsSubscription = Win32USB.RegisterForUsbEvents(Handle, HID.Guid); }
private bool GetCaps() { InputReportLength = -1; OutputReportLength = -1; if (Win32USB.HidD_GetPreparsedData(handle, out IntPtr preparsedData)) { try { Win32USB.HidP_GetCaps(preparsedData, out Win32USB.HidCaps caps); InputReportLength = caps.InputReportByteLength; OutputReportLength = caps.OutputReportByteLength; } finally { Win32USB.HidD_FreePreparsedData(ref preparsedData); } return(true); } else { Win32USB.CloseHandle(handle); handle = null; return(false); } }
public bool Start() { Dispose(); handle = Win32USB.CreateFile( DevicePath, Win32USB.GENERIC_READ | Win32USB.GENERIC_WRITE, Win32USB.FILE_SHARE_READ | Win32USB.FILE_SHARE_WRITE, IntPtr.Zero, Win32USB.OPEN_EXISTING, Win32USB.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == null) { return(false); } if (GetCaps() == false) { return(false); } Stream = new FileStream(handle, FileAccess.ReadWrite, Math.Max(InputReportLength, OutputReportLength), true); return(true); }
public static IEnumerable <DeviceInfo> GetDevices(ushort vendorID, ushort productID) { Guid gHid = HID.Guid; IntPtr infoSetPointer = Win32USB.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, Win32USB.DIGCF_DEVICEINTERFACE | Win32USB.DIGCF_PRESENT); try { var deviceInterfaceData = new Win32USB.DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); int index = 0; while (Win32USB.SetupDiEnumDeviceInterfaces(infoSetPointer, 0, ref gHid, (uint)index, ref deviceInterfaceData)) { string devicePath = GetDevicePath(infoSetPointer, ref deviceInterfaceData); if (devicePath != null) { if (vendorID == 0 && productID == 0) { yield return(new DeviceInfo(devicePath)); } else { if (DeviceInfo.ParseDevicePath(devicePath, out ushort localVendorID, out ushort localProductID, out uint localIdentifier) && localVendorID == vendorID && localProductID == productID) { yield return(new DeviceInfo(devicePath)); } } } index++; } } finally { Win32USB.SetupDiDestroyDeviceInfoList(infoSetPointer); } }