Created
March 25, 2021 16:15
-
-
Save rakisaionji/6197501133c612f4c4817fd3ba07bf42 to your computer and use it in GitHub Desktop.
A spaghetti piece of code to list all raw input devices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
class Program | |
{ | |
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)] | |
private static extern uint GetRawInputDeviceList([In][Out] RawInputDeviceList[] RawInputDeviceList, ref uint NumDevices, uint Size); | |
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)] | |
private static extern uint GetRawInputDeviceInfo(IntPtr hDevice, RawInputDeviceInfo command, IntPtr pData, ref uint size); | |
public enum RawInputDeviceType : uint | |
{ | |
MOUSE, | |
KEYBOARD, | |
HID | |
} | |
public enum RawInputDeviceInfo : uint | |
{ | |
PREPARSEDDATA = 0x20000005U, | |
DEVICENAME = 0x20000007U, | |
DEVICEINFO = 0x2000000BU | |
} | |
public struct RawInputDeviceList | |
{ | |
public IntPtr hDevice; | |
public RawInputDeviceType Type; | |
} | |
static void Main(string[] args) | |
{ | |
var devCnt = 0U; | |
var dwSize = (uint)Marshal.SizeOf(typeof(RawInputDeviceList)); | |
GetRawInputDeviceList(null, ref devCnt, dwSize); | |
if (devCnt == 0U) | |
{ | |
Console.WriteLine("No input device found."); | |
Console.ReadLine(); | |
return; | |
} | |
var devList = new RawInputDeviceList[devCnt]; | |
var listCnt = GetRawInputDeviceList(devList, ref devCnt, dwSize); | |
for (var i = 0U; i < listCnt; i++) | |
{ | |
var pcbSize = 0U; | |
var devType = devList[(int)((UIntPtr)i)].Type; | |
var hDevice = devList[(int)((UIntPtr)i)].hDevice; | |
GetRawInputDeviceInfo(hDevice, RawInputDeviceInfo.DEVICENAME, IntPtr.Zero, ref pcbSize); | |
if (pcbSize == 0) continue; | |
var pData = Marshal.AllocHGlobal((int)pcbSize); | |
GetRawInputDeviceInfo(hDevice, RawInputDeviceInfo.DEVICENAME, pData, ref pcbSize); | |
var devName = Marshal.PtrToStringAnsi(pData); | |
Console.WriteLine("{0} - {1}", devType, devName); | |
Marshal.FreeHGlobal(pData); | |
} | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment