Text / Anoop Thomas describes how to install a keyboard hook in Microsoft Windows. The hook has two types - thread special hooks and full system hooks. Thread special hooks only associate special threads (any thread owned by the call thread). If you want to associate the hook with other processes and threads, you will have to use a full system hook. A hook program is associated with a hook that will always be called when a specific event occurs. For example, the mouse that occurs when the event is associated with the mouse, this hook program will be called. The hook is installed by calling setWindowshookex (?), Deleted by calling UnHookWindowsHookex (?). For thread hooks, the hook program may be in an exe file or in a DLL. However, for global or system hooks, the hook program must exist inside a DLL. So we need to create a DLL. To do this, we use a single boot file to build a Win32 DLL project in it, and then modify it to suit your needs. You'd better write code for installation and delete hooks in the DLL. Now, in the DLL header file, the function is as defined below: #ifdef keydll3_exports # define keydll3_api __declspec (dllexport) # Else # define keydll3_api __declspec (dllimport) #ENDIF
// this function installs the keyboard hook: keydll3_api void installhook (hwnd h);
// this function removes the previously installed hook.keydll3_api void removehook ();
// hook procedure: Keydll3_API LRESULT CALLBACK HOOKPROC (INT NCODE, WPARAM WPARAM, LPARAM LPARAM); For the output function in the DLL, use the __declspec and the dllexport keyword to be a good idea, it is better than using a separate DEF file. . SetwindowshooKex () Returns a handle to the hook - this hook is prepared for unloading hooks from the hook chain. We also have a window handle, we will use it to send messages to the main application window. We first use the FindWindow () function to find the application window, then use the PostMessage () call to send the button message parameter to the primary window, like the program code snippet below: // Find Application WINDOW HANDEHWND = FINDWINDOW ("# 32770" "Keylogger EXE");
// send Info to App Window.PostMessage (HWND, WM_USER 755, WPARAM, LPARAM); At the end of the hook program, we must call the callNextHooKex () function to pass the parameters to the hook installed in the next hook chain. I highly recommend this method because if it is not used, it will cause unpredictable system behavior and system locking. To install the program remove the hook, the hook procedure as follows: KEYDLL3_API void installhook (HWND h) {hook = NULL; hwnd = h; hook = SetWindowsHookEx (WH_KEYBOARD, hookproc, hinstance, NULL); if (hook == NULL) MessageBox (NULL, "Unable to Install Hook", "Error!", MB_OK;} keydll3_api void transovehook () {unhookwindowshookex (hook);
KEYDLL3_API LRESULT CALLBACK hookproc (int ncode, WPARAM wparam, LPARAM lparam) {if (ncode> = 0) {// Find application window handle hwnd = FindWindow ( "# 32770", "Keylogger Exe"); // Send info to app Window. Postmessage (HWND, WM_USER 755, WPARAM, LPARAM);} // Pass Control to next hook. Return (CallNexthookex (Hook, Ncode, WPARAM, LPARAM);} If there is multiple DLLs in memory, Every data member of the situation of different DLLs has different values. However, some data, such as hook handles, window handles should be the same. This is because all situations send the same information to the same application window. For this regard, we need to define shared data as in the DLL's CPP file. Like below: #pragma data_seg (". Hookdata") // shared data among all instances.hhook hook = null; hwnd hook = null; #pragma data_seg () Now, the connector must be given to the command to place the shared data In a separate space of the DLL. To do this, we use the following code, later is the code mentioned above: // Linker Directive # pragma Comment (Linker, "/SECTION :~HOOKDATA) So much about the DLL, now we will look at it Main Application (exe). Create an MFC application (window or dialog-based). For simplicity, I have established a dialog-based EXE file. After creating this project, go to the Project Settings dialog box by selecting Project> Settings from the main menu, select the "LINK" table, and then display "keydll3.lib" in the "Object / library modules" box, click "OK". Now, select Project> Add to Project> Files from the main menu to insert the DLL header file to the workspace. Choose the DLL of our earlier .H file, like it "#include" in your project like the following: // include this for functions in the dll: #include "../keydll3/keydll3.h" should In the CPP file of the primary dialog class. Now, in the class of the master dialog, add a member function to process the buttons sent by the DLL. The function is as follows: AFX_MSG LRESULT ProcessKey (WPARAM W, LPARAM L); // Declaration