Article View: borland.public.cppbuilder.nativeapi
Article #8019Re: Help with Message hook
From: Remy Lebeau (TeamB)
Date: Tue, 07 Dec 2004 17:43
Date: Tue, 07 Dec 2004 17:43
153 lines
5627 bytes
5627 bytes
"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:
<41b5ebea$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41b2766c$1@newsgroups.borland.com> <41b412e1$1@newsgroups.borland.com> <41b4fee6@newsgroups.borland.com> <41b50838@newsgroups.borland.com> <41b53399@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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> 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> Subject: Re: Help with Message hook Date: Tue, 7 Dec 2004 09:43:37 -0800 Lines: 155 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <41b5ebea$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1102441450 68.69.17.41 (7 Dec 2004 09:44:10 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail