(Transfer)
First, create a dynamic link library.
File Name dllhook. cpp
The Code is as follows:
# Include "windows. H"
Hinstance h_st;
Hhook g_keyboardhook;
Hhook g_mousehook;
Hwnd g_wnd;
/* Bool winapi dllmain (
Hinstance hinstdll, // handle to the DLL module
DWORD fdwreason, // reason for calling function
Lpvoid lpvreserved // Reserved
)
{
H_st = hinstdll;
// Return 1;
}*/
Lresult callback mouseproc (
Int ncode, // hook code
Wparam, // message identifier
Lparam // mouse coordinates
)
{
Return 1;
}
Lresult callback keyboardproc (
Int code, // hook code
Wparam, // virtual-key code
Lparam // keystroke-message information
)
{
If (vk_f2 = wparam)
{
: Sendmessage (g_wnd, wm_close, 0, 0 );
Unhookwindowshookex (g_mousehook );
Unhookwindowshookex (g_keyboardhook );
}
Else
{
Return 1;
}
}
Void sethook (hwnd)
{
G_wnd = hwnd;
G_mousehook = setwindowshookex (wh_mouse, mouseproc, getmodulehandle ("hookdll"), 0 );
G_keyboardhook = setwindowshookex (wh_keyboard, keyboardproc, getmodulehandle ("hookdll"), 0 );
}
Then, create a hookdll. Def file under the directory file of the project and use it as a module for export.
Use VC to import this file
Project-> Add project-> files import this file to the Project
The code for this file is as follows:
Library hookdll
Exports
Sethook @ 3
The next step is to create an MFC application. Create a dialog box application named dllhook
Find the onindialog () function under classview,
Make changes