GetMessage with lpMsg NULL, but it works - why?

Found a very simple code example
which is working, but why?

1
2
3
4
5
6
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHook, hInstance , 0);
	while (GetMessage(NULL,NULL,0,0)) ;
	return UnhookWindowsHookEx(hHook);
}


Do you know what really happens if the pointer lpMsg is NULL?

Thank you for clarifications

Erhy
It's simply telling the program to continue running until it is requested to quit.
You could also write, say, while(1){} but then the Hooks will not receive messages, and the program will use an entire (virtual) CPU core (e.g. Takes 100% on a single core, 50% on a dual core, 25% on a quad core...).

If lpMsg is NULL, it means we don't want the actual message.
We just need the return value to know when we are requested to quit.
and how/why the CALLBACK is reached?
1
2
3
4
5
6
7
LRESULT CALLBACK KeyboardHook (int nCode, WPARAM wParam, LPARAM lParam)
{
	if (nCode == HC_ACTION)
		if (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN || wParam == WM_SYSKEYUP || wParam == WM_KEYUP)		
			CheckKey (nCode, wParam, lParam);
	return CallNextHookEx(hHook, nCode, wParam, lParam);
}

I thought all CALLBACKs are via messages.

Erhy
GetMessage handles hook-callback messages.
The specific callback is known since the SetWindowsHookEx call.
Note: A (hook) callback message is not like a windows message:
Translate/DispatchMessage does not apply here.
Last edited on
Thank You so much!
I will try to add the hook code in a windows app with normal message loop.
Hope, there are no problems.
Erhy
Registered users can post here. Sign in or register to post.