我的钩子左钩右钩就是钩不到,请大伙帮帮忙!!(200分)

  • 主题发起人 主题发起人 叶不归
  • 开始时间 开始时间

叶不归

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp;我想控制一个外部程序,就是我的文档这个窗口,我要在该窗口点击“叉”按钮时,<br>不让它关闭,而是发出一声beep(0),做了一个钩子,可是钩不到。<br>&nbsp; &nbsp; 我是用HCBT消息去钩,回调函数有了,都在dll中写了,HHook和Handle都在一个记<br>录中,都mapfile到共享内存中,查找了以往的贴子好多遍,例子也很多,可是没用!<br>&nbsp; &nbsp; 有一个例子特怪,<br>&nbsp; &nbsp; 1. 当我点击我的程序关闭按钮时,却发出了beep。为什么?<br>&nbsp; &nbsp; 2. 也有人是用mouse钩子,好像可以实现,可是我想能不能用HCBT实现呢??<br>&nbsp; &nbsp; 3. 有没有哪位做过的,已经实现了的,请相助....<br>另外,我明天贴源码出来,请帮忙看看是哪不对了...
 
//dll<br>library Hook;<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; HookProc in 'HookProc.pas';<br><br>{$R *.RES}<br><br>exports<br>&nbsp; SetHook,<br>&nbsp; EndHook,<br>&nbsp; SetHandled;<br><br>begin<br>&nbsp; IntoShare;<br>end.<br><br>//dll proc.pas<br>unit HookProc;<br><br>interface<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; Windows,<br>&nbsp; Dialogs,<br>&nbsp; Messages;<br><br>function MyCBTHookProc(nCode: Integer; wp: wParam; lp: lParam): LResult; stdcall;<br>procedure SetHook;export;<br>procedure EndHook;export;<br>procedure IntoShare; stdcall;export;<br>procedure SetHandled( H: THandle );stdcall; export;<br><br>implementation<br><br>type<br>&nbsp; TGoData = record<br>&nbsp; &nbsp; TargetWnd : Hwnd;<br>&nbsp; &nbsp; ghHook : HHook;<br>&nbsp; end;<br>&nbsp; PGoData = ^TGoData;<br><br>{const<br>&nbsp; TARGET_WND_TITLE = '我的文档'; &nbsp; &nbsp; }<br><br>var<br>&nbsp; GoData : PGoData;<br>&nbsp; MemFile : THandle;<br><br>procedure SetHandled( H: THandle );stdcall; export;<br>begin<br>&nbsp; GoData^.TargetWnd := H;<br>&nbsp; if GoData^.TargetWnd &lt;&gt; 0 then<br>&nbsp; &nbsp; MessageBeep(0);<br>end;<br><br>function MyCBTHookProc(nCode: Integer; wp: wParam; lp: lParam): LResult; stdcall;<br>{const<br>&nbsp; TargetFocused : Boolean = False; &nbsp; }<br>begin<br>{ &nbsp;if ( nCode = HCBT_ACTIVATE ) then<br>&nbsp; begin<br>&nbsp; &nbsp; GoData^.TargetWnd := FindWindow( nil, PChar( TARGET_WND_TITLE ));<br>&nbsp; &nbsp; if GoData^.TargetWnd &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; TargetFocused := True;<br>&nbsp; end;<br><br>&nbsp; Result := CallNextHookEx( GoData^.ghHook, nCode, wP, lP); &nbsp; &nbsp;}<br>&nbsp; if ( nCode = HCBT_ACTIVATE ) then<br>&nbsp; begin<br>&nbsp;// &nbsp; MessageBeep(0);<br>&nbsp; &nbsp; if {(nCode = HCBT_SYSCOMMAND) and} ( PMsg(lp)^.Message = WM_CLOSE) and ( GoData^.TargetWnd = PMsg(lP)^.hwnd ) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageBeep(0);<br>&nbsp; &nbsp; &nbsp; ShowWindow( GoData^.TargetWnd, SW_SHOWMINIMIZED );<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; Result := CallNextHookEx( GoData^.ghHook, nCode, wP, lP);<br>end;<br><br>procedure SetHook;export;<br>begin<br>&nbsp; GoData^.ghHook := SetWindowsHookEx( WH_CBT, @MyCBTHookProc, hInstance, 0);<br>end;<br><br>procedure EndHook;export;<br>begin<br>&nbsp; UnhookWindowsHookEx( GoData^.ghHook );<br>end;<br><br>procedure IntoShare; stdcall;export;<br>begin<br>&nbsp; MemFile := OpenFileMapping( FILE_MAP_WRITE, False, 'JJYY' );<br>&nbsp; if MemFile = 0 then<br>&nbsp; &nbsp; MemFile:=CreateFileMapping( $FFFFFFFF, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PAGE_READWRITE, 0, SizeOf( TGoData ), 'JJYY');<br>&nbsp; GoData := MapViewOfFile( MemFile, FILE_MAP_WRITE, 0, 0, 0 );<br>&nbsp; if MemFile = 0 then<br>&nbsp; FillChar( GoData^, SizeOf( TGoData ),0);<br>end;<br><br>end.<br><br>//调用<br>procedure SetHandled( H : THandle ); stdcall;<br>external 'Hook.dll';<br><br>procedure SetHook; stdcall;<br>external 'Hook.dll';<br><br>procedure EndHook; stdcall;<br>external 'Hook.dll';<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; H : THandle;<br>begin<br>&nbsp; H := FindWindow( nil, 'Test' );<br>&nbsp; if H &lt;&gt; 0 then ShowMessage( 'H&lt;&gt;0' );<br>&nbsp; SetHandled( H );<br>&nbsp; SetHook;<br>end;<br><br>请高手相助,嫌分不够可再加!!!!!
 
