Delphi HOOK示例

本应用程序的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.

  

 

 

时间: 2024-09-27 03:47:52

Delphi HOOK示例的相关文章

windows钩子 Hook示例

1.首先编写一个 win32 dll工程. #include "stdafx.h" int WINAPI add(int a,int b) { return a+b; } BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } 在def文件添加显式导出: (没找到def文件需要添加) LIBRARY DESCRIPTION "AD

Delphi Dll示例

//MyInt.pas unit MyInt; interface {$IFNDEF MYLIB} function MyAdd(a,b:integer):integer ;stdcall; {$ENDIF} implementation {$IFNDEF MYLIB} function MyAdd; external 'MyLib.dll' name 'MyAdd'; {$ENDIF} end. //MyLib.dpr library MyLib; { Important note about

helm-chart7,调试与hook

调试 几个命令可以帮助进行调试 helm lint 首选工具,返回错误和警告信息. helm install --dry-run --debug:服务器会渲染你的模板,然后返回结果清单文件. helm get manifest:这是查看服务器上安装的模板的方法. 如果yaml文件没有解析,想看看生成了什么时,检查yaml文件一个简单方法是注释模板中的问题部分,然后重新运行helm install --dry-run --debug hooks hook 机制,为允许在release的生命周期中的

python中import的机制与实现

原文出处: 刘畅(@你猜我猜不猜猜你是谁) 概述 Python 是一门优美简单.功能强大的动态语言.在刚刚接触这门语言时,我们会被其优美的格式.简洁的语法和无穷无尽的类库所震撼.在真正的将python应用到实际的项目中,你会遇到一些无法避免的问题.最让人困惑不解的问题有二类,一个 编码问题,另一个则是引用问题. 本文主要讨论关于Python中import的机制与实现.以及介绍一些有意思的Python Hooks. Python 类库引入机制 首先,看一个简单的例子: """

音视频即时通讯 功能需求汇总

即时通讯开发,也叫音视频即时通信开发.随着互联网的发展,人们之间的交流逐步从电话移向网络.每天都有相当多的人在使用各种网络交流工具,如Anychat,腾讯QQ,ICQ,MSN,新浪微博. 可以看出人们对于网络上即时的沟通方式是非常敏锐的,所能容纳的程度也远远超过我们的预计.然而目前大部分网络交流工具都还是以文字为主,语音视频功能大部分还是不够成熟,完全通过网络实现语音视频需要考虑到很多方面,如:硬件.软件.技术.网络:等等.纯文字沟通方式效率非常低而且也不符合人们平素的习惯,作为一种消遣的工具尚

使用delphi+intraweb进行微信开发1~4代码示例

前几讲重点阐述的是使用iw进行微信开发的技术难点及解决方法,提供的都是代码片段(微信消息加解密是完整代码),实际上我始终感觉按照教程实作是掌握一门技术的最重要的方法!不过对于刚刚接触这类开发的朋友来说可能学习曲线陡峭了点,既然如此,特意放出了前4讲的示例代码,去掉了数据库相关操作,保留了最精华的代码部分,直接编译即可进行测试. 下载地址:http://download.csdn.net/detail/netwizard/9475970 此虽为示例代码但是是从我一个即将完工的商业项目中拷贝出来的,

DELPHI XE5 跨平台 Form ShowModal 官方示例

Calling ShowModal as an Anonymous Method on All Platforms procedure THeaderFooterForm.btnPickClick(Sender: TObject); var dlg: TForm1; begin dlg := TForm1.Create(nil); // select current value, if available in the list dlg.ListBox1.ItemIndex := dlg.Lis

Delphi 多文件拖放获取路径示例

unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, ShellAPI; type  TForm1 = class(TForm)    Memo1: TMemo;    procedure FormCreate(Sender: TObject);  private    // 当文件拖放至窗体中后,系统将向

delphi ICS控件示例解读

1 {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 3 Author: Fran鏾is PIETTE 4 Object: Demo program to show how to use TWSocket object is a very 5 simple server program. This server just wait for a client to 6 connect, th