/* open hid device */ public bool Open(HIDInfo dev) { /* safe file handle */ SafeFileHandle shandle; /* opens hid device file */ handle = Native.CreateFile(dev.Path, Native.GENERIC_READ | Native.GENERIC_WRITE, Native.FILE_SHARE_READ | Native.FILE_SHARE_WRITE, IntPtr.Zero, Native.OPEN_EXISTING, Native.FILE_FLAG_OVERLAPPED, IntPtr.Zero); /* whops */ if (handle == Native.INVALID_HANDLE_VALUE) { return(false); } /* build up safe file handle */ shandle = new SafeFileHandle(handle, false); /* prepare stream - async */ _fileStream = new FileStream(shandle, FileAccess.ReadWrite, 32, true); /* report status */ return(true); }
/* open hid device */ public bool Open(HIDInfo dev) { /* safe file handle */ SafeFileHandle shandle; /* opens hid device file */ handle = Native.CreateFile(dev.Path, Native.GENERIC_READ | Native.GENERIC_WRITE, Native.FILE_SHARE_READ | Native.FILE_SHARE_WRITE, IntPtr.Zero, Native.OPEN_EXISTING, Native.FILE_FLAG_OVERLAPPED, IntPtr.Zero); /* whops */ if (handle == Native.INVALID_HANDLE_VALUE) { return false; } /* build up safe file handle */ shandle = new SafeFileHandle(handle, false); /* prepare stream - async */ _fileStream = new FileStream(shandle, FileAccess.ReadWrite, 32, true); /* report status */ return true; }
/* browse all HID class devices */ public static List<HIDInfo> Browse() { /* hid device class guid */ Guid gHid; /* list of device information */ List<HIDInfo> info = new List<HIDInfo>(); /* obtain hid guid */ Native.HidD_GetHidGuid(out gHid); /* get list of present hid devices */ var hInfoSet = Native.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, Native.DIGCF_DEVICEINTERFACE | Native.DIGCF_PRESENT); /* allocate mem for interface descriptor */ var iface = new Native.DeviceInterfaceData(); /* set size field */ iface.Size = Marshal.SizeOf(iface); /* interface index */ uint index = 0; /* iterate through all interfaces */ while (Native.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface)) { /* vid and pid */ short vid, pid; /* get device path */ var path = GetPath(hInfoSet, ref iface); /* open device */ var handle = Open(path); /* device is opened? */ if (handle != Native.INVALID_HANDLE_VALUE) { /* get device manufacturer string */ var man = GetManufacturer(handle); /* get product string */ var prod = GetProduct(handle); /* get serial number */ var serial = GetSerialNumber(handle); /* get vid and pid */ GetVidPid(handle, out vid, out pid); /* build up a new element */ HIDInfo i = new HIDInfo(prod, serial, man, path, vid, pid); /* add to list */ info.Add(i); /* close */ Close(handle); } /* next, please */ index++; } /* clean up */ if (Native.SetupDiDestroyDeviceInfoList(hInfoSet) == false) { /* fail! */ throw new Win32Exception(); } /* return list */ return info; }
/* browse all HID class devices */ public static List <HIDInfo> Browse() { /* hid device class guid */ Guid gHid; /* list of device information */ List <HIDInfo> info = new List <HIDInfo>(); /* obtain hid guid */ Native.HidD_GetHidGuid(out gHid); /* get list of present hid devices */ var hInfoSet = Native.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, Native.DIGCF_DEVICEINTERFACE | Native.DIGCF_PRESENT); /* allocate mem for interface descriptor */ var iface = new Native.DeviceInterfaceData(); /* set size field */ iface.Size = Marshal.SizeOf(iface); /* interface index */ uint index = 0; /* iterate through all interfaces */ while (Native.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface)) { /* vid and pid */ short vid, pid; /* get device path */ var path = GetPath(hInfoSet, ref iface); /* open device */ var handle = Open(path); /* device is opened? */ if (handle != Native.INVALID_HANDLE_VALUE) { /* get device manufacturer string */ var man = GetManufacturer(handle); /* get product string */ var prod = GetProduct(handle); /* get serial number */ var serial = GetSerialNumber(handle); /* get vid and pid */ GetVidPid(handle, out vid, out pid); /* build up a new element */ HIDInfo i = new HIDInfo(prod, serial, man, path, vid, pid); /* add to list */ info.Add(i); /* close */ Close(handle); } /* next, please */ index++; } /* clean up */ if (Native.SetupDiDestroyDeviceInfoList(hInfoSet) == false) { /* fail! */ throw new Win32Exception(); } /* return list */ return(info); }