WinHidManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #region License
  2. /* Copyright 2012-2013 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. using System.Runtime.InteropServices;
  15. namespace HidSharp.Platform.Windows
  16. {
  17. internal class WinHidManager : HidManager
  18. {
  19. protected override object[] Refresh()
  20. {
  21. var paths = new List<string>();
  22. Guid hidGuid; NativeMethods.HidD_GetHidGuid(out hidGuid);
  23. NativeMethods.HDEVINFO devInfo = NativeMethods.SetupDiGetClassDevs(hidGuid, null, IntPtr.Zero,
  24. /*NativeMethods.DIGCF.AllClasses |*/ NativeMethods.DIGCF.DeviceInterface | NativeMethods.DIGCF.Present);
  25. if (devInfo.IsValid)
  26. {
  27. try
  28. {
  29. NativeMethods.SP_DEVICE_INTERFACE_DATA did = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
  30. did.Size = Marshal.SizeOf(did);
  31. for (int i = 0; NativeMethods.SetupDiEnumDeviceInterfaces(devInfo, IntPtr.Zero, hidGuid, i, ref did); i++)
  32. {
  33. NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA didetail = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA();
  34. didetail.Size = IntPtr.Size == 8 ? 8 : (4 + Marshal.SystemDefaultCharSize);
  35. if (NativeMethods.SetupDiGetDeviceInterfaceDetail(devInfo, ref did, ref didetail,
  36. Marshal.SizeOf(didetail) - (int)Marshal.OffsetOf(didetail.GetType(), "DevicePath"),
  37. IntPtr.Zero, IntPtr.Zero))
  38. {
  39. paths.Add(didetail.DevicePath);
  40. }
  41. }
  42. }
  43. finally
  44. {
  45. NativeMethods.SetupDiDestroyDeviceInfoList(devInfo);
  46. }
  47. }
  48. return paths.Cast<object>().ToArray();
  49. }
  50. protected override bool TryCreateDevice(object key, out HidDevice device, out object completionState)
  51. {
  52. string path = (string)key; var hidDevice = new WinHidDevice(path);
  53. IntPtr handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.None, NativeMethods.EFileShare.All);
  54. device = null; completionState = null; if (handle == (IntPtr)(-1)) { return false; }
  55. bool ok = false;
  56. try { ok = hidDevice.GetInfo(handle); } catch { }
  57. if (!ok) { NativeMethods.CloseHandle(handle); return false; }
  58. device = hidDevice; completionState = handle;
  59. return true;
  60. }
  61. protected override void CompleteDevice(object key, HidDevice device, object creationState)
  62. {
  63. var hidDevice = (WinHidDevice)device; var handle = (IntPtr)creationState;
  64. hidDevice.GetInfoComplete(handle);
  65. }
  66. public override bool IsSupported
  67. {
  68. get
  69. {
  70. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  71. {
  72. var version = new NativeMethods.OSVERSIONINFO();
  73. version.OSVersionInfoSize = Marshal.SizeOf(typeof(NativeMethods.OSVERSIONINFO));
  74. try
  75. {
  76. if (NativeMethods.GetVersionEx(ref version) && version.PlatformID == 2)
  77. {
  78. return true;
  79. }
  80. }
  81. catch
  82. {
  83. // Apparently we have no P/Invoke access.
  84. }
  85. }
  86. return false;
  87. }
  88. }
  89. }
  90. }