Hooks and their applications (v)

Source: Internet
Author: User

Here is the source code of the DLL:

The first is a unit that declares some common public data structures, which are used in DLLs as well as in programs:

Unit Wdspycommon;

{*******************************************

* Brief: A declaration file of the data structure used by the message spy

* Autor:linzhenqun

* date:2005-9-25

* email:linzhengqun@163.com

* Blog:http://blog.csdn.net/linzhengqun

********************************************}

Interface

Uses

Windows, Messages;

Resourcestring

Err_procinvalid = ' The Message spy procedure is invalid. ';

Err_sharemem = ' could not create share memory. ';

Const

Msg_null_value = 0;

Msg_type_sent = 1;

Msg_type_post = 2;

Msg_type_return = 3;

Type

{Monitored message structure}

Pmsginfo = ^tmsginfo;

Tmsginfo = Packed record

Hwnd:hwnd;

Message:uint;

Wparam:wparam;

Lparam:lparam;

Time:dword;

Pt:tpoint;

Lresult:lresult;

Msgtype:uint;

End

Implementation

End.

Next is the source code in the message spy's DLL:

Unit Wdmsgspy;

{*******************************************

* Brief: Message spy's SDK

* Autor:linzhenqun

* date:2005-9-24

* email:linzhengqun@163.com

* Blog:http://blog.csdn.net/linzhengqun

********************************************}

Interface

Uses

Messages, Windows, Classes, Sysutils, Wdspycommon;

Const

Mapingfile_name = ' msgspy_fc819a73-2718-47e2-bf78-6810562cda65 ';

Type

Shared memory

Psharemem = ^tsharemem;

Tsharemem = Record

Hrevwnd:thandle; Hold the window handle that receives the message

Hwndspy:thandle; The monitored window handle

End

{Start monitoring the message, ahspywnd is the monitored window, ahrevwnd is the window that receives the monitored message}

function Startspymessage (Ahspywnd, ahrevwnd:thandle): Boolean; stdcall;

{Stop monitoring messages}

Procedure Stopspymessage; stdcall;

Var

Hmsgproc, Hwndproc, Hwndretproc:thandle; Handle of the corresponding hook

Psmem:psharemem; Shared Memory Blocks

Hmapfile:thandle; A handle to the memory-mapped file.

Implementation

To send the intercepted message structure to the target window

Procedure SendData (Amsginfo:pmsginfo); stdcall;

Var

Pcds:pcopydatastruct;

Begin

New (PCDS);

Pcds^.cbdata: = SizeOf (Tmsginfo);

Pcds^.lpdata: = Amsginfo;

SendMessage (psmem^. Hrevwnd, Wm_copydata, 0, Longint (PCDS));

Dispose (PCDS);

End

{Wh_getmessage's hook procedure}

function Getmsgproc (code:integer; wp:wparam; lp:lparam): LRESULT; stdcall;

Var

Lmsginfo:pmsginfo;

Begin

{Only the window handle of the intercepted message equals the window handle being monitored for the next step,

Next, when the monitored window is not the window that receives the message, do the following, or when the monitored window

is the window that receives the message, the intercepted message is not wm_copydata, and the next step is performed.

This is done to avoid getting into the dead loop of sending messages and intercepting messages}

If code = Hc_action Then

if (PMSG (LP) ^.hwnd = psmem^. Hwndspy) Then

if (psmem^. Hwndspy = psmem^. Hrevwnd) and (PMSG (LP) ^.message <> Wm_copydata))

or (psmem^. Hwndspy <> psmem^. Hrevwnd) Then

Begin

New (Lmsginfo);

Lmsginfo.hwnd: = PMSG (LP) ^.hwnd;

Lmsginfo.message: = PMSG (LP) ^.message;

Lmsginfo.wparam: = PMSG (LP) ^.wparam;

Lmsginfo.lparam: = PMSG (LP) ^.lparam;

lmsginfo.pt: = PMSG (LP) ^.pt;

Lmsginfo.time: = PMSG (LP) ^.time;

Lmsginfo.lresult: = Msg_null_value;

Lmsginfo.msgtype: = Msg_type_post;

SendData (Lmsginfo);

Dispose (Lmsginfo);

End

Result: = CallNextHookEx (Hmsgproc, Code, WP, LP);

End

{Wh_callwndproc's hook procedure}

function Callwndproc (code:integer; wp:wparam; lp:lparam): LRESULT; stdcall;

Var

Lmsginfo:pmsginfo;

Begin

If code = Hc_action Then

if (PCWPSTRUCT (LP) ^.hwnd = psmem^. Hwndspy) Then

if (psmem^. Hwndspy = psmem^. Hrevwnd) and (PCWPSTRUCT (LP) ^.message <> Wm_copydata))

or (psmem^. Hwndspy <> psmem^. Hrevwnd) Then

Begin

New (Lmsginfo);

Lmsginfo^.hwnd: = PCWPSTRUCT (LP) ^.hwnd;

Lmsginfo^.message: = PCWPSTRUCT (LP) ^.message;

Lmsginfo^.wparam: = PCWPSTRUCT (LP) ^.wparam;

Lmsginfo^.lparam: = PCWPSTRUCT (LP) ^.lparam;

lmsginfo^.pt: = Point (0, 0);

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.