gHook:=SetWindowsHookEx(WH_CALLWNDPROC,@MsgHookProc,HInstance,0);安装HOOKHOOK过程
function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
begin
  Result:=0;
  if iCode < 0 then
  begin
    Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
    Exit;
  end;  if alParam=WM_CLOSE then
    ShowMessage('拦截成功');
end;
alParam一直等于0

解决方案 »

  1.   

    试试
    function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    begin
    //  Result:=0;
      if alParam=WM_CLOSE then
        ShowMessage('拦截成功');//  if iCode < 0 then
    //  begin
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
    //    Exit;
    //  end;
    end;
      

  2.   


    function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    var 
      cwp: PCwpStruct; 
    begin
    if iCode < 0 then
      begin
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
        Exit;
      end;
     if cwp.wParam =SC_CLOSE then 
      begin
     ShowMessage('拦截成功');
      end; 
    end;代码我没有测试,你自己测试一下看看
      

  3.   

      cwp := PCwpStruct(lParam);
    if cwp.wParam =SC_CLOSE then 
      begin
     ShowMessage('拦截成功');
      end; 
    end;
      

  4.   

    if alParam=WM_CLOSE then
        ShowMessage('拦截成功');
    if iCode < 0 then
      begin
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
        Exit;
      end;
      

  5.   

    以上代码均测试无效,alParam依然一直等于0
      

  6.   

    我把所有代码贴出来吧,麻烦帮忙看下是哪的问题unit MyMsgHook;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, ComCtrls;
    function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    function SetupHook:Boolean;stdcall
    function EndHook:Boolean;stdcallvar
      IsHooked:Boolean=False;
      gHook:HHOOK;implementation
    //------------------------------------------------------------------------------function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    var
      cwp: PCwpStruct;
    begin
      cwp := PCwpStruct(alParam);
    if iCode < 0 then
      begin
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
        Exit;
      end;
     if cwp.wParam =SC_CLOSE then
      begin
     ShowMessage('拦截成功');
      end;
    end;//------------------------------------------------------------------------------
    function SetupHook:Boolean;stdcall
    begin
      if IsHooked=False then
      begin
        gHook:=SetWindowsHookEx(WH_GETMESSAGE,@MsgHookProc,HInstance,0);
        ShowMessage('开始获取');
        IsHooked:=True;
        Result:=True;
      end
      else
        Result:=False;
    end;
    //------------------------------------------------------------------------------
    function EndHook:Boolean;stdcall
    begin
      if IsHooked=True then
      begin
        ShowMessage('停止工作');
        Result:=UnhookWindowsHookEx(gHook);
        IsHooked:=False;
      end
      else
        Result:=False;
    end;
    end.
    然后我点开一个记事本,运行之后点击关闭记事本,没有任何动作
      

  7.   

    OK 十分感谢hjkto你的回答,但为什么if alParam=WM_CLOSE then 
        ShowMessage('拦截成功'); 
    if iCode < 0 then 
      begin 
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam); 
        Exit; 
      end;这样确不行?
      

  8.   

    gHook:=SetWindowsHookEx(WH_GETMESSAGE
    WH_GETMESSAGE,换成gHook:=SetWindowsHookEx(WH_CALLWNDPROC,@MsgHookProc,HInstance,0);安装HOOK 你一楼发的帖子,是用WH_CALLWNDPROC这个消息
    怎么又换了?
      

  9.   

    unit MyMsgHook;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, ComCtrls;
    function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    function SetupHook:Boolean;stdcall
    function EndHook:Boolean;stdcallvar
      IsHooked:Boolean=False;
      gHook:HHOOK;implementation
    //------------------------------------------------------------------------------function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    begin
    if alParam=WM_CLOSE then 
        ShowMessage('拦截成功'); 
    if iCode < 0 then 
      begin 
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam); 
        Exit; 
      end; 
    end;//------------------------------------------------------------------------------
    function SetupHook:Boolean;stdcall
    begin
      if IsHooked=False then
      begin
        gHook:=SetWindowsHookEx(WH_CALLWNDPROC,@MsgHookProc,HInstance,0);
        ShowMessage('开始获取');
        IsHooked:=True;
        Result:=True;
      end
      else
        Result:=False;
    end;
    //------------------------------------------------------------------------------
    function EndHook:Boolean;stdcall
    begin
      if IsHooked=True then
      begin
        ShowMessage('停止工作');
        Result:=UnhookWindowsHookEx(gHook);
        IsHooked:=False;
      end
      else
        Result:=False;
    end;
    end.
    前面已经改了,能成功,但我想问的是这个,,为什么不能拦截。
      

  10.   

    function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    var
      cwp: PCwpStruct;
    begin
      cwp := PCwpStruct(alParam);
    if iCode < 0 then
      begin
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
        Exit;
      end;
     if cwp.wParam =SC_CLOSE then
      begin
     ShowMessage('拦截成功');
      end;
    end;
    换成
    function MsgHookProc(icode:Integer;awParam:WPARAM;alParam:LPARAM):LongInt;stdcall;
    var
      cwp: PCwpStruct;
    begin
    if iCode < 0 then
      begin
        Result := CallNextHookEx(gHook, iCode, awParam, aLParam);
        Exit;
      end;cwp := PCwpStruct(lParam);
      if cwp^.message <> WM_SYSCOMMAND then 
      begin
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); 
        exit; 
      end;   if cwp.wParam <>SC_CLOSE then 
      begin 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); 
        exit; 
      end;   GetClassName(cwp^.hwnd,na,299); 
      if lstrcmpi(na,'notepad')=0 then 
      begin 
    ShowMessage('拦截成功');
      end; 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); end; 这回再试试
      

  11.   

    我不是这个意思,前面听你的改成
      cwp := PCwpStruct(lParam); 
    if cwp.wParam =SC_CLOSE then 
      begin 
    ShowMessage('拦截成功'); 
      end; 
    end; 
    已经成功了,但我想问的是为什么用我的方法却不能成功,因为你的方法我实在是不能理解啊。
      

  12.   

    alParam这个消息在我的代码中,是被强制转换过的,而你的代码中没有
    正如,你所说的一直为0,根本没传进去
      

  13.   

    这是我在网上看到的代码C++的
    // QQ发送消息对话框回调过程·李马伪造版
    LRESULT CALLBACK ProcSendDlg(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
     switch (Msg)
     {
      case WM_CLOSE:
       EndDialog(hDlg, 0);
       break;
      case WM_COMMAND:
      {
       switch (LOWORD(wParam))
       {
        case IDC_BTN_SENDMSG:
         // 发送消息...
         break;
         // 其它的命令按钮处理部分...
       }
      }
      break;
      // 其它的case部分...
     }
     return 0;
    } 他这里就没有转换,但他的这个回调函数中多了一个参数
      

  14.   

    难道要我去看看MSDN?只不过这里我没有安装
      

  15.   

    先回去看看MSDN 明天再来吧,明天把MSDN安装到这个地方来,。
      

  16.   

    总之今天谢谢你的耐心解答,我有好几个问题都是你解答的,在这里十分感谢,这个问题就明天吧,如果你有空的话,我也回去温习下MSDN。
      

  17.   

     感谢hjkto的指点
    看了MSDN后我懂了,不过有一个小小疑问。
     cwp := PCwpStruct(lParam);  
    if cwp.wParam =SC_CLOSE then 
      begin 
    ShowMessage('拦截成功'); 
      end; 
    end; CWP是一个指针吧,指针可以直接CWP.WPARAM调用?
    另外在WH_CALLWNDPROC钩子类型中,LPARAM本来就是指向CWPSTRUCT的吧,还用得着去刻意转换?不可以用
    lParam.wParam=SC_CLOSE这样来使用?
      

  18.   

    或者lParam^.wParam=SC_CLOSE这样?因为家里没有装DELPHI,所以无法测试。
      

  19.   

    lParam.wParam=SC_CLOSE这样来使用,提示语法错误
    我一会儿试着按着你的c代码写一下看看
      

  20.   

    我看了下MSDN,因为MSDN里这样写到lParam 
    Pointer to a CWPSTRUCT structure that contains details about the message说明LPARAM是一个指针吧,
    就是
    PCwpStruct(lParam)没有理解到。
      

  21.   

    lParam^.wParam=SC_CLOSE
    lParam.wParam=SC_CLOSE
    都会报错
    [Pascal Error] TmHook.dpr(1): Unable to invoke Code Completion due to errors in source codefunction TmHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): Integer; stdcall; 
    中的lParam: LPARAM,按下ctrl+双击LPARAM
    type
      WPARAM = Longint;
      {$EXTERNALSYM WPARAM}
      LPARAM = Longint;
      {$EXTERNALSYM LPARAM}
      LRESULT = Longint;
      {$EXTERNALSYM LRESULT}
      

  22.   

    lParam 
    你在msdn中,查的是什么,这个参数是谁的?
      

  23.   

    LRESULT CALLBACK CallWndProc(
      int nCode,      // hook code
      WPARAM wParam,  // current-process flag
      LPARAM lParam   // address of structure with message data
    );
     
    Parameters
    nCode 
    Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and must return the value returned by CallNextHookEx. 
    wParam 
    Specifies whether the message was sent by the current thread. If the message was sent by the current thread, it is nonzero; otherwise, it is zero. 
    lParam 
    Pointer to a CWPSTRUCT structure that contains details about the message. 

    msdn里查的资料
      

  24.   

    PCwpStruct(lParam)这是强制把lParam转换为PCwpStruct指针?但当WH_CALLWNDPROC安装时,lParam 
    Pointer to a CWPSTRUCT structure that contains details about the message.lParam已经是指向这个的指针了啊。
      

  25.   

    hjkto非常感谢你,这贴完了后,我会开新贴送分的
      

  26.   


      library TmHook;
    uses 
      SysUtils, 
      Classes, 
      Windows, 
      Messages,
      Dialogs,
      CommCtrl; var
      hhook: Windows.HHOOK;
     // hwndListView: HWND;
      //na:array[0..300] of char;{$R *.res} 
    function TmHookProc(nCode: Integer;wParam: WPARAM; lParam: LPARAM): Integer; stdcall;
    var 
      cwp: PCwpStruct; 
    //  cs: PCreateStruct;
    begin
    //Result:=0;
    if (nCode < 0) then
      begin 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); 
        exit; 
      end;   cwp := PCwpStruct(lParam);
      if cwp^.message = WM_close then //设置要钩的消息。
      begin
       ShowMessage('拦截成功');
      end; 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam);end;
    procedure Hook(bEnable: Boolean); stdcall; export;
    begin
      if bEnable then
      begin
        if hhook = 0 then
        begin
          hhook := SetWindowsHookEx(WH_CALLWNDPROC, @TmHookProc, HInstance, 0);
        end;
      end
      else
      begin
        if hhook <> 0 then
        begin 
          UnhookWindowsHookEx(hhook); 
          hhook := 0; 
        end; 
      end; 
    end; 
    exports 
      Hook; begin 
      hhook := 0; 
      //hwndListView := 0;end. 
    这是我昨晚写的
    你所说的哪些,我都测试了不行
    hhook := SetWindowsHookEx(WH_CALLWNDPROC, TmHookProc, HInstance, 0);
    TmHookProc你认为它是什么?回调函数,还是其它的?
      

  27.   

    TmHookProc,这个是由你自己来写,但是由系统调用的
    WH_CALLWNDPROC你有没有在msdn查一下这个消息?
      

  28.   

    TmHookProc是回调函数啊,我发的那个C++代码不是回调函数
    我的意思是
    cwp := PCwpStruct(lParam);
    cwp^.message 
    这两句我不懂,第一句你的意思把LPARAM强制转换指成针类型吧,我想问的是当HOOK安装的时候,LPARAM本来就是指向PCwpStruct这个的指针啊,不过在编译的时候他是一个INTEGER,不知道我说得对不对。第二就是CWP不是一个指针类型吗,为什么CWP.WPARAM不是CWP^.wparam 而取MESSAGE成员的时候却要用到cwp^.message.
      

  29.   

    CallWndProc就是WH_CALLWNDPROC
    The CallWndProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function whenever theSendMessage function is called. Before passing the message to the destination window procedure, the system passes the message to the hook procedure. The hook procedure can examine the message; it cannot modify it. The HOOKPROC type defines a pointer to this callback function. CallWndProc is a placeholder for the application-defined or library-defined function name. LRESULT CALLBACK CallWndProc(
      int nCode,      // hook code
      WPARAM wParam,  // current-process flag
      LPARAM lParam   // address of structure with message data
    );
     
    Parameters
    nCode 
    Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and must return the value returned by CallNextHookEx. 
    wParam 
    Specifies whether the message was sent by the current thread. If the message was sent by the current thread, it is nonzero; otherwise, it is zero. 
    lParam 
    Pointer to a CWPSTRUCT structure that contains details about the message. 
    Return Values
    If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure does not call CallNextHookEx, the return value should be zero. Res
    The CallWndProc hook procedure can examine the message, but it cannot modify it. After the hook procedure returns control to the system, the message is passed to the window procedure. An application installs the hook procedure by specifying the WH_CALLWNDPROC hook type and the address of the hook procedure in a call to the SetWindowsHookEx function. The WM_CALLWNDPROC hook is called in the context of the thread that calls SendMessage, not the thread that receives the message. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in winuser.h.
      Import Library: User-defined.See Also
    Hooks Overview, Hook Functions, CallNextHookEx, CWPSTRUCT,SendMessage, SetWindowsHookEx  
      

  30.   

    cwp := PCwpStruct(lParam); 这是指针结构,包括成员的,有点像记录结构
    if cwp^.message <> WM_SYSCOMMAND thenlParam中的lparam本身指向wparam if cwp.wParam <>SC_MINIMIZE then 
      

  31.   

    typedef struct {
        LPARAM lParam;
        WPARAM wParam;
        UINT message;
        HWND hwnd;
    } CWPSTRUCT, *PCWPSTRUCT;
    if cwp^.message <> WM_SYSCOMMAND then 
    message它还是一个结构,所以要用^
    cwp.wParam <>SC_MINIMIZE 
    wParam只存在着一些附加信息,所以不用^用.)))))))我想问的是当HOOK安装的时候,LPARAM本来就是指向PCwpStruct这个的指针啊,不过在编译的时候他是一个INTEGER,不知道我说得对不对。
    这点,我要再想想
      

  32.   

    if cwp.message = WM_close then 这样写也可以,我有点让你搞晕了
    但不管怎么样,都要在它们之前强制转换我看过许多代码,都是这样的写的至于msdn所讲的,我也有点糊涂))))))))))WH_CALLWNDPROC 
    nCode 只能是HC_ACTION,它代表有一个消息发送给了一个窗口 
    wParam 如果非0,代表正被发送的消息 
    lParam 指向CWPSTRUCT型结构体变量的指针 
    return value: 未使用,返回0 
      

  33.   

    lParam中的lparam本身指向wparam
    这个想了半天还是没想通?
      

  34.   

    cwp.message = WM_close 之类的先放一边吧,用久了也就成了习惯了。
    主要是转换这个过程,也就是说可以把一个integer类型的强制转换成POINTER类型?
      

  35.   


    function TmHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): Integer; stdcall; 
    var 
      cwp: PCwpStruct; 
    //  cs: PCreateStruct;
    begin
    //Result:=0;
    if (nCode < 0) then
      begin 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); 
        exit; 
      end;   cwp := PCwpStruct(lParam);
      if cwp.message <> WM_SYSCOMMAND then //设置要钩的消息。
      begin
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); 
        exit; 
      end;   if cwp.wParam <>SC_MINIMIZE then 
      begin 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); 
        exit; 
      end;   GetClassName(cwp.hwnd,na,299); 
      if lstrcmpi(na,'notepad')=0 then 
      begin 
    ShowMessage('拦截成功');   end; 
        Result := CallNextHookEx(hhook, nCode, wParam, lParam); end; 这么写也行,前面cwp已经是指针类型了lparam是长整形,要想访问哪个结构的成员,必须强制转换一下,也许在c中不用转,在d中要换的,许多代码都是这样写的
      

  36.   

    SizeOf(Longint)
      SizeOf(Pointer) 都是4 
    即能转换。
    message是一个属性,LPARAM类型没有这个属性。 
    不转换你用不了它的
      

  37.   

      cwp := PCwpStruct(lParam);
    你不那样写编译器咋知道呢?
      

  38.   

    好,十分感谢你的帮忙。
    =------------------------------------------------------------------------
    SizeOf(Longint) 
      SizeOf(Pointer) 都是4 
    即能转换。 
    message是一个属性,LPARAM类型没有这个属性。 
    不转换你用不了它的
    -------------------------------------------------------------有个小问题,请在这个贴子里回答。
    http://topic.csdn.net/u/20091124/11/76d2c2b2-378e-453f-ad2e-e2f954f0eb4d.html?seed=56049176&r=61387658#r_61387658