C ++ Hook Function

Source: Internet
Author: User

C ++ system hook application implementation steps

Recently, I learned c ++ Hook Functions and got a lot of help from my friends. Especially bdmh, daniel is very grateful.
I deeply feel that on the way to learning c ++, I have many confidants. I am not fighting alone.
Combined with custom message ing, This is a summary of learning.
Step 1: Create a win32 dynamic link library (an empty project DLL) and name it HookDLL.

1: Add a class Named DLL. cpp.

2: Add a header file

# Include <windows. h>

# Include "DLL. h"

3: create a global variable

# Pragma data_seg ("Shared ")

HHOOK mHook = NULL;

HINSTANCE hInstance = NULL;

# Pragma data_seg ()

4: add implementation functions in DLL. cpp.

A. Hook callback function lresult winapi KeyProc (int code, WPARAM wParam, LPARAM lParam)

B. Entry Functions

Bool apientry DllMain (HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved)

C. Start and Stop hook functions for external calls

Extern "C" _ declspec (dllexport) bool winapi Start ()

Extern "C" _ declspec (dllexport) void WINAPI Stop ()

D. Custom message: # define WM_Hook WM_USER + 100

The Code is as follows:

Lresult winapi KeyProc (int code, WPARAM wParam, LPARAM lParam)

{

If (code = HC_ACTION & (lParam & 0xc000ffff) = 1)

{

Char * sName;

BOOL B _sft =: GetAsyncKeyState (VK_SHIFT) >>( (sizeof (short) * 8)-1 );

If (B _sft)

{

Switch (wParam)

{

Case '1': sName = "! "; Break;

Case '2': sName = "@"; break;

Case '3': sName = "#"; break;

Case '4': sName = "$"; break;

Case '5': sName = "%"; break;

Case '6': sName = "^"; break;

Case '7': sName = "&"; break;

Case '8': sName = "*"; break;

Case '9': sName = "("; break;

Case '0': sName = ")"; break;

Case 'A': sName = "A"; break;

Case 'B': sName = "B"; break;

Case 'C': sName = "C"; break;

Case 'D': sName = "D"; break;

Case 'E': sName = "E"; break;

Case 'F': sName = "F"; break;

Case 'G': sName = "G"; break;

Case 'H': sName = "H"; break;

Case 'I': sName = "I"; break;

Case 'J': sName = "J"; break;

Case 'K': sName = "K"; break;

Case 'l': sName = "L"; break;

Case 'M': sName = "M"; break;

Case 'N': sName = "N"; break;

Case 'O': sName = "O"; break;

Case 'p': sName = "P"; break;

Case 'q': sName = "Q"; break;

Case 'r': sName = "R"; break;

Case's ': sName = "S"; break;

Case 'T': sName = "T"; break;

Case 'U': sName = "U"; break;

Case 'V': sName = "V"; break;

Case 'W': sName = "W"; break;

Case 'X': sName = "X"; break;

Case 'y': sName = "Y"; break;

Case 'Z': sName = "Z"; break;

}

}

Else

{

Switch (wParam)

{

/* 0 ~ 9 */

Case '1': sName = "1"; break;

Case '2': sName = "2"; break;

Case '3': sName = "3"; break;

Case '4': sName = "4"; break;

Case '5': sName = "5"; break;

Case '6': sName = "6"; break;

Case '7': sName = "7"; break;

Case '8': sName = "8"; break;

Case '9': sName = "9"; break;

Case '0': sName = "0"; break;

/* ~ Z */

Case 'A': sName = "A"; break;

Case 'B': sName = "B"; break;

Case 'C': sName = "C"; break;

Case 'D': sName = "D"; break;

Case 'E': sName = "E"; break;

Case 'F': sName = "F"; break;

Case 'G': sName = "G"; break;

Case 'H': sName = "H"; break;

Case 'I': sName = "I"; break;

Case 'J': sName = "J"; break;

Case 'K': sName = "k"; break;

Case 'l': sName = "L"; break;

Case 'M': sName = "M"; break;

Case 'N': sName = "N"; break;

Case 'O': sName = "O"; break;

Case 'p': sName = "P"; break;

Case 'q': sName = "Q"; break;

Case 'r': sName = "R"; break;

Case's ': sName = "S"; break;

Case 'T': sName = "T"; break;

Case 'U': sName = "U"; break;

Case 'V': sName = "V"; break;

Case 'W': sName = "W"; break;

Case 'X': sName = "X"; break;

Case 'y': sName = "Y"; break;

Case 'Z': sName = "Z"; break;

}

}

Switch (wParam) // number key

{

Case VK_BACK: sName = "~ "; Break;

Case VK_NUMPAD1: sName = "1"; break;

Case VK_NUMPAD3: sName = "3"; break;

Case VK_NUMPAD4: sName = "4"; break;

Case VK_NUMPAD5: sName = "5"; break;

Case VK_NUMPAD6: sName = "6"; break;

Case VK_NUMPAD7: sName = "7"; break;

Case VK_NUMPAD8: sName = "8"; break;

Case VK_NUMPAD9: sName = "9"; break;

Case VK_NUMPAD0: sName = "0"; break;

Case VK_MULTIPLY: sName = "*"; break;

Case VK_ADD:

SName = "+ ";

Break;

Case VK_SUBTRACT:

SName = "-";

Break;

Case VK_DECIMAL:

SName = ".";

Break;

Case VK_DIVIDE:

SName = "/";

Break;

}

HWND H_wnd =: GetForegroundWindow ();

// Customize message sending

: SendMessage (H_wnd, WM_Hook, wParam, (LPARAM) (LPCSTR) sName );

MessageBox (H_wnd, sName, "keyboard prompt", MB_ OK );

 

}

Return CallNextHookEx (mHook, code, wParam, lParam );

}

