• Welcome to Theos PowerBasic Museum 2017.

USB HID GetRawInputData

Started by John Thompson, October 25, 2009, 02:06:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

John Thompson

GetRawInputData allows you to receive data from USB HIDs (Human Interface Devices).  These are keyboards, mice, magnetic card readers, etc.

Can anyone help out with a C to PB conversion?

I am trying to use the following function in PB:

GetRawInputData
http://msdn.microsoft.com/en-us/library/ms645596%28VS.85%29.aspx

UINT GetRawInputData(     
    HRAWINPUT hRawInput,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize,
    UINT cbSizeHeader
);


The first time you call it, pData is %NULL and it will set pcbSize to the amount of space you need to allocate for pData.  The second time you call it, it fills pData.

This is a sample of its usage:

Doing a Standard Read of Raw Input
http://msdn.microsoft.com/en-us/library/ms645546%28VS.85%29.aspx#standard_read

LPBYTE lpb = new BYTE[dwsize]
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*)lpb;


Can anyone translate?

Thanks!

-John

Patrice Terrier

from José's WinUser.inc


'/*
' * Flags for GetRawInputData
' */

%RID_INPUT  = &H10000003???
%RID_HEADER = &H10000005???

DECLARE FUNCTION GetRawInputData LIB "USER32.DLL" ALIAS "GetRawInputData" ( _
   BYVAL hRawInput AS DWORD _                           ' __in HRAWINPUT hRawInput
, BYVAL uiCommand AS DWORD _                           ' __in UINT uiCommand
, BYREF pData AS ANY _                                 ' __out LPVOID pData
, BYREF pcbSize AS DWORD _                             ' __in_out PUINT pcbSize
, BYVAL cbSizeHeader AS DWORD _                        ' __in UINT cbSizeHeader
) AS DWORD                                             ' UINT

'/*
' * Raw Input Device Information
' */
%RIDI_PREPARSEDDATA = &H20000005???
%RIDI_DEVICENAME    = &H20000007??? ' the return value is the character length, not the byte size
%RIDI_DEVICEINFO    = &H2000000b???
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

José Roca

 

DIM dwSize AS DWORD
GetRawInputData(lParam, %RID_INPUT, BYVAL %NULL, dwSize, SIZEOF(RAWINPUTHEADER))
DIM lpb AS STRING
lpb = SPACE$(dwSize)
GetRawInputData(lParam, %RID_INPUT, BYVAL STRPTR(lpb), dwSize, SIZEOF(RAWINPUTHEADER))
DIM raw AS RAWINPUT
LSET raw = lpb


John Thompson

Thank you!  That works perfectly.

John Thompson

Jose,

I'd used the code you provided to get the data, but it always seemed like I was missing data.  After trying out multiple HIDs (identical device), I found this to be the case.  I added error checking code (probably should've done that earlier) and found that upon the second call, it fails (returns -1) and a call to GetLastError advises 'Handle is invalid' #6.  As the only thing different from the successful call is the BYVAL STRPTR(lpb), I'm guessing this is where it's occurring.  Is there possibly another way they'd be expecting to submit this data?

Thanks again!

-John


        CASE %WM_INPUT
            DIM dwSize AS DWORD
            lRC = GETRAWINPUTDATA(lParam, %RID_INPUT, BYVAL %NULL, dwSize, SIZEOF(RAWINPUTHEADER))
dwError = GETLASTERROR ( ) : GOSUB WindowsError
? FORMAT$(lRC)
            DIM lpb AS STRING
            lpb = SPACE$(dwSize)
            lRC = GETRAWINPUTDATA(lParam, %RID_INPUT, BYVAL STRPTR(lpb), dwSize, SIZEOF(RAWINPUTHEADER))
dwError = GETLASTERROR ( ) : GOSUB WindowsError
? FORMAT$(lRC)

    WindowsError:
        FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, dwError, %NULL, zError, SIZEOF(zError), BYVAL %NULL
        MSGBOX zError, %MB_ICONERROR, "Windows Error (" & FORMAT$ ( dwError ) & ")"
        RETURN

José Roca

 
You can use a pointer to any kind of buffer of the appropriate size. Check that lParam is not zero and also that dwSize if not zero.

John Thompson

I checked and lParam <> 0 and dwSize <> 0.  The weird thing is that the data I get back actually contains a very small amount of data from the HID, but it is not properly formatted and is largely blank.  Is there another pointer type I should try?  Does anyone out there use any HIDs (joystick, magstripe readers, some GPS, etc.) who would be willing to try this out (I'll post a compilable example, but will need to determine Usage and UsagePage so that it can interact with the specific HID).

Thanks!

José Roca

 
I can't test it, since I don't have any HIDs.