本应用程序的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. Revit二次开发示例:HelloRevit

    本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...

  2. BZOJ2757 : [SCOI2012]Blinker的仰慕者

    BZOJ AC900题纪念~~ 若K>0,则 设f[i][j]表示i位数字,积为j的数字的个数 g[i][j]表示i位数字,积为j的数字的和 DP+Hash预处理 查询时枚举LCP然后统计贡献 ...

  3. C#多线程与异步的区别

    随着拥有多个硬线程CPU(超线程.双核)的普及,多线程和异步操作等并发程序设计方法也受到了更多的关注和讨论.本文主要是想与园中各位高手一同探讨一下如何使用并发来最大化程序的性能. 多线程和异步操作的异 ...

  4. Codeforces Round #207 (Div. 2) A. Group of Students

    #include <iostream> #include <vector> using namespace std; int main(){ ; cin >> m ...

  5. 【BZOJ】1008: [HNOI2008]越狱(快速幂)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1008 刚开始看不会做啊,以为是dp,但是数据太大!!!所以一定有log的算法或者O1的算法,,,,还 ...

  6. TJOI2016 && HEOI2016 解题报告

    好吧我来写一波题解骗访问量QAQ 题目可以在cogs提交 bzoj4551~4456 D1T1 tree 树剖可做,然而有更简单的做法,10min搞定 维护一个并查集,时光倒流,如果当前点没有标记就把 ...

  7. CentOS 拷贝mysql数据库到新的硬盘报错了

    服务器硬盘满了,加了一块,在目录 下新建了 mkdir /mysql cp -r /var/lib/mysql/* /mysql chown -R mysql:root /mysql 更改/etc/m ...

  8. Shell之sed用法 转滴

    通过例子学习sed的用法 1,sed介绍    sed可删除(delete).改变(change).添加(append).插入(insert).合.交换文件中的资料行,或读入其它档的资料到 文> ...

  9. 8.20 css样式表:样式分类,选择器。样式属性,

    样式表分类: 1.内联样式表, 和html联合显示,例:<p style="font-size:14px;">内联样式表</p> 2.内嵌样式表 作为一个独 ...

  10. HP StorageWorks MSL2024 Tape Libraries - Tape library Error Codes

    Main error codes Error Code Description Details and Solution 80 Can not initialize bar code reader P ...