Bool apientry DllMain (HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved)

{

Switch (ul_reason_for_call)

{

Case DLL_PROCESS_ATTACH:

HInstance = (HINSTANCE) hModule;

Break;

Case DLL_THREAD_ATTACH:

Case DLL_THREAD_DETACH:

Case DLL_PROCESS_DETACH:

Break;

}

Return TRUE;

}

Extern "C" _ declspec (dllexport) bool winapi Start ()

{

If (mHook! = NULL) return FALSE;

// The WH_KEYBOARD value is 2, and the Keyboard Message hook

// KeyProc is the callback function

// HInstance: instance

// 0: indicates a global hook.

MHook =: SetWindowsHookEx (WH_KEYBOARD, KeyProc, hInstance, 0 );

Return mHook! = NULL;

}

Extern "C" _ declspec (dllexport) void WINAPI Stop ()

{

// Hook to be detached

: UnhookWindowsHookEx (mHook );

}

5. Add the following function definitions to DLL. h:

Extern "C" _ declspec (dllexport) bool winapi Start ();

Extern "C" _ declspec (dllexport) void WINAPI Stop ();

Compile and generate HookDll. dll and HookDll. lib

Step 2:

1. Create an application in the basic dialog box of MFC, name it HookTest, and add HookDll. dll and HookDll. lib to its working directory.

2. Add the following reference to CHookTestDlg. cpp:

 

// Add a reference to the dynamic link library

# Include "DLL. h"

 

// Call the library file implicitly

# Pragma comment (lib, "HookDLL ")

 

3. Add custom messages

# Define WM_Hook WM_USER ++ 100

 

4. Add message ing

BEGIN_MESSAGE_MAP (CHookTestDlg, CDialog)
ON_MESSAGE (WM_Hook, OnReceive) // Add some
END_MESSAGE_MAP ()

 

5. Add a message declaration to CHookTestDlg. h.

Afx_msg void OnReceive (WPARAM wParam, LPARAM lParam );

 

6. Add two buttons btnStart and btnStop in the dialog box. The Code is as follows:

Void CHookTestDlg: OnbtnStartHook ()

{

// TODO: Add your control notification handler code here

Start ();

}

 

Void CHookTestDlg: OnbtnStopHook ()

{

// TODO: Add your control notification handler code here

Stop ();

}

 

7. added the onreceive implementation code of the ing function.

 

Void chooktestdlg: onreceive (wparam, lparam)
{
Cedit * medit = (cedit *) getdlgitem (idc_edit1 );
Medit-> setwindowtext (lpcstr) lparam );
}

 

Step 3:

Run the program, click btnstart, and press any key on the keyboard, such as a. a dialog box is displayed, and the text is displayed as

 

 

Source code URL:

 

Http://download.csdn.net/source/3035667

 

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.