khanh1984
Joined: 13 Jul 2009
Posts: 17
|
Hi everybody,
Today I'm trying to code a very simple application that simply demonstrated how to use keyboard hook and started to use LoadLibrary and GetProcAddress API function. However, when I make a call to GetProcAddress, the 1st function was retrieved sucessfully but not the second. After examing the symbol name in the dll file, I'v found them to be still correct. So where the thing goes wrong ?
I include here the source code of both DLL file and main application. You can compile it right away and see it yourself.
DLL CODE
format PE GUI 4.0 DLL
entry DllMain
include "win32a.inc"
include "\macro\if.inc"
section '.data' data readable writeable shareable
msg1 db "Ko bam phim xuong",0
msg2 db "Call nexthook",0
hook dw 0
section '.code' code readable executable
proc DllMain hinstDLL,fdwReason,lpvReserved
mov eax,TRUE
ret
endp
proc KBHookSetup DLL_module
mov eax,[DLL_module]
invoke SetWindowsHookEx,WH_KEYBOARD,KBHookFilter,dword [eax],0
mov dword [hook],eax
ret
endp
proc KBHookFilter keycode,wParam,lParam
mov ebx,[lParam]
shr ebx,31
mov eax,[keycode]
.if eax = HC_ACTION
.if ebx = 0
.if [wParam]=30h
invoke MessageBox,NULL,msg1,NULL,MB_OK
.endif
.endif
.else
invoke MessageBox,NULL,msg2,MB_OK
mov eax,[keycode]
mov ebx,[wParam]
mov ecx,[lParam]
invoke CallNextHookEx,dword [hook],eax,ebx,ecx
ret
.endif
xor eax,eax
ret
endp
section '.edata' export data readable
export 'HOOKDLL.DLL',\
KBHookSetup,'KBHookSetup',\
KBHookFilter,'KBHook'
section ".idata" import data readable writeable
library kernel32,'kernel32.dll',\
user32,'user32.dll'
import user32,\
MessageBox,'MessageBoxA',\
SetWindowsHookEx,'SetWindowsHookExA',\
CallNextHookEx, 'CallNextHookEx'
section '.reloc' fixups data discardable
Main Application code
include 'win32ax.inc'
include 'rc.inc'
.data
msg MSG
nResult dw 0
hModule dw 0
hook dw 0
Dll_module dw 0
_dll_hook_setup dw 0
_dll_hook_filter dw 0
error db 1,2,3,4
db "can't load the module",0
kbh db "KBHookSetup",0
kbf db "KBHookFilter",0
.code
start:
invoke GetModuleHandle,NULL
mov dword [hModule],eax
invoke DialogBoxParam,eax,IDD_DLG1001,NULL,DLG_Function,NULL
.if eax = 0
mov dword [nResult],eax
invoke MessageBox,NULL,"Hi! I'm the example program!","Win32 Assembly",MB_OK
.endif
invoke UnhookWindowsHook,[hook]
invoke ExitProcess,0
proc DLG_Function,hwnd,uMsg,wParam,lParam
mov eax,[uMsg]
cmp eax ,WM_DESTROY
jz .wm_destroy
cmp eax,WM_COMMAND
jz .wm_command
cmp eax,WM_CLOSE
jz .wm_close
cmp eax,WM_INITDIALOG
jz .wm_initdialog
jmp .default
.wm_initdialog:
invoke LoadLibrary,"C:\HOOKDLL.DLL"
mov dword [Dll_module],eax
.if eax = 0
invoke MessageBox,NULL,"the specified modulesss could not be found",NULL,MB_OK
invoke ExitProcess,0
.endif
mov ecx,dword [Dll_module]
invoke GetProcAddress,ecx,"KBHookSetup"
mov dword [_dll_hook_setup],eax
.if eax=0
invoke MessageBox,NULL,"Can't get the function address setup",NULL,MB_OK
invoke FreeLibrary,[Dll_module]
invoke ExitProcess,NULL
.endif
mov ecx,dword [Dll_module]
invoke GetProcAddress,ecx,"KBHook"
mov dword [_dll_hook_filter],eax
.if eax = 0
invoke MessageBox,NULL,"Can't get the function address filter",NULL,MB_OK
invoke FreeLibrary,[Dll_module]
invoke ExitProcess,NULL
.endif
invoke _dll_hook_setup,Dll_module
mov dword [hook],eax
jmp .exit
.wm_destroy:
invoke PostQuitMessage,,NULL
jmp .exit
.wm_command:
mov eax, [wParam]
.if ax = IDC_BUTTON_EXIT
shr eax,16
.if ax=BN_CLICKED ;click exit buttion
mov eax,[hwnd]
invoke DestroyWindow,[hwnd]
invoke FreeLibrary,[Dll_module]
.endif
.endif
jmp .exit
.wm_close:
mov eax,[hwnd]
invoke EndDialog,eax,nResult
jmp .exit
.default:
mov eax,FALSE
ret
.exit:
mov eax,TRUE
ret
endp
.end start
section '.rsrc' data readable resource from 'rc.res'
|