In-depth analysis of hooks and dynamic link libraries (medium)

zhaozj2021-02-16  102

In-depth resolution hooks and dynamic link libraries (in) The first thing you have to do is to create a common data segment. So we use the # pragma data_seg declaration. Use a good data segment name (it must be no longer than 8 characters). I want to emphasize the names anything, here I use my own name. I found that if I use a good name. SHARE or .shr or .shrdata, others will think that the name has special meaning. But I want to say NO.

# prgma data_seg (". Joe")

Handle hwnd = NULL;

# Pragma DTA_SEG ()

# Pragma Comment (Linker, "/ Section: .joe, RWS")

# PRAGMA declares a data segment, the variable declared within this range will be assigned to the data segment after initialization, assume that they initialize. If it is not initialized, the variable will be assigned to the default data segment, while #prgma does not work.

It looks at it, which will prevent you from using some C objects in a common data segment, because you can't initialize the user-defined object in C . This seems to be an fundamental limit.

# Pragma Comment Enables the connector to have a command line switch to be displayed to the link step. You can enter the VC project | Settings and change the connector command line.

You can schedule a mechanism to set the window handle, for example

Void setWindow (hwnd w) {hWnd = W;

But more often is combined with the hook as shown below.

Sample: a mouse hook header file (myhook.h) function setMyhook and ClearMyHook must be declared here. This is discussed in detail in my other article. "The Ultimate DLL Header File."

#define uwm_mousehook_msg /

_T ("umw_mousehook-" /

"{B30856F0-D3DD-11D4-A00B-006067718D04}")

Source file (myhook.cpp)

#include "stdafx.h"

#include "myhook.h"

#pragma data_seg (". Joe")

HWND HWNDSERVER = NULL;

#pragma data_seg ()

#pragma comment ("Linker, /SECTION:: JOE ,RWS")

Hinstance hinstance;

Uint hwm_mousehook;

Hhook hook;

// forward decaration

Static Lresult Callback Msghook (Int Ncode, WPARAM WPARAM, LPARAM LPARAM);

/ ************************************************** ****************

* DLLMAIN

* INPUTS:

* Hinstance Hinst: Instance Handle for the DLL

* DWord Reason: Reason for Call

* LPVOID Reserved: ignored

* Result: bool

* True if Successful

* False if there is an error (never returned)

* Effect:

* Initializes The DLL.

*********************************************************** ************** / BOOL DLLMAIN (Hinstance Hinst, DWord Reason, LPvoid ​​Reserved)

{

Switch (REASON)

{/ * REASON * /

// ************************************************************

// process_attach

// ************************************************************

Case DLL_Process_attach:

// Save the instance handle becault we need it to set the hook lat

Hinstance = hinst;

// this code initializes the hook notification message

UWM_MouseHook = RegisterWindowMessage (UWM_MOUSEHOK_MSG);

Return True;

// ************************************************************

// process_detach

// ************************************************************

Case DLL_PROCESS_DETACH:

// if The Server Has NOT Unhooked The Hook, UnHook It As We unload

IF (HWNDSERVER! = NULL)

ClearMyHook (HWNDSERVER);

Return True;

} / * REASON * /

/ ************************************************** ****************

* SETMYHOOK

* INPUTS:

* HWND HWND: Window whose hook is to be set

* Result: bool

* True if the hook is protly set

* False if there is an error, Such as the hook already

* Being Set

* Effect:

* Sets the hook for the specified window.

* This sets a message-intercept hook (wh_getMessage)

* If the setting is successful, the hwnd is set as the

* Server window.

*********************************************************** ************* /

__Declspec (DLLEXPORT) BOOL WINAPI SETMYHOOK (HWND HWND)

{

IF (HWNDSERVER! = NULL)

Return False;

Hook = setwindowshookex

WH_GetMessage,

(HookProc) Msghook,

Hinstance,

0);

IF (hook! = null)

{/ * success * /

HWNDSERVER = HWND;

Return True;

} / * success * /

Return False;

} // setMYHOOK

转载请注明原文地址:https://www.9cbs.com/read-10694.html

New Post(0)