I'm playing with hooks and messages and I have a code like:

.code

Invoke CreateWindowEx, NULL, ADDR CLASSNAME, NULL,
0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
Mov MHWND, EAX

[...]

Invoke SetWindowHookEx, ..., etc...

Bucle:
Invoke GetMessage, ADDR MESG, NULL, 0, 0
Invoke DispatchMessage,ADDR MESG
"More code"

Jmp Bucle


HookProc PROC NCODE:DWORD, WPARAM:DWORD, LPARAM:DWORD

[...]
Invoke SendMessage, ..., etc...
[...]
HookProc EndP


The idea that I'm trying is that the main program be "blocked" (with GetMessage) until it recives a message that the hook sends. After that the main program executes the "More Code". I have tryied with different messages but the main program is always blocked in the GetMessage. Can anyone help me, where is the problem? What message I must send? How I can solve? Thanx

:alright:
Posted on 2002-09-22 02:22:40 by wildark
The problem is that SendMessage never places messeges in message queue.
It simply calls window procedure as subroutine. So, GetMessage doesn't return because of there is no new messages in message queue. Use PostMessage instead.
BTW, from code you have posted, it's not clear where your hook proc resides, it must be in DLL. But i guess you know that.
Posted on 2002-09-22 03:36:49 by Four-F
Ok here is my code:

.code
Start:
Invoke CreateWindowEx, NULL, ADDR CLASSNAME, NULL,
0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
Mov MHWND, EAX

Invoke GetModuleHandle,NULL
Invoke SetWindowsHookEx, WH_JOURNALRECORD, ADDR HookProc, EAX, NULL
Mov HHANDLE, EAX

Main:
Invoke GetMessage, ADDR MESG, NULL, 0, 0
Invoke DispatchMessage,ADDR MESG
Invoke MessageBox, 0, ADDR Text, ADDR Text, MB_OK
Jmp Main


HookProc PROC NCode:DWORD, WParam:DWORD, LParam:DWORD

Pushfd
Pushad

Cmp NCode, HC_ACTION
JnZ NextHook
Mov EAX, LParam
Cmp DWORD PTR [ EAX ], WM_KEYDOWN
JnZ NextHook
; A key has been pressed

; Here is where I would like to send a message to the "main" program
; and the main program shows the Message Box


NextHook:
Popad
Popfd
Invoke CallNextHookEx, HHANDLE, NCode, WParam, LParam
Ret

HookProc EndP


I have tryied with PostMessage but now the programs crash... I don't know what I'm doing wrong. Is the message the problem? What message I would must send? Can anyone help me?
Posted on 2002-09-22 04:30:59 by wildark
OK. More clearer now.

1. Don't use WH_JOURNALRECORD/WH_JOURNALPLAYBACK hooks if you are not quite sure for which purpose they are. It's special case. I guess it's not that you want. In your code you want to monitor messages posted to a message queue. So, use WH_GETMESSAGE hook.

2. Invoke GetModuleHandle, NULL returns the base address of the executable file not base of dll even if you call it from code that is contained inside a DLL! But SetWindowsHookEx expects HINSTANCE of DLL in which hook procedure resides if you install system-wide hook.

3. To send some specific message you can define your own user message.
WM_MYMESSAGE equ WM_USER+1
And post it. Change your message loop like this:
Main:

Invoke GetMessage, ADDR MESG, NULL, 0, 0
.if MESG.message != WM_MYMESSAGE
Invoke DispatchMessage,ADDR MESG
.else
Invoke MessageBox, 0, ADDR Text, ADDR Text, MB_OK
.endif
Jmp Main

This way you can decide which message GetMessage returns, and handle it as needed.

4. If your MessageBox is not for debugging purpose, it hangs your message loop,
and can overflow message queue.

5. Once more. Your hook procedure must reside in DLL.
From code you have posted i can see it resides in you main executable.

Excuse me, wildark, but too many errors. Do you understand what you are doing?
Are you sure you need to install hook to achieve your purpose?

May be you should read Icz's tut about hooks if you haven't read it before, and play with its code.
Posted on 2002-09-22 05:31:03 by Four-F
Mmm yes... I'm really newer :( I would must read the tutorials again... I have a lot of errors :'''( Thanx!!!!
Posted on 2002-09-22 06:01:39 by wildark