flat assembler
Message board for the users of flat assembler.

Index > Windows > Windows and USB HID

Author
Thread Post new topic Reply to topic
VSH



Joined: 05 Jul 2022
Posts: 4
VSH
Hello,everybody.I am beginner in windows programming and FASM,i have made few projects with Easy Code IDE.In these project i used Serial port programming Win32 API
with success.Now i want to learn how to communicate with usb hid devices on windows.
So I would like to ask you is there any tutorials for HID Application Programming in FASM or can i use them with it?
At this stage i am interested about using this APIs:https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/introduction-to-hid-concepts ,but i can not call them in Easy Code and FASM,even that i add:hid.lib,hidclass.lib,hidparse.lib files to project.
For example if i use HidD_GetAttributes,build output shows: error:undefined symbol 'HidD_GetAttributes'.

P.S. i am sorry for my bad language
Thank you!
Post 05 Jul 2022, 12:29
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 652
Location: Russia
macomics
Functions are exported from hid.dll . You can make yourself a static import file of these functions. hid.lib are recommendations for MASM
Post 05 Jul 2022, 14:12
View user's profile Send private message Reply with quote
I



Joined: 19 May 2022
Posts: 31
I
I'm no expert but can cut and paste some part from a Razer mouse control I lost interest in and have mostly forgotten about. Didn't like the huge download for their app. Hopefully it should help.
Code:
format PE console

include 'win32a.inc'

 VID                    = 0x1532                                        ; Razer Viper Mini (mouse)
 PID                    = 0x008A                                        ; Vendor & Product ID

 struct SP_DEVICE_INTERFACE_DATA
  Size                  dd ?
  Guid                  rd 4
  Flags                 dd ?
  Rsvd                  dd ?
 ends

 struct SP_DEVICE_INTERFACE_DETAIL_DATA_A
  Size                  dd ?
  DevicePath            db ?                                            ; Variable size, reserve space directly after this
 ends

 struct HIDD_ATTRIBUTES
  Size                  dd ?
  VendorID              dw ?
  ProductID             dw ?
  VersionNumber         dw ?
 ends

 struct Urb
  Unk                   db ?
  Status                db ?
  Trans                 db ?
  Packet                dw ?
  Proto                 db ?
  DataSize              db ?
  Class                 db ?
  CMD                   db ?
  Data                  rb 80
  CS                    db 0
  Rsvd                  db 0
 ends

 GetFW                  = 0x87000800                                    ; A specific code for Viper Mini!

 DIGCF_DEFAULT          = 1
 DIGCF_PRESENT          = 2
 DIGCF_ALLCLASSES       = 4
 DIGCF_PROFILE          = 8
 DIGCF_DEVICEINTERFACE  = 0x10


;----------------------------------------
section '.text' code readable executable

        invoke  GetStdHandle,STD_OUTPUT_HANDLE
        mov     [hstd],eax

        mov     [HidAttribs.Size],sizeof.HIDD_ATTRIBUTES
        invoke  HidD_GetHidGuid,HidGuid                                 ; Get Hid GUID

        invoke  SetupDiGetClassDevs,HidGuid,0,0,DIGCF_PRESENT or DIGCF_DEVICEINTERFACE
        cmp     eax,INVALID_HANDLE_VALUE
        je      exit
        mov     [hDev],eax

        invoke  WriteFile,[hstd],Enum,Enum.len,NULL,NULL

 NextMember:
        mov     [SPDiD.Size],sizeof.SP_DEVICE_INTERFACE_DATA
        invoke  SetupDiEnumDeviceInterfaces,[hDev],NULL,HidGuid,[MemberIndex],SPDiD
        cmp     eax,0
        je      None

        mov     dword[SPDiD_Detail.Size],sizeof.SP_DEVICE_INTERFACE_DETAIL_DATA_A   ; struct dword size + 1 char byte! :/
        invoke  SetupDiGetDeviceInterfaceDetail,[hDev],SPDiD,SPDiD_Detail,DPlen,RequiredSize,0
        cmp     eax,0
        je      exit

        mov     eax,[RequiredSize]
        sub     eax,5                                                   ; 4 Bytes for Size header dword + 1 Byte zero termination
        invoke  WriteFile,[hstd],SPDiD_Detail.DevicePath,eax,NULL,NULL
        invoke  WriteFile,[hstd],CR,1,NULL,NULL


        invoke  CreateFile,SPDiD_Detail.DevicePath,0,FILE_SHARE_READ or FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0
        mov     [hFile],eax
        cmp     eax,INVALID_HANDLE_VALUE
        je      BadFile

;        invoke  HidD_GetAttributes,[hFile],HidAttribs                  ; Too simplistic in this case
;        cmp     [HidAttribs.VendorID],VID
;        jne     NotFound
;        cmp     [HidAttribs.ProductID],PID
;        jne     NotFound

        push    esi                                                     ; Need to be more specific with VID/PID as several Razer devices are generated from one mouse.
        push    edi
        cld
        mov     esi,CheckDev
        mov     edi,SPDiD_Detail.DevicePath + 3
        mov     ecx,28/4
        repe    cmpsd
        pop     edi
        pop     esi
        je      Found

 NotFound:
        invoke  CloseHandle,[hFile]
        inc     [MemberIndex]
        jmp     NextMember
 Found:
        mov     [Present],1
        jmp     None
 BadFile:
        inc     [MemberIndex]
        jmp     NextMember
 None:
        invoke  SetupDiDestroyDeviceInfoList,[hDev]
        cmp     eax,0
        je      exit

        cmp     [Present],0
        je      exit

        mov     dword[UrbBuffer+1],0x1f00                               ; Status
        mov     dword[UrbBuffer+5],GetFW                                ; Proto FW
        mov     dword[UrbBuffer+9],0                                    ; Data
        mov     dword[UrbBuffer+13],0                                   ;
        mov     dword[UrbBuffer+17],0                                   ;
        call    UrbPkt

        movzx   eax,byte[UrbBuffer.Data]
        movzx   ecx,byte[UrbBuffer.Data+1]
        movzx   edx,byte[UrbBuffer.Data+2]
        cinvoke wsprintf,_FWVer,wsFW,eax,ecx,edx
        invoke  WriteFile,[hstd],_FWVer,eax,NULL,NULL

        invoke  HidD_GetProductString,[hFile],ProdBuff,ProdBuff.len
        invoke  CloseHandle,[hFile]

 exit:
        cmp     [Present],1
        je      @f
        invoke  MessageBox,0,NotPresent,Product,0
        invoke  ExitProcess, 0
 @@:
        invoke  MessageBoxW,0,ProdBuff,ProductW,0                        ; unicode!
        invoke  ExitProcess, 0
;----------------------------------------------
 UrbPkt:
        push    esi
        xor     eax,eax
        lea     esi,[UrbBuffer+3]
        mov     ecx,86
        xor     dl,dl
 @@:                                                                    ; UrbPkt checksum
        lodsb
        xor     dl,al
        dec     ecx
        jnz     @b
        mov     byte[esi],dl

        invoke  HidD_SetFeature,[hFile],UrbBuffer,sizeof.Urb
        invoke  Sleep,1
        invoke  HidD_GetFeature,[hFile],UrbBuffer,sizeof.Urb
        pop     esi
        ret
;----------------------------------------------

 align 4
 CheckDev               db '\hid#vid_1532&pid_008a&mi_00',0
 wsFW                   db 10,'Firmware Version  %u.%02u.%02u',0
 CR                     db 10
 NotPresent             db 'Product Not Found',0
 Product                db 'Product',0
 ProductW               du 'Product',0
 Enum                   db 10,'Searching Hid Devices...',10,10
 .len                   = $ - Enum
;===========================================
section '.data' data readable writeable
 HidGuid                rd 4
 hDev                   dd ?
 hFile                  dd ?
 Present                dd ?
 hstd                   dd ?
 RequiredSize           dd ?
 BytesWritten           dd ?
 MemberIndex            dd ?
 HidAttribs             HIDD_ATTRIBUTES
 SPDiD                  SP_DEVICE_INTERFACE_DATA
 UrbBuffer              Urb
 _FWVer                 rb 10h
 SPDiD_Detail           SP_DEVICE_INTERFACE_DETAIL_DATA_A
                        rb 200h - 1
 DPlen                  =  $ - SPDiD_Detail - 4
 ProdBuff               rb 200h
 .len                   = $ - ProdBuff

;----------------------------------------
section '.idata' import data readable writeable

     library kernel32,'KERNEL32.DLL',\
             user32,'USER32.DLL',\
             Hid,'Hid.DLL',\
             Setupapi,'Setupapi.dll'

             include 'api/kernel32.inc'
             include 'api/user32.inc'

   import Hid,\
         HidD_SetFeature,'HidD_SetFeature',\
         HidD_GetFeature,'HidD_GetFeature',\
         HidD_GetHidGuid,'HidD_GetHidGuid',\
         HidD_GetProductString,'HidD_GetProductString',\
         HidD_GetAttributes,'HidD_GetAttributes'

     import Setupapi,\
        SetupDiGetClassDevs,'SetupDiGetClassDevsA',\
        SetupDiEnumDeviceInterfaces,'SetupDiEnumDeviceInterfaces',\
        SetupDiGetDeviceInterfaceDetail,'SetupDiGetDeviceInterfaceDetailA',\
        SetupDiDestroyDeviceInfoList,'SetupDiDestroyDeviceInfoList'
    
Post 06 Jul 2022, 05:19
View user's profile Send private message Reply with quote
VSH



Joined: 05 Jul 2022
Posts: 4
VSH
Thank you very much!
Quote:

Hopefully it should help.

Yes it is! Any examples are welcome.
Quote:

hid.lib are recommendations for MASM

import Hid,\...... this is it! It works in FASM, and i can call these APIs.I use Easy Code and i am very happy with it,so i will try to find solution there.
Post 06 Jul 2022, 11:08
View user's profile Send private message Reply with quote
VSH



Joined: 05 Jul 2022
Posts: 4
VSH
Hello,again.I wold like to share with you, that i ask Easy Code developers for support,and on the other day they created and send me needed files for the functions of “hid.dll”. Now everything is alright(as usual with this IDE Smile)
Many thanks to Easy Code developers for the fast and accurate support,and to the members of this forum!
Post 07 Jul 2022, 15:42
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.