🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8021

Re: Help with Message hook

#8021
From: mike
Date: Tue, 07 Dec 2004 20:19
175 lines
6372 bytes
Remy,

Yikes, guess I am back to the drawing board on understanding this subject.
Thanks for taking the time to help me, I will now study the code, implement
it and work my way through it. I'll let you know how confused I get as I go
along.

Again thanks so much for helping.

Mike
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:41b5ebea$1@newsgroups.borland.com...
>
> "mike" <mfbrowne@earthlink.net> wrote in message
> news:41b53399@newsgroups.borland.com...
>
> > The code inside the Dll is defined as follows:
>
> That won't work.  For one thing, when setting up a system-wide global
hook,
> your DLL's data, in this case the HHOOK and HWND handles, must be in
shared
> memory.  You are not doing that, so your values for those handles are only
> available to your main application, not to all of the other applications
> that are being hooked.  Also, you need to export the hook procedure as
well,
> which you are currently not doing, either.
>
> > That is all the code. I have traced the DLL and it appears the
> > call to the DLL is working fine as I see it and the  hMsgFilterHook
> > gets an address assigned. But no messages are received within the
> > hook procedure.
>
> That is because the other application's can't find the hook procedure in
the
> first place, let alone the HWND and HHOOK values need to manage the hook
> properly..
>
> Try this code instead:
>
>     extern "C"
>     {
>         __declspec(dllexport) bool  __stdcall
> SetupFullWindowsMsgFilterHook(HWND MessageMapWindow);
>         __declspec(dllexport) void __stdcall
> RemoveFullWindowsMsgFilterHook();
>         __declspec(dllexport) LRESULT CALLBACK MessageProc(int nCode,
WPARAM
> wParam, LPARAM lParam);
>     }
>
>     #pragma pack(push, 1)
>     struct sGlobalHookData
>     {
>         HWND hWindowForMessages;
>         HHOOK hMsgFilterHook;
>     };
>     #pragma pack(pop)
>
>     HINSTANCE hInst = NULL;
>     HANDLE hMapping = NULL;
>     sGlobalHookData *MyGlobalHookData = NULL;
>
>     bool CreateMap()
>     {
>         hMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
> PAGE_READWRITE, 0, sizeof(sGlobalHookData), "MyGlobalMessageHookData");
>         if( hMapping )
>         {
>             MyGlobalHookData = (sGlobalHookData*)
::MapViewOfFile(hMapping,
> FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0);
>             if( !MyGlobalHookData )
>             {
>                 ::CloseHandle(hMapping);
>                 hMapping = NULL;
>             }
>         }
>         return (MyGlobalHookData != NULL);
>     }
>
>     void FreeMap()
>     {
>         if( MyGlobalHookData )
>         {
>             ::UnmapViewOfFile(MyGlobalHookData);
>             MyGlobalHookData = NULL;
>         }
>         if( hMapping )
>         {
>             ::CloseHandle(hMapping);
>             hMapping = NULL;
>         }
>     }
>
>     void SendMessageToWindow(const AnsiString &Str)
>     {
>         COPYDATASTRUCT cds = {0};
>         cds.dwData = 0x4D696B65;
>         cds.cbData = DataVal.Length();
>         cds.lpData = DataVal.c_str();
>         ::SendMessage(MyGlobalHookData->hWindowForMessages , WM_COPYDATA,
0,
> reinterpret_cast<LPARAM>(&cds));
>     }
>
>     int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
>     {
>         switch( reason )
>         {
>             case DLL_PROCESS_ATTACH:
>                 hInst = hinst;
>                 if( !CreateMap() )
>                     return FALSE;
>                 break;
>
>             case DLL_PROCESS_DETACH:
>                 FreeMap();
>                 break;
>         }
>
>         return TRUE;
>     }
>
>     bool  __stdcall SetupFullWindowsMsgFilterHook(HWND MessageMapWindow)
>     {
>         RemoveFullWindowsMsgFilterHook();
>         if( !MessageMapWindow ) return false;
>
>         MyGlobalHookData->hWindowForMessages = MessageMapWindow;
>         MyGlobalHookData->hMsgFilterHook =
::SetWindowsHookEx(WH_MSGFILTER,
> (HOOKPROC)MessageProc, hInst, 0);
>
>         return (MyGlobalHookData->hMsgFilterHook != NULL);
>     }
>
>     void __stdcall  RemoveFullWindowsMsgFilterHook()
>     {
>         MyGlobalHookData->hWindowForMessages = NULL;
>         if( MyGlobalHookData->hMsgFilterHook )
>         {
>             ::UnhookWindowsHookEx(MyGlobalHookData->hMsgFilterHook);
>             MyGlobalHookData->hMsgFilterHook = NULL;
>         }
>     }
>
>     LRESULT CALLBACK MessageProc(int nCode, WPARAM wParam, LPARAM lParam)
>     {
>         if( nCode >= 0 )
>         {
>             // this is only here to see if any messages are being seen
> inside the hook
>             SendMessageToWindow("Filter Hook ");
>
>             if( nCode == MSGF_MENU )
>             {
>                 SendMessageToWindow("Filter Hook -- Menu Message");
>
>                 CWPSTRUCT *cwpsParam = reinterpret_cast<CWPSTRUCT
> *>(lParam);
>                 if( cwpsParam->message == WM_MENUSELECT )
>                     SendMessageToWindow("Filter Hook -- Menu Select
> Message");
>             }
>         }
>
>         return CallNextHookEx(MyGlobalHookData->hMsgFilterHook, nCode,
> wParam, lParam);
>     }
>
>
> Gambit
>
>
Technical Details
Message-ID: <41b60ffc$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!newsread.com!newsprint.newsread.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41b2766c$1@newsgroups.borland.com> <41b412e1$1@newsgroups.borland.com> <41b4fee6@newsgroups.borland.com> <41b50838@newsgroups.borland.com> <41b53399@newsgroups.borland.com> <41b5ebea$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,21323583e8de481a
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "mike" <mfbrowne@earthlink.net>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <41b2766c$1@newsgroups.borland.com> <41b412e1$1@newsgroups.borland.com> <41b4fee6@newsgroups.borland.com> <41b50838@newsgroups.borland.com> <41b53399@newsgroups.borland.com> <41b5ebea$1@newsgroups.borland.com>
Subject: Re: Help with Message hook
Date: Tue, 7 Dec 2004 15:19:05 -0500
Lines: 177
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 68.204.189.169
Message-ID: <41b60ffc$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1102450684 68.204.189.169 (7 Dec 2004 12:18:04 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!newsread.com!newsprint.newsread.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail