dgm5555

How can I use _WinAPI_GetRawInputDeviceInfo to get HID device info?

4 posts in this topic

#1 ·  Posted (edited)

I'm trying to modify the example code to actually get the HID device info (rather than just the name), but I can't make it work (all I get is zeros).

Quote

\\?\HID#VID_056A&PID_0314&Col05#6&1a48d70e&0&0004#{4d1e55b2-f16f-11cf-88cb-001111000030}
Device Info
0
0
0
0
0
 

Can anyone help?

 

#include <APISysConstants.au3>
#include <Array.au3>
#include <WinAPISys.au3>

Local $tText, $aData = _WinAPI_EnumRawInputDevices()

;ConsoleWrite($tagRID_DEVICE_INFO_HID)          ; struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruc
If IsArray($aData) Then
    ReDim $aData[$aData[0][0] + 1][3]
    $tText = DllStructCreate('wchar[256]')
    $devInf = DllStructCreate($tagRID_DEVICE_INFO_HID)
    For $i = 1 To $aData[0][0]
        If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tText, 256, $RIDI_DEVICENAME) Then
            $aData[$i][2] = DllStructGetData($tText, 1)
            ConsoleWrite ($aData[$i][2] & @CRLF)
        Else
            $aData[$i][2] = ''
        EndIf


        If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $devInf, DllStructGetSize($devInf), $RIDI_DEVICEINFO ) Then
            ConsoleWrite ("Device Info:-" & @CRLF)

            ConsoleWrite (DllStructGetData($devInf, 'VendorId') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'ProductId') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'VersionNumber') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'UsagePage') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'Usage') & @CRLF)
        EndIf

    Next

EndIf

_ArrayDisplay($aData, '_WinAPI_EnumRawInputDevices')

 

Edited by dgm5555

Share this post


Link to post
Share on other sites



This should work

#include <APISysConstants.au3>
#include <Array.au3>
#include <WinAPISys.au3>

; fix this first in WinAPISys.au3
; Global Const $tagRID_DEVICE_INFO_HID = 'struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruc'  <<< add trailing "t"

; ConsoleWrite($tagRID_INFO_HID)          ; dword Size;dword Type;struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruct;dword Unused[2]

Local $tText, $aData = _WinAPI_EnumRawInputDevices()
If IsArray($aData) Then
    ReDim $aData[$aData[0][0] + 1][3]
    $tText = DllStructCreate('wchar[256]')
    For $i = 1 To $aData[0][0]
        If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tText, 256, $RIDI_DEVICENAME) Then
            $aData[$i][2] = DllStructGetData($tText, 1)
            ConsoleWrite ($aData[$i][2] & @CRLF)
        Else
            $aData[$i][2] = ''
        EndIf

     If $aData[$i][1] = $RIM_TYPEHID Then
       $devInf = DllStructCreate($tagRID_INFO_HID)
       $p_devInf = DllStructGetPtr($devInf)
       $i_devInf = DllStructGetSize($devInf)
       DllStructSetData($devInf , 'Size', $i_devInf)

        If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $p_devInf, $i_devInf, $RIDI_DEVICEINFO ) Then
            ConsoleWrite ("Device Info:-" & @CRLF)

            ConsoleWrite (DllStructGetData($devInf, 'VendorId') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'ProductId') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'VersionNumber') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'UsagePage') & @CRLF)
            ConsoleWrite (DllStructGetData($devInf, 'Usage') & @CRLF)
        EndIf
      EndIf
    Next

EndIf

_ArrayDisplay($aData, '_WinAPI_EnumRawInputDevices')

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

No, unfortunately I still get the same list of 0s  (although your code restricts it to the HID type device and skips the mouse/keyboard).

For testing I added the following in case the failure was HID-type device specific to see if it would list mouse or keyboard info (and to enable testing on systems without other HID devices) but it doesn't work either.

 

#include <APISysConstants.au3>
#include <Array.au3>
#include <WinAPISys.au3>

; fix this first in WinAPISys.au3
; Global Const $tagRID_DEVICE_INFO_HID = 'struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruc'  <<< add trailing "t"

; ConsoleWrite($tagRID_INFO_HID)          ; dword Size;dword Type;struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruct;dword Unused[2]

Local $tText, $aData = _WinAPI_EnumRawInputDevices()
If IsArray($aData) Then
    ReDim $aData[$aData[0][0] + 1][3]
    $tText = DllStructCreate('wchar[256]')
    For $i = 1 To $aData[0][0]
        If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tText, 256, $RIDI_DEVICENAME) Then
            $aData[$i][2] = DllStructGetData($tText, 1)
            ConsoleWrite ($aData[$i][2] & @CRLF)
        Else
            $aData[$i][2] = ''
        EndIf

        If $aData[$i][1] = $RIM_TYPEMOUSE Then
           $devInf = DllStructCreate($tagRID_INFO_MOUSE)
           $p_devInf = DllStructGetPtr($devInf)
           $i_devInf = DllStructGetSize($devInf)
           DllStructSetData($devInf , 'Size', $i_devInf)

            If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $p_devInf, $i_devInf, $RIDI_DEVICEINFO ) Then
                ConsoleWrite ("Device Info:-" & @CRLF)

                ConsoleWrite (DllStructGetData($devInf, 'Id') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'NumberOfButtons') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'SampleRate') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'HasHorizontalWheel') & @CRLF)
            EndIf
          EndIf

        If $aData[$i][1] = $RIM_TYPEKEYBOARD Then
           $devInf = DllStructCreate($tagRID_INFO_KEYBOARD)
           $p_devInf = DllStructGetPtr($devInf)
           $i_devInf = DllStructGetSize($devInf)
           DllStructSetData($devInf , 'Size', $i_devInf)

            If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $p_devInf, $i_devInf, $RIDI_DEVICEINFO ) Then
                ConsoleWrite ("Device Info:-" & @CRLF)

                ConsoleWrite (DllStructGetData($devInf, 'KbType') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'KbSubType') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'KeyboardMode') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'NumberOfFunctionKeys') & @CRLF)
            EndIf
          EndIf


        If $aData[$i][1] = $RIM_TYPEHID Then
            $devInf = DllStructCreate($tagRID_INFO_HID)
            $p_devInf = DllStructGetPtr($devInf)
            $i_devInf = DllStructGetSize($devInf)
            DllStructSetData($devInf , 'Size', $i_devInf)

            If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $p_devInf, $i_devInf, $RIDI_DEVICEINFO ) Then
                ConsoleWrite ("Device Info:-" & @CRLF)

                ConsoleWrite (DllStructGetData($devInf, 'VendorId') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'ProductId') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'VersionNumber') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'UsagePage') & @CRLF)
                ConsoleWrite (DllStructGetData($devInf, 'Usage') & @CRLF)
            EndIf
        EndIf
    Next

EndIf

_ArrayDisplay($aData, '_WinAPI_EnumRawInputDevices')

Also I'm not sure why you are setting up a pointer to $devInf as shouldn't this already be a pointer?

(I did try converting the GetRawInputDeviceInfo call back to $devInf but keeping $i_devInf for size, but that didn't work either)

Edited by dgm5555

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

DUH of course. mikell in a reply to another post of mine noted there are typos in the autoit WinAPISys.au3 core code. This of course causes the script to fail as DllStructCreate doesn't work properly. This was actually pretty obvious when I checked as $i_devInf (sizeOf) was always 0.

(endstruct constructs are missing their final t)

this hack works (and I've contracted it slightly)

#include <APISysConstants.au3>
#include <Array.au3>
#include <WinAPISys.au3>

; fix this first in WinAPISys.au3
; Global Const $tagRID_DEVICE_INFO_HID = 'struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruc'  <<< add trailing "t"

; ConsoleWrite($tagRID_INFO_HID)          ; dword Size;dword Type;struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruct;dword Unused[2]



; NB The following tags are a hack due to typos in the autoit core code (in WinAPISys.au3)
$atagRID_DEVICE_INFO_MOUSE = 'struct;dword Id;dword NumberOfButtons;dword SampleRate;int HasHorizontalWheel;endstruct'
$atagRID_DEVICE_INFO_KEYBOARD = 'struct;dword KbType;dword KbSubType;dword KeyboardMode;dword NumberOfFunctionKeys;dword NumberOfIndicators;dword NumberOfKeysTotal;endstruct'
$atagRID_DEVICE_INFO_HID = 'struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruct'

$atagRID_INFO_MOUSE = 'dword Size;dword Type;' & $atagRID_DEVICE_INFO_MOUSE & ';dword Unused[2];'
$atagRID_INFO_KEYBOARD = 'dword Size;dword Type;' & $atagRID_DEVICE_INFO_KEYBOARD
$atagRID_INFO_HID = 'dword Size;dword Type;' & $atagRID_DEVICE_INFO_HID & ';dword Unused[2]'



Local $tText, $aData = _WinAPI_EnumRawInputDevices()
If IsArray($aData) Then
    ReDim $aData[$aData[0][0] + 1][3]
    $tText = DllStructCreate('wchar[256]')
    For $i = 1 To $aData[0][0]
        If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tText, 256, $RIDI_DEVICENAME) Then
            $aData[$i][2] = DllStructGetData($tText, 1)
            ConsoleWrite ($aData[$i][2] & @CRLF)
        Else
            $aData[$i][2] = ''
        EndIf

        If $aData[$i][1] = $RIM_TYPEMOUSE Then
           $devInf = DllStructCreate($atagRID_INFO_MOUSE)

            If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $devInf, DllStructGetSize($devInf), $RIDI_DEVICEINFO ) Then
                ConsoleWrite ("Mouse Info:-" & @CRLF)

                ConsoleWrite ('Id: ' & DllStructGetData($devInf, 'Id') & @CRLF)
                ConsoleWrite ('NumberOfButtons: ' & DllStructGetData($devInf, 'NumberOfButtons') & @CRLF)
                ConsoleWrite ('SampleRate: ' & DllStructGetData($devInf, 'SampleRate') & @CRLF)
                ConsoleWrite ('HasHorizontalWheel: ' & DllStructGetData($devInf, 'HasHorizontalWheel') & @CRLF)
            EndIf
          EndIf

        If $aData[$i][1] = $RIM_TYPEKEYBOARD Then
           $devInf = DllStructCreate($atagRID_INFO_KEYBOARD)

            If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $devInf, DllStructGetSize($devInf), $RIDI_DEVICEINFO ) Then
                ConsoleWrite ("Keyboard Info:-" & @CRLF)

                ConsoleWrite ('KbType: ' & DllStructGetData($devInf, 'KbType') & @CRLF)
                ConsoleWrite ('KbSubType: ' & DllStructGetData($devInf, 'KbSubType') & @CRLF)
                ConsoleWrite ('KeyboardMode: ' & DllStructGetData($devInf, 'KeyboardMode') & @CRLF)
                ConsoleWrite ('NumberOfFunctionKeys: ' & DllStructGetData($devInf, 'NumberOfFunctionKeys') & @CRLF)
            EndIf
          EndIf


        If $aData[$i][1] = $RIM_TYPEHID Then
            $devInf = DllStructCreate($atagRID_INFO_HID)

            If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $devInf, DllStructGetSize($devInf), $RIDI_DEVICEINFO ) Then
                ConsoleWrite ("Device Info:-" & @CRLF)

                ; see http://www.usb.org/developers/hidpage/Hut1_12v2.pdf for UsagePage and UsageID
                ConsoleWrite ('VendorId: ' & Hex(DllStructGetData($devInf, 'VendorId'),4) & @CRLF)
                ConsoleWrite ('ProductId: ' & Hex(DllStructGetData($devInf, 'ProductId'),4) & @CRLF)
                ConsoleWrite ('VersionNumber: ' & DllStructGetData($devInf, 'VersionNumber') & @CRLF)
                ConsoleWrite ('UsagePage: ' & Hex(DllStructGetData($devInf, 'UsagePage'),2) & @CRLF)
                ConsoleWrite ('Usage: ' & Hex(DllStructGetData($devInf, 'Usage'),2) & @CRLF)
            EndIf
        EndIf
    Next

EndIf

;_ArrayDisplay($aData, '_WinAPI_EnumRawInputDevices')

 

Edited by dgm5555

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now