Article View: borland.public.cppbuilder.nativeapi
Article #10233Re: MouseHook,Message From DLL to Application while not on Focus
From: Remy Lebeau (TeamB)
Date: Thu, 06 May 2004 18:05
Date: Thu, 06 May 2004 18:05
132 lines
4687 bytes
4687 bytes
"Alex Sarafian" <demelene_gr@yahoo.gr> wrote in message
news:409a0030$1@newsgroups.borland.com...
> Since the handle of the parent window is passed in every dll instance,
No, it is not. It is only set for the instance of the DLL that is loaded
into the application that is actually calling setHook(). Which means only
the instances attached to your own application will have a valid HWND handle
available. No other DLL instance will have their HWND handle set, unless
you put the HWND into shared memory like I explained to you earlier.
> I read about CreateFileMapping and MapViewOfFile but I'm
> having some trouble, so if you can help?
> Where is the file mapping must be created??
Create and destroy it in your DLLEntryPoint() function, so that the memory
is always available as soon as the DLL is loaded.
typedef struct
{
HWND hWnd;
HHOOK hHook;
} TMyHookInfo, *PMyHookInfo;
HANDLE hMapping = NULL;
PMyHookInfo pHookInfo = NULL;
HINSTANCE hInst = NULL;
UINT UWM_MOUSEMOVE = 0;
bool CreateSharedHookInfo()
{
hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, 0, sizeof(TMyHookInfo), "MySharedHookInfo");
if( hMapping == NULL )
return false;
bool bIsNewMapping = (GetLastError() != ERROR_ALREADY_EXISTS);
pHookInfo = (PMyHookInfo) MapViewOfFile(hMapping, FILE_MAP_WRITE, 0,
0, 0);
if( pHookInfo == NULL )
{
CloseHandle(hMapping);
hMapping = NULL;
return false;
}
if( bIsNewMapping )
{
pHookInfo->hWnd = NULL;
pHookInfo->hHook = NULL;
}
return true;
}
}
return FALSE;
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
switch( reason )
{
case DLL_PROCESS_ATTACH:
hInst = hinst;
UWM_MOUSEMOVE = RegisterWindowMessage(UWM_MOUSEMOVE_MSG);
if( UWM_MOUSEMOVE == 0 )
return FALSE;
if( !CreateSharedHookInfo() )
return FALSE;
break;
case DLL_PROCESS_DETACH:
if( pHookInfo != NULL ) UnmapViewOfFile(pHookInfo);
if( hMapping != NULL ) CloseHandle(hMapping);
break;
}
return TRUE;
}
extern "C" __EXPORT_TYPE BOOL __stdcall setMyHook(HWND hWnd)
{
if( (pHookInfo) && (pHookInfo->hHook == NULL) )
{
pHookInfo->hHook = SetWindowsHookEx(WH_MOUSE,
(HOOKPROC)MessageHook, hInst, 0);
if( pHookInfo->hHook != NULL )
{
pHookInfo->hWnd = hWnd;
return TRUE;
}
}
return FALSE;
}
extern "C" __EXPORT_TYPE BOOL __stdcall clearMyHook(HWND hWnd)
{
if( (pHookInfo) && ((pHookInfo->hWnd == hWnd) || (hWnd == NULL)) )
{
if( UnhookWindowsHookEx(pHookInfo->hHook) )
{
pInfo->hWnd = NULL;
pInfo->hHook = NULL;
return TRUE;
}
}
return FALSE;
}
extern "C" LRESULT CALLBACK MessageHook(int nCode, WPARAM wParam, LPARAM
lParam)
{
if( pHookInfo )
{
if( nCode >= 0 )
PostMessage(pHookInfo->hWnd, UWM_MOUSEMOVE, 0, 0);
return CallNextHookEx(pHookInfo->hHook, nCode, wParam, lParam);
}
return 0;
}
Gambit
Technical Details
Message-ID:
<409a7fd0$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4090ce56@newsgroups.borland.com> <409152c9$1@newsgroups.borland.com> <409a0030$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,ae4b21ed9340f943 X-Google-Attributes: gid10cc67,public From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4090ce56@newsgroups.borland.com> <409152c9$1@newsgroups.borland.com> <409a0030$1@newsgroups.borland.com> Subject: Re: MouseHook,Message From DLL to Application while not on Focus Date: Thu, 6 May 2004 11:05:48 -0700 Lines: 134 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: 67.22.138.253 Message-ID: <409a7fd0$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1083867088 67.22.138.253 (6 May 2004 11:11:28 -0700) Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail