Article View: borland.public.delphi.nativeapi
Article #12071Re: How to Hook other program window WindowProc under win nt/2k/xp?
From: Scott Maskiel
Date: Mon, 25 Nov 2002 10:16
Date: Mon, 25 Nov 2002 10:16
44 lines
2258 bytes
2258 bytes
MS wrote:
> Hi,
> we can use GetWindowLong(AHandle,GWL_WNDPROC) and
> SetWindowLong(AHandle,GWL_WNDPROC,AWndProc) to Hook an window wndproc in our
> owner program,But how to do it for other program window?
> Example,I want to hook NOTEPAD's main window WNDPROC,the under code can't do
> it:
>
> var
> OldWndProc:Pointer;
>
> function
> NewWndProc(AHwnd:HWND;uMsg:UINT;wParam:WPARAM;lParam:LPARAM):UINT;stdcall;
> begin
> ////Do myself something....
> CallWindowProc(OldWndProc,AHwnd,uMsg,wParam,lParam);
> end;
>
> procedure TForm1.Button1Click(Sender:TObject);
> var
> H:HWND;
> begin
> h:=FindWindow('Notepad',nil);
> if h=0 then exit;
> OldWndProc:=GetWindowLong(h,GWL_WNDPROC); /////Error : Access denied
> SetWindowLong(h,GWL_WNDPROC,integer(@NewWndProc));
> end;
>
> ......
>
> I'm sorry for my english.
>
> Thanks in advance.
>
>
SetWindowLong must be executed in the context of the process that
contains the window thread, so you cannot subclass Notepad's main window
using it.
Instead, use SetWindowsHookEx, with the hook procedure being called from
a DLL you have written.
Regards,
Scott
Technical Details
Message-ID:
<3de1f89a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mailReferences:
<3de1e554@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f94dd,7c920ad525001973 X-Google-Attributes: gidf94dd,public X-Google-ArrivalTime: 2002-11-25 03:15:54 PST Date: Mon, 25 Nov 2002 21:16:52 +1100 From: Scott Maskiel <scott_maskiel@somedomain.com.au> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.delphi.nativeapi Subject: Re: How to Hook other program window WindowProc under win nt/2k/xp? References: <3de1e554@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 210.49.88.70 Message-ID: <3de1f89a$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1038219418 210.49.88.70 (25 Nov 2002 02:16:58 -0800) Lines: 45 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail