一关闭程序就引起程序崩溃Hook代码:
unit hkprocunit;
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;var
hHk:HHOOK;
function HookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;function EnableHook:Boolean; stdcall; export;
function DisableHook:Boolean; stdcall; export;implementationfunction HookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
var
Data: TCWPStruct;
WindowText,WindowClass:Array[0..254] of char;
pid:hwnd;
begin
Result := 0;
if nCode < 0 then begin
Result := CallNextHookEx(hHk,nCode,WParam,LParam)
end else begin
Data := (PCWPStruct(LParam))^;
case Data.message of
wm_move:begin
GetClassName(Data.hwnd, @WindowClass, 254);
GetWindowText(Data.hwnd,@WindowText,254);
if (WindowClass='IEFrame') then begin
pid := FindWindow(nil, PChar('Form2'));
SendMessage(pid,WM_USER+122,data.wParam,data.lParam);
end; end;
end;
end;
end;function EnableHook:Boolean; stdcall; export;
begin
if hHk = 0 then
Begin
hHk := SetWindowsHookEx(WH_CALLWNDPROC,@HookProc,Hinstance,0);
Result := True;
end
else
Result := False;
end;function DisableHook:Boolean; stdcall; export;
begin
if hHk <> 0 then
begin
UnHookWindowsHookEx(hHk);
hHk := 0;
Result := True;
end
else
Result := False;
end;end.Form2:
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
WM_USERMY = WM_USER + 122;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure WM_USERMY(var msg:TMessage);message WM_USERMY;
{ Public declarations }
end;var
Form2: TForm2;
function EnableHook:Boolean; stdcall; external 'MyHook.dll' name 'EnableHook';
function DisableHook:Boolean; stdcall; external 'MyHook.dll' name 'DisableHook';
implementation{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DisableHook;
end;procedure TForm2.WM_USERMY(var msg: TMessage);
var
WindowText,WindowClass:Array[0..254] of char;
pt: TPoint;
buf: array[0..255] of Char;
R: TRect;
begin
if Windows.GetClientRect(msg.WParam, R) then begin
pt.X := LoWord(msg.lParam); //lParam 中的低两位是 x 坐标
pt.Y := HiWord(msg.lParam); //lParam 中的高两位是 y 坐标
Form2.Top:=pt.Y;//顶端有问题
Form2.Left:=pt.X+r.Right;//左端有问题
form2.Height:=R.Bottom;
//memo1.Lines.Add(inttostr(pt.X)+';'+inttostr(r.Right)+';'+inttostr(pt.y)+'['+inttostr(r.Bottom)+']'+inttostr(r.Top));
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
if EnableHook then
memo1.Lines.Add('启动成功');
end;end.
unit hkprocunit;
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;var
hHk:HHOOK;
function HookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;function EnableHook:Boolean; stdcall; export;
function DisableHook:Boolean; stdcall; export;implementationfunction HookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
var
Data: TCWPStruct;
WindowText,WindowClass:Array[0..254] of char;
pid:hwnd;
begin
Result := 0;
if nCode < 0 then begin
Result := CallNextHookEx(hHk,nCode,WParam,LParam)
end else begin
Data := (PCWPStruct(LParam))^;
case Data.message of
wm_move:begin
GetClassName(Data.hwnd, @WindowClass, 254);
GetWindowText(Data.hwnd,@WindowText,254);
if (WindowClass='IEFrame') then begin
pid := FindWindow(nil, PChar('Form2'));
SendMessage(pid,WM_USER+122,data.wParam,data.lParam);
end; end;
end;
end;
end;function EnableHook:Boolean; stdcall; export;
begin
if hHk = 0 then
Begin
hHk := SetWindowsHookEx(WH_CALLWNDPROC,@HookProc,Hinstance,0);
Result := True;
end
else
Result := False;
end;function DisableHook:Boolean; stdcall; export;
begin
if hHk <> 0 then
begin
UnHookWindowsHookEx(hHk);
hHk := 0;
Result := True;
end
else
Result := False;
end;end.Form2:
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
WM_USERMY = WM_USER + 122;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure WM_USERMY(var msg:TMessage);message WM_USERMY;
{ Public declarations }
end;var
Form2: TForm2;
function EnableHook:Boolean; stdcall; external 'MyHook.dll' name 'EnableHook';
function DisableHook:Boolean; stdcall; external 'MyHook.dll' name 'DisableHook';
implementation{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DisableHook;
end;procedure TForm2.WM_USERMY(var msg: TMessage);
var
WindowText,WindowClass:Array[0..254] of char;
pt: TPoint;
buf: array[0..255] of Char;
R: TRect;
begin
if Windows.GetClientRect(msg.WParam, R) then begin
pt.X := LoWord(msg.lParam); //lParam 中的低两位是 x 坐标
pt.Y := HiWord(msg.lParam); //lParam 中的高两位是 y 坐标
Form2.Top:=pt.Y;//顶端有问题
Form2.Left:=pt.X+r.Right;//左端有问题
form2.Height:=R.Bottom;
//memo1.Lines.Add(inttostr(pt.X)+';'+inttostr(r.Right)+';'+inttostr(pt.y)+'['+inttostr(r.Bottom)+']'+inttostr(r.Top));
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
if EnableHook then
memo1.Lines.Add('启动成功');
end;end.
解决方案 »
- 用友华表cell产品渠道伙伴盛大招募中
- 问个数据库的问题!
- 计算机图形学达人请进,帮忙做几个作业题目,高分相送,不够还可以给!
- 如何控制ListView每行的颜色,比如说绿色,红色。等都可以
- 如何知道数据已经更新或未更新?
- FastReport 和 OpenDialog1 一起用怎么就打印不了 兼容性 ?
- 求职:熟悉SQL语言,PB,DElPHI,C++等
- 求救!在delphi中如何将两个表连接生成一个新表???
- 由于某种正当目的:如何改变EXE文件的大小、创建日期、修改日期、访问日期。谢谢。
- 好书推荐
- 在不改变原有行、列显示数据的情况下,Dbgrid能再次填充其它表的内容吗?
- 如何给手持机 发送文字?请指教。
PostMessage或者另开一个线程SendMessage
PostMessage不会等待结果就会继续
多试了几次,用PostMessage还是要崩溃,程序退出后一开我的电脑就崩溃
调试看看UnHookWindowsHookEx是否执行了。
另外,相关代码都贴出来了吗?