Article View: borland.public.cppbuilder.nativeapi
Article #10179Re: MouseHook,Message From DLL to Application while not on Focus
From: Remy Lebeau (TeamB)
Date: Thu, 29 Apr 2004 19:02
Date: Thu, 29 Apr 2004 19:02
100 lines
4458 bytes
4458 bytes
"Alex Sarafian" <demelene_gr@yahoo.gr> wrote in message
news:4090ce56@newsgroups.borland.com...
> The problem is that the application handles the messages
> only when my application windows is focused and when
> i switch to other applications stops.
You probably did not set asside a block of shared memory. Every application
is going to get a separate copy of your global DLL. Every copy of the DLL
is going to have its own separate variables from ohter instances of the same
DLL. You need to set asside a block of shared memory (look at
CreateFileMapping() and MapViewOfFile()), and put your application HWND and
HHOOK handles into the shared memory. Then each individual DLL instance can
call RegisterWindowMessage() when it is first loaded into a new process
(which you are already doing ok), and use the shared memory to know where to
send the message and which hook handle to pass to CallNextHookEx().
> extern "C" __EXPORT_TYPE BOOL setMyHook(HWND);
> extern "C" __EXPORT_TYPE BOOL clearMyHook(HWND);
You should be using the __stdcall calling convention as well:
extern "C" __EXPORT_TYPE BOOL __stdcall setMyHook(HWND);
extern "C" __EXPORT_TYPE BOOL __stdcall clearMyHook(HWND);
> typedef BOOL (*SETMOUSEHOOK)(HWND);
> typedef BOOL (*CLEARMOUSEHOOK)(HWND);
Likewise:
typedef BOOL (__stdcall *SETMOUSEHOOK)(HWND);
typedef BOOL (__stdcall *CLEARMOUSEHOOK)(HWND);
> SetMouseHook = (SETMOUSEHOOK)
> GetProcAddress((HMODULE)DLLHinstance, "_setMyHook");
> HINSTANCE hInst;
> HHOOK hook;
Those need to be put into shared memory.
> static LRESULT CALLBACK MessageHook(int nCode,
> WPARAM wParam, LPARAM lParam);
You need to export your callback function.
> case DLL_PROCESS_DETACH: {
> if(hWndServer != NULL)
> clearMyHook(hWndServer);
> return TRUE;
> }
Do not ever let the DLL unregister the hook. Your main application should
be the only one doing that. The application should be calling the DLL's
exported functions to register and unregister the hook, and then the DLL
simply processes the hook events otherwise.
> void __fastcall TForm1::FormCreate(TObject *Sender)
Do not use the OnCreate event in C++. It is a Delphi remnant that is
unstable in C++ as it can be triggered before the constructor, which is
illegal in C++. Use the constructor instead.
> SetMouseHook = (SETMOUSEHOOK)
> GetProcAddress((HMODULE)DLLHinstance, "_setMyHook");
If you use __stdcall, then you don't use the underscore in the exported name
anymore:
SetMouseHook = (SETMOUSEHOOK) GetProcAddress(DLLHinstance, "setMyHook");
> ClearMouseHook = (CLEARMOUSEHOOK)
> GetProcAddress((HMODULE)DLLHinstance, "_clearMyHook");
Likewise:
ClearMouseHook = (CLEARMOUSEHOOK) GetProcAddress(DLLHinstance,
"clearMyHook");
> ClearMouseHook(this->Handle);
You should be checking to make sure that ClearMouseHook() and SetMouseHook()
are not NULL before calling them.
> Edit1->Text=AnsiString(UWM_MOUSEMOVE).c_str();
You don't need to cast:
Edit1->Text = UWM_MOUSEMOVE;
> if (Msg.message == UWM_MOUSEMOVE)
You need to make sure that RegisterWindowMessage() does not return 0,
otherwise your code will be handling and blocking WM_NULL messages instead,
which the VCL uses internally for other purposes.
Gambit
Technical Details
Message-ID:
<409152c9$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4090ce56@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 X-Google-ArrivalTime: 2004-04-29 12:10:08 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4090ce56@newsgroups.borland.com> Subject: Re: MouseHook,Message From DLL to Application while not on Focus Date: Thu, 29 Apr 2004 12:02:59 -0700 Lines: 102 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: <409152c9$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1083265737 67.22.138.253 (29 Apr 2004 12:08:57 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail