如何利用hook制作程序如何监视对话框的弹出及其提示的内容?

gyhong 2004-10-20 08:15:17
想大家请教
如何利用hook制作程序如何监视对话框的弹出及其提示的内容?
...全文
266 7 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
longtusoft 2004-10-20
  • 打赏
  • 举报
回复
zwb666 2004-10-20
  • 打赏
  • 举报
回复
ding
gyhong 2004-10-20
  • 打赏
  • 举报
回复
我的具体问题如下:

form1 中有个Webbrower1控件, 用Webbrower1打开一个网页开始录入数据,录入结束后,
该网页弹出对话框(messagebox),文字提示为“数据录入成功!",
为了监视这个对话框,我原来用 Timer ,findwindow 来实现,但程序不稳定。
我想用hook方法来监视监视这个对话框,

我查到了监控键盘的hook方法,思路为: 先用hook方法作一个Dll文件,然后用程序调用
该DLL文件。希望高手通过下面的代码帮我该成一个监控messagebox对话框弹出的程序代码。

谢谢 gyhong

键盘监控程序代码如下:

1。DLL文件代码:
unit Global;
interface
uses windows,Local;
//define the structure of the SetHookHandle function in hookdll.dll
type TSetHookHandle = procedure(HookHandle: HHook); stdcall;

var LibLoaded: boolean; //true if hookdll.dll is already loaded
LibHandle: HInst; //dll handle
HookProcAdd: pointer; //memory address of hook procedure in windows
GHookInstalled: boolean;
SetHookHandle: TSetHookHandle;

function LoadHookProc: boolean;
function SetupGlobalHook: boolean;

implementation

{
LoadHookProc
------------
This function loads the hook procedure from the dll created in hookdll.dll
and obtains a handle for the dll and the address of the procedure in the
dll. The procedure will be called 'GlobalKeyBoardHook'
This procedure also loads the SetHookHandle procedure in hookdll.dll. As
explained in the dll code, this procedure is simply used to inform the dll
of the handle for the current hook, which is needed to call CallNextHookEx
and also to initialise the keyarray (see the dll code).
}
function LoadHookProc: boolean;
begin
//attempt to load the dll containing our hook proc
LibHandle:=LoadLibrary('hookdll.dll');
if LibHandle=0 then begin //if loading fails, exit and return false
LoadHookProc:=false;
exit;
end;
//once the dll is loaded, get the address in the dll of our hook proc
HookProcAdd:=GetProcAddress(LibHandle,'GlobalKeyBoardHook');
@SetHookHandle:=GetProcAddress(LibHandle,'SetHookHandle');
if (HookProcAdd=nil)or(@SetHookHandle=nil) then begin //if loading fails, unload library, exit and return false
FreeLibrary(LibHandle);
LoadHookProc:=false;
exit;
end;
LoadHookProc:=true;
end;

{
SetupGlobalHook
---------------
This function installs a global hook. To the install a global hook, we first have
to load the hook procedure that we have written from hookdll.dll, using the LoadHookProc
above. If succesful use the setwindowshookex function specifying the hook type as WH_KEYBOARD,
the address of the hook procedure is that loaded from hookdll.dll, the hMod is the handle
of the loaded hookdll.dll and the threadid is set to 0 to indicate a global hook.
}
function SetupGlobalHook: boolean;
begin
SetupGlobalHook:=false;
if LibLoaded=false then LibLoaded:=LoadHookProc; //if hookdll isnt loaded, load it
if LibLoaded=false then exit; //if dll loading fails, exit
CurrentHook:=setwindowshookex(WH_KEYBOARD,HookProcAdd,LibHandle,0); //install hook
SetHookHandle(CurrentHook);
if CurrentHook<>0 then SetupGlobalHook:=true; //return true if it worked
end;

end.


2。运行的另一个程序,监控键盘,当通过键盘键入的字超过20个时,存入log.txt文件。
unit MainFormUnit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Local, global,
StdCtrls;

type
TMainForm = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.Button1Click(Sender: TObject);
begin
if GHookInstalled=true then exit; //if a global hook is installed, exit routine
//if a local hook not installed, then attempt to install one, else attempt to remove one
if HookInstalled=false then HookInstalled:=SetupLocalHook else HookInstalled:=not(RemoveLocalHook);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
HookInstalled:=false;
GHookInstalled:=false;
LibLoaded:=false;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
if HookInstalled=true then RemoveLocalHook;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
if HookInstalled=true then exit; //if a local hook is installed, exit routine
//if a local hook not installed, then attempt to install one, else attempt to remove one
//note that removelocalhook can still be used no matter whether the hook is global or local
if GHookInstalled=false then GHookInstalled:=SetupGlobalHook else GHookInstalled:=not(RemoveLocalHook);
end;

end.

gyhong 2004-10-20
  • 打赏
  • 举报
回复
监视其它程序弹出的messagebox对话框
delphi99 2004-10-20
  • 打赏
  • 举报
回复
请说具体一点呀
hottey 2004-10-20
  • 打赏
  • 举报
回复
什么对话框
hottey 2004-10-20
  • 打赏
  • 举报
回复
什么对话框
相关推荐
发帖
Windows SDK/API

1177

社区成员

Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
帖子事件
创建了帖子
2004-10-20 08:15
社区公告
暂无公告