Article View: borland.public.cppbuilder.nativeapi
Article #10243Re: MouseHook,Message From DLL to Application while not on Focus
From: Alex Sarafian
Date: Fri, 07 May 2004 09:18
Date: Fri, 07 May 2004 09:18
143 lines
5296 bytes
5296 bytes
Gambit You have been very thankfull. It works finally.
But thanks for you, i now understand better some aspects of Windows API.
Thank you very much. Much obliged
"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> wrote in message
news:409a7fd0$1@newsgroups.borland.com...
>
> "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:
<409b545c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!controlnews3.google.com!news2.google.com!news1.google.com!news.glorb.com!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4090ce56@newsgroups.borland.com> <409152c9$1@newsgroups.borland.com> <409a0030$1@newsgroups.borland.com> <409a7fd0$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: "Alex Sarafian" <demelene_gr@yahoo.gr> Newsgroups: borland.public.cppbuilder.nativeapi References: <4090ce56@newsgroups.borland.com> <409152c9$1@newsgroups.borland.com> <409a0030$1@newsgroups.borland.com> <409a7fd0$1@newsgroups.borland.com> Subject: Re: MouseHook,Message From DLL to Application while not on Focus Date: Fri, 7 May 2004 12:18:31 +0300 Lines: 145 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: 150.140.3.118 Message-ID: <409b545c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1083921500 150.140.3.118 (7 May 2004 02:18:20 -0700) Path: arc1-ca.pugleaf.net!controlnews3.google.com!news2.google.com!news1.google.com!news.glorb.com!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail