如何获得键盘设备的句柄?

时间:2017-04-07 11:19:58

标签: windows assembly keyboard x86-64

我正在尝试挂钩键盘ISR,但我无法打开设备,因为 CreateFile返回0000007Bh ERROR_INVALID_NAME。我做错了什么?

invoke DefineDosDevice,[raw],filename1,devicename
lea     rcx,[filename2]
invoke  CreateFileA,rcx,GENERIC_ALL,FILE_SHARE_WRITE or 
FILE_SHARE_READ,0,3,0,0
ret

section '.data' data writeable readable

devicename      db '\\Device\\KeyboardClass0',0
filename1       db 'keyboard',0
filename2       db '\\.\keyboard',0
raw             dq      1

1 个答案:

答案 0 :(得分:0)

这是平坦的汇编语法,你应该在不转义反斜杠的情况下传递devicename

devicename      db '\Device\KeyboardClass0',0

关于如何在Windows中访问键盘设备,还有一个很好的tutorial with source code in C。你可能会发现它很有趣。

将其翻译为程序集,您需要执行类似

的操作
include 'include\win64ax.inc'

.code 

start:
  invoke DefineDosDevice, 1, kbdFileName, kbdDeviceName
  mov rcx, kbdPath
  invoke  CreateFile, rcx, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0
  mov [hKbd], rax
  ;. . .
  invoke ExitProcess, 0

.end start 

.data 
kbdDeviceName  db '\Device\KeyboardClass0',0
kbdFileName    db 'kbd',0
kbdPath        db '\\.\kbd',0
hKbd           dq ?