不会吧,高手哪去了????<br>怎么没人回答????<br>温柔一刀、hubdog、沈前卫、Another_eYes 你们在不在,<br>以前的回答解决不了问题啊,还有谁懂得的,说一下吧!
 
SetWindowsHookEx( WH_CBT, @MyCBTHookProc, hInstance, 0);<br>这句好像不对,第三个参数不应该是hInstance,这样表示这个hook本程序中有效,<br>好像应使用0或者nil,表示是全局的。
 
To: Pan Ying<br>我试了一下,把hInstance改为0或GoData^.TargeWnd都不行啊。<br>
 
高手高手你在哪?
 
注意到有这样一个判断语句GoData^.TargetWnd = PMsg(lP)^.hwnd <br>好像表示要点击的窗口必须为设置的窗口才行,<br>去掉此判断再试试。
 
再没有人回答我要把分收回来了!<br>太另人失望了,高手都小气.........
 
function MyCBTHookProc(nCode: Integer; wp: wParam; lp: lParam): LResult; stdcall;<br>begin<br>&nbsp; begin<br>&nbsp; &nbsp; if (( PCWPStruct(lp)^.Message = WM_CLOSE) or ( PCWPStruct(lp)^.Message = WM_DESTROY)or ( PCWPStruct(lp)^.Message = WM_QUIT)) and ( GoData^.TargetWnd = PCWPStruct(lP)^.hwnd ) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageBeep(0);<br>&nbsp; &nbsp; &nbsp; showmessage(inttostr(GoData^.TargetWnd)+'test');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; Result := CallNextHookEx( GoData^.ghHook, nCode, wP, lP);<br>end;<br><br>procedure SetHook;export;<br>begin<br>&nbsp; GoData^.ghHook := SetWindowsHookEx( WH_CALLWNDPROC , @MyCBTHookProc,hInstance, 0);<br>&nbsp; showmessage(inttostr(GoData^.ghHook));<br>end;<br><br>首先要说明的是WH_CBT是不能拦截WM_Close的,用WH_CALLWNDPROC 吧,可以挂到,但是有个问题<br>没有调用CallNextHookEx( GoData^.ghHook, nCode, wP, lP);为什么该窗口依然被关闭?有没有人知道<br><br>to:叶不归, <br>(你用的的HCBT_ACTIVATE,没有消息体,不知道你怎么会这样用!不要完全照抄例子)<br><br>
 
非常感谢zj1978!!<br>问题已解决,你说的问题我没碰到,估计是你在Exit时没有UnHook吧。<br>好了,问题结束,分是你的。
 
后退
顶部