本应用程序的Hook:

unit UFrmMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
btnClose: TButton;
btnSetHook: TButton;
btnSizeLongInt: TButton;
procedure btnCloseClick(Sender: TObject);
procedure btnSetHookClick(Sender: TObject);
procedure btnSizeLongIntClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm}
var
hHookKeyboard : HHOOK; procedure TForm1.btnCloseClick(Sender: TObject);
begin
close;
end; function MouseHook(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
result := 1;
end; function KeyHook(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
if (wparam = vk_f4) and ((lparam and (1 shl 29)) > 0) then
result := 1
else
result := CallNextHookEx(hHookKeyboard, code, wparam, lparam);
end; procedure TForm1.btnSetHookClick(Sender: TObject);
begin
SetWindowsHookEx(WH_MOUSE,@MouseHook,HInstance,GetCurrentThreadId());
hHookKeyboard := SetWindowsHookEx(WH_KEYBOARD,@KeyHook,HInstance,GetCurrentThreadId());
end; procedure TForm1.btnSizeLongIntClick(Sender: TObject);
begin
ShowMessageFmt('sizeof longint:%d',[sizeof(longint)]);
end; end.

//HookLibInterface.pas
unit HookLibInterface; interface
USES
windows;
{$IFNDEF HookLibInterface}
procedure SetHook(hwnd1:HWND); stdcall;
procedure UnHook(); stdcall;
{$ENDIF} implementation
{$IFNDEF HookLibInterface}
procedure SetHook(hwnd1:HWND); external 'HookLib.dll' name 'SetHook';
procedure UnHook(); external 'HookLib.dll' name 'UnHook';
{$ENDIF} end.

  

//HookLib.dpr
library HookLib; { Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. } uses
SysUtils,
Classes,
Windows,
Messages,
Dialogs,
HookLibInterface in 'HookLibInterface.pas'; {$R *.res}
var
hMouseHook : HHOOK;
hKeyboardHook : HHOOK;
var
g_hwnd : HWND; function MouseHookProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
result := 1;
end;
function KeyboardHookProc (code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
if vk_f2 = wparam then
begin
PostMessage(g_hwnd,wm_close,0,0);
//UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKeyboardHook);
end;
result := 1;
end; procedure SetHook(hwnd1 : HWND); stdcall;
begin
g_hwnd := hwnd1;
//hMouseHook := SetWindowsHookEx(WH_MOUSE, MouseHookProc, GetModuleHandle('HookLib.dll'),0);
hKeyboardHook := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProc, GetModuleHandle('HookLib.dll'),0);
showmessage('成功加载勾子程序!');
end; procedure UnHook(); stdcall;
begin
//UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKeyboardHook);
showmessage('成功取消勾子程序!');
end;
exports
SetHook,
Unhook;
end.

使用:

unit UMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TFrmMain = class(TForm)
btnClose: TButton;
btnHookMouse: TButton;
btnUnHookMouse: TButton;
procedure btnCloseClick(Sender: TObject);
procedure btnHookMouseClick(Sender: TObject);
procedure btnUnHookMouseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
FrmMain: TFrmMain; implementation
uses
HookLibInterface;
{$R *.dfm} procedure TFrmMain.btnCloseClick(Sender: TObject);
begin
close;
end; procedure TFrmMain.btnHookMouseClick(Sender: TObject);
begin
SetHook(application.Handle);
end; procedure TFrmMain.btnUnHookMouseClick(Sender: TObject);
begin
UnHook;
end; procedure TFrmMain.FormCreate(Sender: TObject);
begin
SetWindowPos(self.Handle,HWND_TOPMOST,0,0,screen.Width,Screen.Height,SWP_SHOWWINDOW );
end; end.

  


HookLib.dll共享内存最终版:

//HookLib.dpr
library HookLib; { Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. } uses
SysUtils,
Classes,
Windows,
Messages,
Dialogs,
HookLibInterface in 'HookLibInterface.pas'; {$R *.res}
const
cMMFileName: PChar = 'SharedMapData';
var
hMouseHook : HHOOK;
hKeyboardHook : HHOOK; type
TGlobalDLLData = HWND;
PGlobalDLLData = ^HWND;
var
GlobalData : PGlobalDLLData;
MapHandle : THandle;
{
var
g_hwnd : HWND;
}
function MouseHookProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
result := 1;
end;
function KeyboardHookProc (code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
if vk_f2 = wparam then
begin
messagebeep(0);
PostMessage(GlobalData^,wm_close,0,0);
//UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKeyboardHook);
end;
result := 1;
end; procedure SetHook(aHwnd : HWND); stdcall;
begin
//g_hwnd := aHwnd;
GlobalData^ := aHwnd;
//hMouseHook := SetWindowsHookEx(WH_MOUSE, MouseHookProc, GetModuleHandle('HookLib.dll'),0);
hKeyboardHook := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProc, GetModuleHandle('HookLib.dll'),0);
showmessage('成功加载勾子程序!');
end; procedure UnHook(); stdcall;
begin
//UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKeyboardHook);
showmessage('成功取消勾子程序!');
end;
exports
SetHook,
Unhook; procedure OpenSharedData;
var
Size: Integer;
begin
{ Get the size of the data to be mapped. }
Size := SizeOf(TGlobalDLLData); { Now get a memory-mapped file object. Note the first parameter passes
the value $FFFFFFFF or DWord(-1) so that space is allocated from the system's
paging file. This requires that a name for the memory-mapped
object get passed as the last parameter. } MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, Size, cMMFileName); if MapHandle = 0 then
RaiseLastWin32Error;
{ Now map the data to the calling process's address space and get a
pointer to the beginning of this address }
GlobalData := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, Size); if GlobalData = nil then
begin
CloseHandle(MapHandle);
RaiseLastWin32Error;
end;
end; procedure CloseSharedData;
{ This procedure un-maps the memory-mapped file and releases the memory-mapped
file handle }
begin
UnmapViewOfFile(GlobalData);
CloseHandle(MapHandle);
end; procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH: OpenSharedData;
DLL_PROCESS_DETACH: CloseSharedData;
end;
end;
begin
{ First, assign the procedure to the DLLProc variable }
DllProc := @DLLEntryPoint;
{ Now invoke the procedure to reflect that the DLL is attaching
to the process }
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.

