本应用程序的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. jquery.validate.js的remote用法

    <script> $(function(){ $("#myform").validate( { rules: { name:{required:true,rangele ...

  2. POJ2699 The Maximum Number of Strong Kings(最大流)

    枚举所有Strong King的状态(最多1024种左右),然后判断是否合法. 判定合法用网络流,源点-比赛-人-汇点,这样连边. 源点向每场比赛连容量为1的边: 如果一场比赛,A和B,A是Stron ...

  3. TYVJ P1004 滑雪 Label:记忆化搜索

    背景 成成第一次模拟赛 第三道 描述     trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行的路线必须向下倾斜.    ...

  4. 【BZOJ】3809: Gty的二逼妹子序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...

  5. 【POJ】1279 Art Gallery

    http://poj.org/problem?id=1279 题意:给一个n个点的多边形,n<=1500,求在多边形内能看到所有多边形上的点的面积. #include <cstdio> ...

  6. HTML5中的Blob对象的使用

    HTML5中的Blob对象和MYSQL中的BLOB类型在概念上是有点区别的.MYSQL中的BLOB类型就只是个二进制数据容器.而HTML5中的Blob对象除了存放二进制数据外还可以设置这个数据的MIN ...

  7. Google Code Jam 2010 Round 1B Problem A. File Fix-it

    https://code.google.com/codejam/contest/635101/dashboard#s=p0   Problem On Unix computers, data is s ...

  8. Java实现FTP文件上传与下载

    实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方式 package com.cl ...

  9. 创建 maven 本地仓库; (五)

    在 pom.xml 添加依赖包的时候,有时候会提示无法从 http://repo1.maven.org/maven2/ 获取的情况,这时可配置个本地仓库: 从网上下载 maven 仓库网站源码包 Ne ...

  10. OpenCV IplImage FlyCapture2 Image Conversion 两种图像类的相互转化

    OpenCV的IplImag和 FlyCapture2 的 Image是两种常见的图片格式,在实际的应用中,我们通常要混合使用OpenCV和FlyCapture2这两个SDK,所以这两种图片格式之间的 ...