一关闭程序就引起程序崩溃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.

解决方案 »

  1.   

    Hook里面不要调用SendMessage,会死循环的
    PostMessage或者另开一个线程SendMessage
      

  2.   

    SendMessage要等待结果放回才会继续
    PostMessage不会等待结果就会继续
      

  3.   

    谢谢两位,果然使用PostMessage就正常了
      

  4.   


    多试了几次,用PostMessage还是要崩溃,程序退出后一开我的电脑就崩溃
      

  5.   

    崩溃的时候用debugger打开看看call stack
      

  6.   

    无论nCode是否小于0,都要调用CallNextHookEx。最好判断一下FindWindow成功再发消息。
    调试看看UnHookWindowsHookEx是否执行了。
    另外,相关代码都贴出来了吗?