Hi,

I'm trying to understand this code. This is the message loop in WinMain, it keeps asking for messages.

.WHILE TRUE
invoke GetMessage, ADDR msg,0,0,0
.BREAK .IF (!eax) ; Break if no message
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW

I have a few questions:

1. Is GetMessage an API call? Is there one big global message queue, managed by Windows, where all messages are stored, with the handles of the windows they are sent to? Or many message queues, one for every application?

2. Why does it "break if no message" (eax == 0)? If there is no message waiting the window is supposed to just stay there, not close! :confused:

3. What does TranslateMessage do?

4. What does DispatchMessage do?
Posted on 2003-09-05 02:44:01 by alexp
GetMessage: This function retrieves a message from the calling thread?s message queue and places it in the specified structure.

Nonzero indicates that the function retrieves a message other than WM_QUIT. Zero indicates that the function retrieves the WM_QUIT message, or that lpMsg is an invalid pointer. ?1 indicates that an error occurred?for example, the function fails if hWnd is an invalid window handle. To get extended error information, call GetLastError.

TranslateMessage: This function translates virtual-key messages into character messages. The character messages are posted to the calling thread?s message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.

DispatchMessage: This function dispatches a message to a window procedure. It is typically used to dispatch a message retrieved by the GetMessage function.

MSDN is your friend, use it :)
Posted on 2003-09-05 05:53:21 by Beefy
Hi, alexp !

1. GetMessage is API call
l don't remember :( about message queue, but applications can SendMessage to window or windows

2. "If GetMessage function retrieves the WM_QUIT message, the return value is zero. "
so if EAX==0 then window closes
If there is no message waiting the window isn't supposed then EAX!=0, window must "filter" all needed messages and after it
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
If there no messages for window then GetMessage "waits" instead of PeekMessage. But still these calls are optional (if you do not want to close window, for example)

3&4. Thise functions used for standard window management.

This comes from Win16 "multitasking", IIRC

I'm sometimes just using this (not C-like) for my dummy window:
Console_Loop	proc

LOCAL msg:MSG
@msg equ ESP
@1: mov ESI, @msg
invoke GetMessage, ESI, NULL, 0, 0;
@2: cmp MSG.message[@msg], WM_QUIT
je @3
; invoke TranslateMessage, @msg ; no keyboard input ;D
invoke DispatchMessage, @msg
jmp @1
@3: ; exit console
mov eax, MSG.wParam[@msg] ; save returned value in eax
invoke ExitThread,0
; ret
Console_Loop endp
Posted on 2003-09-05 06:24:44 by S.T.A.S.