I’ve been looking into low level keyboard hooks lately. I have already got mine working but I have some questions on the code.
I see there is a complete code for Console application here:
http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
In the Main function, I see the programmer called a “UnhookWindowsHookEx” DLL function. What is the purpose of this function and what would happen if it is commented out? (Bad consequences especially?)
EDIT:
And one more thing. Why must “Application.Run()” be called in order for the code to work?
In this particular case, if UnhookWindowsHookEx is commented, nothing bad happens, because the program exits anyway. But it is a good idea to close any resource when it is not required anymore, both when the program continues executing, and in the end of the program – to keep resources usage under the control.
Without Application.Run the program exits immediately. The second reason for using Application.Run is SetWindowsHookEx requirement – it needs message loop to work. Application.Run executes the message loop. Program may be stopped by pressing Ctrl+C.