With the development of middleware technology, DLL is increasingly focusing on programmers because the use of DLL has a series of advantages, so programming can use this technology in their own software.
Below I will analyze a simple full keyboard hook you have done.
Hook [Herek] is a mechanism for monitoring a message stream in the Microsoft Windows message processing process to monitor message flows and has not yet reached a window in the processing system. If the hook process is implemented in the application, if the application is not the current window, the hook does not work; if the hook is implemented in the DLL, the program is dynamically called it in the run, which can monitor the system in real time. As needed, we use the way to implement hook in the DLL [About Hook More detailed information, please refer information].
In the VC, a new Win32 Dynamic-Link Library project is called Kblock. AppWizard generates related files, compiles the generated kblock.cpp:
#include "stdafx.h"
#include "kblock.h"
HHOOK hHKHOOK = NULL; / / Define hook handle
Hinstance hinstance = null; // Program instance
// The following DLLMAIN is equivalent to the WinMain function in the Win32 program, which is the entry point.
Bool apientry dllmain (Handle Hmodule,
DWORD UL_REASON_FOR_CALL,
LPVOID LPRESERVED
)
{
Switch (ul_reason_for_call)
{
Case DLL_Process_attach:
Case DLL_THREAD_ATTACH:
Case DLL_THREAD_DETACH:
Case DLL_PROCESS_DETACH:
Break;
}
Hinstance = (hinstance) hmodule; // Get a DLL instance
Return True;
}
// This is the main function of processing the keyboard message, and prohibits the operation.
LResult Callback HookProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
IF (ncode <0)
{
Return CallNexthookex (HHKHOOK, NCODE, WPARAM, LPARAM);
}
IF (ncode! = hc_action)
{
Return CallNexthookex (HHKHOOK, NCODE, WPARAM, LPARAM);
}
/ / Give a prompt: The keyboard has been locked, to be judged, see if there is a prompt window, otherwise it will be bounced
IF (! :: findwindow (0, "keyboard locked")))
{
:: MessageBox (0, "keyboard is locked !!!", "keyboard locked", MB_OK;
}
Return 1; // No Return CallNexthooKex (HHKHOK, NCODE, WPARAM, LPARAM) will not pass the message //, so our keyboard does not work.
}
// this is an esample of an exported variable
// Export function: Start the keyboard lock
BOOL EnableKeyboardCapture ()
{
IF (! (hh_keyboard, (hookproc) hookproc, hinstance, 0)) Return False;
Return True;
}
// Export function: Release the keyboard lock
Bool disableKeyboardCapture ()
{
Return UnhookWindowshookex (hhkhook);
}
The above is the most important code in the DLL, of course, to make the DLL can work normally to edit the KBLock.h file:
__declspec (dllexport) BOOL EnableKeyboardCapture (); // Loading hook
__Declspec (DLLEXPORT) BOOL DisableKeyboardCapture (); // Uninstall hook
Edit Kblock.def again
Kblock.def: Declares The Module Parameters for the DLL.
Library "Kblock"
Description 'KBlock Windows Dynamic Link Library'
Exports
Explicit Exports Can Go Here
EnableKeyboardCapture @ 1
DisableKeyboardCapture @ 2
This way we will find these two export functions when we look at this DLL with Depends.exe.
The work in DLL has been completed so that we can call it in the program.
Although the DLL is developed by VC, the front desk program that calls it can be implemented with any other language that supports DLL calls such as: VB, VC, Delphi, Win32ASM implementation, the following or VC is an example of implementing the DLL call.
Built a Dialog-based project, join two buttons: "Lock Keyboard" "Unlock"
Add a member function in the CEXEDLG class:
/ * sign = true lock Sign = false unlocked * /
Bool Cexedlg :: Kblock (Bool Sign)
{
HDLL = :: LoadLibrary ((lpctstr) "kblock"); // Loading DLL
IF (HDLL! = NULL)
{LoadHook = (LoadHook) :: getProcaddress (HDLL, "EnableKeyBoardCapture);
UnloadHook = (UnloadHook) :: getProcaddress (HDLL, "DisableKeyboardCapture);
IF (LoadHook == Null || unloadHook == null)
{:: MessageBox (0, "Sorry, this feature cannot be used !!!", "somthing wrong", mb_ok);
Return 0;
}
IF (SIGN)
Loadhook ();
Else
{
UnloadHook ();
:: Freelibrary (HDLL);
}
Return 1;
}
:: MessageBox (0, "Dynamic Library Load Failed !!!", "Somthing Wrong", MB_OK;
Return 0;
}
Among them, the global variables defined in advance:
TypeDef Bool (Callback * Loadhook ();
TypeDef Bool (Callback * UnloadHook (); Hinstance HDLL = NULL;
Loadhook loading;
Unloadhook unloadhook;
This way we join KBlock (TRUE) in two buttons; and Kblock (false);