private void EnumerateDevices() { uint deviceCount = 0; int dwSize = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST)); if (NativeMethods.GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0) { IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount)); try { _validHandles.Clear(); NativeMethods.GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize); for (int i = 0; i < deviceCount; i++) { uint pcbSize = 0; NativeMethods.RAWINPUTDEVICELIST rid = (NativeMethods.RAWINPUTDEVICELIST)Marshal.PtrToStructure( new IntPtr(pRawInputDeviceList.ToInt64() + dwSize * i), typeof(NativeMethods.RAWINPUTDEVICELIST)); if (rid.dwType == NativeMethods.RIM_TYPEHID) { NativeMethods.GetRawInputDeviceInfo(rid.hDevice, NativeMethods.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize); if (pcbSize > 0) { IntPtr pData = Marshal.AllocHGlobal((int)pcbSize); try { NativeMethods.GetRawInputDeviceInfo(rid.hDevice, NativeMethods.RIDI_DEVICENAME, pData, ref pcbSize); string deviceName = Marshal.PtrToStringAnsi(pData); if (string.IsNullOrEmpty(deviceName) || deviceName.IndexOf("VIRTUAL_DIGITIZER", StringComparison.OrdinalIgnoreCase) >= 0 || deviceName.IndexOf("ROOT", StringComparison.OrdinalIgnoreCase) >= 0) { continue; } _validHandles.Add(rid.hDevice); } finally { Marshal.FreeHGlobal(pData); } } } } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } } else { throw new ApplicationException("Error!"); } }
private static bool HasTabletDevices() { uint deviceCount = 0; // Determine the number of devices first (result will be -1 if fails and cDevices will have count) int result = (int)MS.Win32.UnsafeNativeMethods.GetRawInputDeviceList(null, ref deviceCount, (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST))); if (result >= 0 && deviceCount != 0) { NativeMethods.RAWINPUTDEVICELIST[] ridl = new NativeMethods.RAWINPUTDEVICELIST[deviceCount]; int count = (int)MS.Win32.UnsafeNativeMethods.GetRawInputDeviceList(ridl, ref deviceCount, (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST))); if (count > 0) { for (int i = 0; i < count; i++) { if (ridl[i].dwType == NativeMethods.RIM_TYPEHID) { NativeMethods.RID_DEVICE_INFO deviceInfo = new NativeMethods.RID_DEVICE_INFO(); deviceInfo.cbSize = (uint)Marshal.SizeOf(typeof(NativeMethods.RID_DEVICE_INFO)); uint cbSize = (uint)deviceInfo.cbSize; int cBytes = (int)MS.Win32.UnsafeNativeMethods.GetRawInputDeviceInfo(ridl[i].hDevice, NativeMethods.RIDI_DEVICEINFO, ref deviceInfo, ref cbSize); if (cBytes > 0) { if (deviceInfo.hid.usUsagePage == NativeMethods.HID_USAGE_PAGE_DIGITIZER) { switch (deviceInfo.hid.usUsage) { case NativeMethods.HID_USAGE_DIGITIZER_DIGITIZER: case NativeMethods.HID_USAGE_DIGITIZER_PEN: case NativeMethods.HID_USAGE_DIGITIZER_TOUCHSCREEN: case NativeMethods.HID_USAGE_DIGITIZER_LIGHTPEN: { return(true); } } } } else { System.Diagnostics.Debug.WriteLine("TabletDeviceCollection: GetRawInputDeviceInfo failed!"); } } } } else if (count < 0) { System.Diagnostics.Debug.WriteLine("TabletDeviceCollection: GetRawInputDeviceList failed!"); } } return(false); }
private int EnumerateDevices() { int numberOfDevices = 0; uint deviceCount = 0; int dwSize = (Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST))); if (NativeMethods.GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0) { IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount)); try { _touchScreenPhysicalMax.Clear(); NativeMethods.GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize); for (int i = 0; i < deviceCount; i++) { uint pcbSize = 0; NativeMethods.RAWINPUTDEVICELIST rid = (NativeMethods.RAWINPUTDEVICELIST)Marshal.PtrToStructure( new IntPtr(pRawInputDeviceList.ToInt64() + dwSize * i), typeof(NativeMethods.RAWINPUTDEVICELIST)); NativeMethods.GetRawInputDeviceInfo(rid.hDevice, NativeMethods.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize); if (pcbSize > 0) { IntPtr pData = Marshal.AllocHGlobal((int)pcbSize); try { NativeMethods.GetRawInputDeviceInfo(rid.hDevice, NativeMethods.RIDI_DEVICENAME, pData, ref pcbSize); string deviceName = Marshal.PtrToStringAnsi(pData); if (deviceName != null && deviceName.IndexOf("ROOT", StringComparison.OrdinalIgnoreCase) >= 0) { continue; } if (rid.dwType == NativeMethods.RIM_TYPEHID) { var isTouchDevice = CheckDeviceIsTouchScreen(deviceName); if (isTouchDevice && !_touchScreenPhysicalMax.ContainsKey(rid.hDevice)) { numberOfDevices++; _touchScreenPhysicalMax.Add(rid.hDevice, Point.Empty); } } } finally { Marshal.FreeHGlobal(pData); } } } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } return(numberOfDevices); } else { throw new ApplicationException("Error!"); } }
private static bool HasTabletDevices() { uint deviceCount = 0; // Determine the number of devices first (result will be -1 if fails and cDevices will have count) int result = (int)MS.Win32.UnsafeNativeMethods.GetRawInputDeviceList(null, ref deviceCount, (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST))); if (result >= 0 && deviceCount != 0) { NativeMethods.RAWINPUTDEVICELIST[] ridl = new NativeMethods.RAWINPUTDEVICELIST[deviceCount]; int count = (int)MS.Win32.UnsafeNativeMethods.GetRawInputDeviceList(ridl, ref deviceCount, (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST))); if (count > 0) { for (int i = 0; i < count; i++) { if (ridl[i].dwType == NativeMethods.RIM_TYPEHID) { NativeMethods.RID_DEVICE_INFO deviceInfo = new NativeMethods.RID_DEVICE_INFO(); deviceInfo.cbSize = (uint)Marshal.SizeOf(typeof(NativeMethods.RID_DEVICE_INFO)); uint cbSize = (uint)deviceInfo.cbSize; int cBytes = (int)MS.Win32.UnsafeNativeMethods.GetRawInputDeviceInfo(ridl[i].hDevice, NativeMethods.RIDI_DEVICEINFO, ref deviceInfo, ref cbSize); if (cBytes > 0) { if (deviceInfo.hid.usUsagePage == NativeMethods.HID_USAGE_PAGE_DIGITIZER) { switch (deviceInfo.hid.usUsage) { case NativeMethods.HID_USAGE_DIGITIZER_DIGITIZER: case NativeMethods.HID_USAGE_DIGITIZER_PEN: case NativeMethods.HID_USAGE_DIGITIZER_TOUCHSCREEN: case NativeMethods.HID_USAGE_DIGITIZER_LIGHTPEN: { return true; } } } } else { System.Diagnostics.Debug.WriteLine("TabletDeviceCollection: GetRawInputDeviceInfo failed!"); } } } } else if (count < 0) { System.Diagnostics.Debug.WriteLine("TabletDeviceCollection: GetRawInputDeviceList failed!"); } } return false; }