Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetRawInputDeviceInfo (user32)
 
.
Summary
The GetRawInputDeviceInfo function gets information about the raw input device.

C# Signature:

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);

C# User-Defined Types:

    [StructLayout(LayoutKind.Sequential)]
    internal struct RID_DEVICE_INFO_HID
    {
        [MarshalAs(UnmanagedType.U4)]
        public int dwVendorId;
        [MarshalAs(UnmanagedType.U4)]
        public int dwProductId;
        [MarshalAs(UnmanagedType.U4)]
        public int dwVersionNumber;
        [MarshalAs(UnmanagedType.U2)]
        public ushort usUsagePage;
        [MarshalAs(UnmanagedType.U2)]
        public ushort usUsage;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct RID_DEVICE_INFO_KEYBOARD
    {
        [MarshalAs(UnmanagedType.U4)]
        public int dwType;
        [MarshalAs(UnmanagedType.U4)]
        public int dwSubType;
        [MarshalAs(UnmanagedType.U4)]
        public int dwKeyboardMode;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfFunctionKeys;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfIndicators;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfKeysTotal;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct RID_DEVICE_INFO_MOUSE
    {
        [MarshalAs(UnmanagedType.U4)]
        public int dwId;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfButtons;
        [MarshalAs(UnmanagedType.U4)]
        public int dwSampleRate;
        [MarshalAs(UnmanagedType.U4)]
        public int fHasHorizontalWheel;
    }

    [StructLayout(LayoutKind.Explicit)]
    internal struct RID_DEVICE_INFO
    {
        [FieldOffset(0)]
        public int cbSize;
        [FieldOffset(4)]
        public int dwType;
        [FieldOffset(8)]
        public RID_DEVICE_INFO_MOUSE mouse;
        [FieldOffset(8)]
        public RID_DEVICE_INFO_KEYBOARD keyboard;
        [FieldOffset(8)]
        public RID_DEVICE_INFO_HID hid;
    }
    internal enum DeviceInfoTypes {
        RIDI_PREPARSEDDATA = 0x20000005,
        RIDI_DEVICENAME = 0x20000007,
        RIDI_DEVICEINFO = 0x2000000B
    }

VB Signature:

Declare Function GetRawInputDeviceInfo Lib "user32.dll" Alias "GetRawInputDeviceInfoW" (ByVal hDevice As IntPtr, ByVal uiCommand As DeviceInfoTypes, ByVal pData As IntPtr, ByRef pcbSize As UInteger) As Integer

VB User-Defined Types:

Public Enum DeviceInfoTypes As UInteger
     RIDI_PREPARSEDDATA = &H20000005
     RIDI_DEVICENAME = &H20000007
     RIDI_DEVICEINFO = &H2000000B
End Enum

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    ' Enumerating devices with a known VID
    ' First half heavily borrowed from C# example on GetRawInputDeviceList()
    Sub GetAllRawDeviceVID()

         Dim deviceCount As UInt32 = 0
         Dim dwSize As Integer = CInt(Marshal.SizeOf(GetType(RAWINPUTDEVICELIST)))
         Dim retValue As UInt32 = GetRawInputDeviceList(IntPtr.Zero, deviceCount, dwSize)
         If retValue <> 0 Then Exit Sub       ' handle this

         Dim devPtr As IntPtr = Marshal.AllocHGlobal(CType(dwSize * deviceCount, Integer))
         retValue = GetRawInputDeviceList(devPtr, deviceCount, dwSize)

         Dim deviceList As RAWINPUTDEVICELIST
         Dim rawDevs As New Dictionary(Of IntPtr, RawInputDeviceType)

         For i As Integer = 0 To (deviceCount - 1)
         deviceList = CType(Marshal.PtrToStructure(New IntPtr((devPtr.ToInt32() + (dwSize * i))), GetType(RAWINPUTDEVICELIST)), RAWINPUTDEVICELIST)
         If deviceList.dwType = RawInputDeviceType.KEYBOARD Then
             rawDevs.Add(deviceList.hDevice, deviceList.dwType)
         End If

         Next

         Dim pcbSize As UInt32 = 0
         Dim deviceName As String = String.Empty
         Dim lstHDevices As New List(Of String)

         For Each itm As KeyValuePair(Of IntPtr, RawInputDeviceType) In rawDevs
         GetRawInputDeviceInfo(itm.Key, DeviceInfoTypes.RIDI_DEVICENAME, IntPtr.Zero, pcbSize)
         Dim pData As IntPtr = Marshal.AllocHGlobal(CType(pcbSize, Integer))

         GetRawInputDeviceInfo(itm.Key, DeviceInfoTypes.RIDI_DEVICENAME, pData, pcbSize)
         deviceName = Marshal.PtrToStringAuto(pData)

         If deviceName.Contains("VID_05FE") Then lstHDevices.Add(deviceName)

         Next

         ' do something

     End Sub

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions