I have read the msdn link, and tried to approach the GetMessage() function in the following keylogger-code .
In my smallest version of a program attached below, why isn't GetMessage() releasing and print "new message" if I press keyboard or resize window?
#include <stdio.h>
#inlcude <stdlib.h>
#inlcude <windows.h>
int main() {
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) != 0) {
printf("\nnew message!");
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
UPDATE: As you mentioned I gave the process a window(handle) and it worked fine during I kept the GetMessage() within the WinMain. Because there should be other functionality I need to outsource the GetMessage() to its own thread as shown below. Unfortunately the GetMessage() function hangs up again, even though I specify the window-handle for which the messages should be recieved within its parameters. Any hints to get me further in understanding this function?
void control(HWND hwnd) {
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) != 0) {
printf("\nnew message!");
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// window class creation
const char window_name[] = "myWindow";
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = window_name;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// register the class
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// window creation
HWND hwnd;
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, window_name, "The Window Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// show window
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// threading
HANDLE thread
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE*) control, hwnd, 0, NULL);
WaitForSingleObject(thread, INFINITE);
return 0;
}