Hook Technology-satisfying our program's Perspective (2)

Source: Internet
Author: User
Tags sendmsg

Next, let's talk about the hook instance for an example.

This example sets a global hook to capture otherProgram.

First, build the DLL to map to other programs. The DLL file name is keyhook. h/keyhook. cpp.

In keyhook. H, two functions are exported, namely the function for setting and canceling the hook.

// ------------------------------------ Keyhook. h -----------------------------------//

# Ifndef expert
# Define Expert extern "C" _ declspec (dllimport)
# Endif

Expert bool sethook (hwnd ); // Set hook
Expert bool cancelhook (hwnd ); // Cancel hook

# Define Wm_key_hook wm_user + 102 // Custom message: When this DLL intercepts the Keyboard Message of another program, it sends the message to our application. // ------------------------- Keyhook. cpp --------------------- //

# Define Expert extern "C" _ declspec (dllexport)

// Define the DLL shared data segment so that the data in this segment can be rewritten by this application.
# Pragma data_seg ( " Hookshare " )
Hwnd hwndserver = NULL;
# Pragma data_seg ()
# Pragma comment (linker, " /Section: hookshare, RWS " )

Hinstance hinst; // DLL instance handle
Hhook hook; // Hook handle

Lresult winapi wndhookproc ( Int Ncode, wparam, lparam ); // Once the hook is mounted, the wndhookproc will receive messages from other applications.

Bool apientry dllmain (handle hmodule, DWORD ul_reason_for_call, lpvoid lpreserved)
{
Switch (Ul_reason_for_call)
{< br> case dll_process_attach:
hinst = (hinstance) hmodule;
return true;
// you need to use the DLL instance handle to set hooks later. Therefore, hmodule is assigned to the global variable hinst.
case dll_process_detach:
cancelhook (hwndserver);
return true;
}
Return True;
}


Expert bool sethook (hwnd)
{
If (Hwndserver = Null) // Have't hook
{
Hook = : Setwindowshookex (wh_getmessage, wndhookproc, hinst, 0 );
// Sets a global hook. This hook captures messages and transmits them to wndhookproc for processing. Hinst is the instance handle that saves the DLL. The last parameter is the thread ID. Set 0 to global hook.

If (Hook ! = Null)
{
Hwndserver=Hwnd;
//Assign the window handle of our application to hwndserver. In this way, we have a destination for sending messages in the DLL (that is, the window to which the messages are sent after the messages are intercepted ).
ReturnTrue;
}
Else
Return False;
}
Else
Return False;
}

Expert bool cancelhook (hwnd)
{
If (Hwndserver ! = Null) // Is hook
{
If (Unhookwindowshookex (Hook ))
{
Hwndserver=NULL;
ReturnTrue;
}
Else
Return False;
}
Else
Return False;
}

Lresult winapi wndhookproc ( Int Ncode, wparam, lparam)
{
If (Ncode < 0 )
{
: Callnexthookex (Hook, ncode, wparam, lparam );
Return 0;
}

Lpmsg = (Lpmsg) lparam;
If (Lpmsg -> Message = Wm_keydown)
{
//If the message is a wm_keydown message, send our custom message to our application window (hwndserver)
: Postmessage (hwndserver, wm_key_hook, lpmsg->Wparam, lpmsg->Lparam );
Return: Callnexthookex (Hook, ncode, wparam, lparam );
}
Return   0 ;
}

Next, we need to write our application to process the messages intercepted by DLL. Assume that our application is handled by a dialog box.
The file names in the dialog box are sendmsg. h sendmsg. cpp.

// ------------------ Sendmsg. h --------------------- //

// Add two buttons: hook and unhook. Their processing programs are onhook and onunhook.
Afx_msg Void Onhook ();
Afx_msg Void Onunhook ();
// Custom message. When the window receives the wm_key_hook message, the onhookmsg function is used for processing.
Afx_msg lresult onhookmsg (wparam, lparam ); // ------------------ Sendmsg. cpp ------------------- //
# Include " Sethook. h "   // Add the DLL. h file to the project.

// Custom message ing
On_message (wm_key_hook, onhookmsg)

// When you click the hook button, set the hook
Void Csendmsg: onhook ()
{
If ( ! M_ishook)
{
Bool ishook = Sethook (m_hwnd ); // Set the hook to map the DLL to the address space of other processes.
If (Ishook)
{
M_hook.enablewindow (false );
M_unhook.enablewindow (true );
M_ishook=True;
}
}
}


// When you click the unhook button, cancel the hook
Void Csendmsg: onunhook ()
{
// Todo: add your control notification handler code here
If (M_ishook)
{
Bool ishook = Cancelhook (m_hwnd );
If (Ishook)
{
M_hook.enablewindow (true );
M_unhook.enablewindow (false );
M_ishook=False;
}
}
}


Lresult csendmsg: onhookmsg (wparam, lparam)
{
//Here, when the keydown message is received in other processes, it is transferred here for processing.
//You can obtain the value of the keyboard here, or change the keydown before forwarding.
//It is relatively simple to simply stop writing.
Return 0;
}

In addition, there are other precautions during compilation. If we want to compile this application, we need to compile it with the DLL and tell the compiler that the dependencies of this project are keyhook. DLL (menu: Project-> Project dependency)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.