tags:

views:

399

answers:

3
A: 

GetModuleHandle() is a Windows API which in simple word returns you the handle of the loaded DLL or EXE.

You can see the detailed description of this API at this link

Straight From MSDN:

The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. Therefore, use care when passing the handle to the FreeLibrary function, because doing so can cause a DLL module to be unmapped prematurely.

This function must be used carefully in a multithreaded application. There is no guarantee that the module handle remains valid between the time this function returns the handle and the time it is used. For example, a thread retrieves a module handle, but before it uses the handle, a second thread frees the module. If the system loads another module, it could reuse the module handle that was recently freed. Therefore, first thread would have a handle to a module different than the one intended.

Aamir
Being new to C# I cannot understand what "module" and "handle" exactly mean.I have a vague idea but it would be great if you explain in brief what they mean with regards to the given context..
5lackp1x3l0x17
Module handle is not a C# or .NET concept. It's from the Win32 API, and in fact, you don't need to understand them. It's a parameter to SetWindowsHookEx, that's all you need to know.
John Saunders
A: 

First as a general point for questions on Windows library functions, you should consider searching MSDN. Here is the MSDN page for GetModuleHandle(), it contains much of the relevant information.

To your question (and anyone more familiar with the Windows API feel free to correct me), a "module" is a sort of catch-all term for a program in Windows, usually specifically referring to either an executable(.exe) or a library(.dll). A "handle" is a term referring to a pointer. GetModuleHandle() returns a pointer (handle) to the specific program (module). As Pavel commented, both are very broad terms.

As for the code snippet you posted:

It's getting the current running process, as well as the current module (obvious.) It is then calling SetWindowsHookEx (refer to the MSDN for more information) which takes in the event to hook (In this case, low level Keyboard events.), a procedure to call (proc) when the hooked event happens, and a pointer to the current program.

Refer to Hooks on the MSDN for more information on hooking.

Essentially the message of this post is make more use of the MSDN, it's a pretty solid piece of documentation :)

Falaina
Yes.. Thankyou.. things have become clearer in terms of concepts..
5lackp1x3l0x17
+1  A: 

In short this code initializes a key logger. The passed in parmeter, proc, is a callback function that will be called on every key press.

The using statement just ensures immediate calls to dispose() on the declared variables (curProcess and curModule) when they leave scope, thus properly releasing resources in a expedient manner rather than waiting for the garbage collector to release them which may take a while.

SetWindowsHookEx is a win32 api call that allows you to register a callback to be called when a specific OS level event occurs. In this case the first parameter, WH_KEYBOARD_LL, specifies that you would like to register for low level keyboard events. The second parameter is the callback (a Delegate in .Net) that will be called. The third parameter is a windows handle (a pointer managed by the OS) to the module where the callback is located, in this case the main .exe for the process. Note that a process has multiple modules (exe's or dll's) loaded at any given time. The last parameter is the thread id that you would like to monitor; because 0 is passed in, the callback will be called for any key events for any window opened on the OS.

More info here about SetWindowsHookEx

Darwyn