Delphi HOOK示例的更多相关文章

  1. React教程:4 个 useState Hook 示例

    摘要: React示例教程. 原文:快速了解 React Hooks 原理 译者:前端小智 到 React 16.8 目前为止,如果编写函数组件,然后遇到需要添加状态的情况,咱们就必须将组件转换为类组 ...

  2. windows钩子 Hook示例

    1.首先编写一个 win32 dll工程. #include "stdafx.h" int WINAPI add(int a,int b) { return a+b; } BOOL ...

  3. Delphi Dll示例

    //MyInt.pas unit MyInt; interface {$IFNDEF MYLIB} function MyAdd(a,b:integer):integer ;stdcall; {$EN ...

  4. delphi hook alt+F4 ctrl+delete+alt win键等

    unit uHook; interface uses  Windows, Messages, SysUtils, Variants, Classes, Controls, Forms, Dialogs ...

  5. delphi WaitForSingleObject 示例之一等待另一个进程的结束

    <pre>unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Cont ...

  6. Hook入门

    Hook入门 2014-07-24 基本概念 Windows消息机制 Hook(钩子) 运行机制 核心函数 C# hook示例 基本概念[1] Windows消息机制[5] Windows操作系统是建 ...

  7. helm-chart7,调试与hook

    调试 几个命令可以帮助进行调试 helm lint 首选工具,返回错误和警告信息. helm install --dry-run --debug:服务器会渲染你的模板,然后返回结果清单文件. helm ...

  8. react新特性hook

    一.hook示例.   import React, { useState } from 'react'; function Example() { // 声明一个叫 “count” 的 state 变 ...

  9. PHP API 框架开发的学习

    基于互联网的应用正变得越来越普及,在这个过程中,有更多的站点将自身的资源开放给开发者来调用.对外提供的API 调用使得站点之间的内容关联性更强,同时这些开放的平台也为用户.开发者和中小网站带来了更大的 ...

随机推荐

  1. spring实战五之Bean的自动检测

    在spring实战四中,使用在Spring中增加<context:annotation-config>的方式告诉Spring,我们打算使用基于注解的自动装配,希望Spring特殊对待我们所 ...

  2. ubuntu桌面进不去,我跪了

    ubuntu12.04 输入密码正确,但仍然跳回到登陆界面,实在受不了啊! 不知道bug再哪里,但是有个方法真是屡试不爽啊.. ctrl+alt+f1切换到字符界面 /home/xxx/.Xautho ...

  3. BZOJ4026: dC Loves Number Theory

    Description  dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯 竭的水题资源.    给定一个长度为 n的正整数序列A,有q次询问,每次询问一段区间内所 ...

  4. 字符串分割与存入List集合

    List<string> namelist = new List<string>(); string[] namejh = null; string name= "张 ...

  5. SVN 中trunk、branches、tags

    SVN 中trunk.branches.tags   我们在一些著名开源项目的版本库中,通常可以看到trunk, branches, tags等三个目录.由于SVN固有的特点,目录在SVN中并没有特别 ...

  6. [转载]VC6中的文件后缀

    VC文件扩展名 .APS:存放二进制资源的中间文件,VC把当前资源文件转换成二进制格式,并存放在APS文件中,以加快资源装载速度. .BMP:位图资源文件. .BSC:浏览信息文件,由浏览信息维护工具 ...

  7. sql语句清除mssql日志

    DUMP TRANSACTION TestDB WITH NO_LOG 清除日志 DBCC SHRINKFILE ('TestDB_log',1) 收缩数据库文件            -----直接 ...

  8. style在进行图形绘制前,要设置好绘图的样式

    是html5出现的新标签,像所有的dom对象一样它有自己本身的属性.方法和事件,其中就有绘图的方法,js能够调用它来进行绘图 ,最近在研读<html5与css3权威指南>下面对其中最好玩的 ...

  9. zhcon安装过程记录

    参考资料: 1. http://www.linuxdiyf.com/viewarticle.php?id=81796 需要下载的文件有两个:zhcon-0.2.5.tar.gz和zhcon-0.2.5 ...

  10. NBOJv2 Problem 1009 蛤玮的魔法(二分)

    Problem 1009: 蛤玮的魔法 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  %ll ...