Article View: borland.public.cppbuilder.nativeapi
Article #10423MouseHook,Message From DLL to Application while not on Focus
From: Alex Sarafian
Date: Thu, 29 Apr 2004 09:43
Date: Thu, 29 Apr 2004 09:43
273 lines
8776 bytes
8776 bytes
Hello.
i'm trying to write a global hook application, and i'm focused on the
mouse. I just for now want my application to be aware of mouse events,
whether the mouse is on top of my application or not.
I have studied various aspected of SetGlobalMouseHooxEx and that the
handler should be in a DLL domain.
The dll recieves all mouse messages (set the handler to beep and it
does).Then the handler posts a message to my applications handler that
has been stored in a global variable. The message sent is a static
UINT returned by RegisterWindowMessage(Text) where Text is common in
DLL and Application (same header file is included).
The problem is that the application handles the messages only when my
application windows is focused and when i switch to other applications
stops.
Two problems have benn noticed.When s\witching to other application
they are freezed and the wierd thing is that if my mouse gets on top
of My application Window while its not focused it recieves the
messages generated by the mouse handler in the dll.
Here is the code for the header file of the dll
#ifndef MouseHookDLLH
#define MouseHookDLLH
#ifdef __BUILDING_THE_DLL
#define __EXPORT_TYPE __declspec(dllexport)
#define A 20
#else
#define __EXPORT_TYPE __declspec(dllimport)
#define A 10
#endif
extern "C" __EXPORT_TYPE BOOL setMyHook(HWND);
extern "C" __EXPORT_TYPE BOOL clearMyHook(HWND);
#undef LIBSPEC
typedef BOOL (*SETMOUSEHOOK)(HWND);
typedef BOOL (*CLEARMOUSEHOOK)(HWND);
typedef void (*SENDCUSTOMMESSAGE)(HWND);
#define UWM_MOUSEMOVE_MSG "MouseMove"
static UINT UWM_MOUSEMOVE;
#endif
And here is the code of the source file of the DLL
//--------------------------------------------------------------------------
-
#include <windows.h>
#include <stdio.h>
#define __BUILDING_THE_DLL
#include "MouseHookDLL.h"
HWND hWndServer = NULL;
HINSTANCE hInst;
HHOOK hook;
static LRESULT CALLBACK MessageHook(int nCode, WPARAM wParam, LPARAM
lParam);
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
// char s[10];
switch(reason)
{
case DLL_PROCESS_ATTACH:{
hInst = hinst;
UWM_MOUSEMOVE =
RegisterWindowMessage(UWM_MOUSEMOVE_MSG);
// sprintf(s,"%i", UWM_MOUSEMOVE);
// MessageBox(NULL,s,"",MB_OK);
return TRUE;
}
case DLL_PROCESS_DETACH: {
if(hWndServer != NULL)
clearMyHook(hWndServer);
return TRUE;
}
} return TRUE;
}
//--------------------------------------------------------------------------
-
extern "C" __EXPORT_TYPE BOOL setMyHook(HWND hWnd)
{
if(hWndServer != NULL)
return FALSE; // already hooked!
hook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MessageHook,hInst,0);
Sleep(500);
if(hook != NULL)
{
hWndServer = hWnd;
return TRUE;
} return FALSE; // failed to set hook
}
extern "C" __EXPORT_TYPE BOOL clearMyHook(HWND hWnd)
{
if(hWnd != hWndServer || hWnd == NULL)
return FALSE;
BOOL unhooked = UnhookWindowsHookEx(hook);
if(unhooked)
hWndServer = NULL;
return unhooked;
}
static LRESULT CALLBACK MessageHook(int nCode, WPARAM wParam, LPARAM
lParam)
{
if(nCode < 0)
{
CallNextHookEx(hook, nCode, wParam, lParam);
return 0;
}
PostMessage(hWndServer, UWM_MOUSEMOVE, 0, 0);
return CallNextHookEx(hook, nCode, wParam, lParam);
}
And here is the code of the main applications header file
//--------------------------------------------------------------------------
-
#ifndef MouseHookFormH
#define MouseHookFormH
//--------------------------------------------------------------------------
-
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "MouseHookDLL.h"
#include <AppEvnts.hpp>
#include <ExtCtrls.hpp>
//--------------------------------------------------------------------------
-
class TForm1 : public TForm
{
__published: // IDE-managed Components
TCheckBox *MouseHookCheckBox;
TEdit *Edit1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall MouseHookCheckBoxClick(TObject *Sender);
void __fastcall HandleMessages(MSG &Msg, bool &Handled);
void __fastcall WndProc(Messages::TMessage& Message);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
HINSTANCE DLLHinstance;
SETMOUSEHOOK SetMouseHook;
CLEARMOUSEHOOK ClearMouseHook;
};
//--------------------------------------------------------------------------
-
extern PACKAGE TForm1 *Form1;
//--------------------------------------------------------------------------
-
#endif
And the source file cpp of Application
//--------------------------------------------------------------------------
-
#include <vcl.h>
#pragma hdrstop
#include "MouseHookForm.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner) {
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DLLHinstance = LoadLibrary("MouseHookDLLPro.dll");
if( DLLHinstance!=NULL)
{
SetMouseHook = (SETMOUSEHOOK)
GetProcAddress((HMODULE)DLLHinstance, "_setMyHook");
if(SetMouseHook==NULL)
MessageBoxA(this->Handle,AnsiString(GetLastError()).c_str(),"Set",MB_OK);
ClearMouseHook = (CLEARMOUSEHOOK)
GetProcAddress((HMODULE)DLLHinstance, "_clearMyHook");
if(ClearMouseHook==NULL)
MessageBoxA(this->Handle,AnsiString(GetLastError()).c_str(),"Clear",MB_OK);
}
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction
&Action)
{
// MessageBoxA(this->Handle,"SK","SK:",MB_OK);
ClearMouseHook(this->Handle);
// MessageBoxA(this->Handle,"SK","SK:",MB_OK);
FreeLibrary(DLLHinstance);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::MouseHookCheckBoxClick(TObject *Sender)
{
if(MouseHookCheckBox->Checked)
{
UWM_MOUSEMOVE= RegisterWindowMessage(UWM_MOUSEMOVE_MSG);
Edit1->Text=AnsiString(UWM_MOUSEMOVE).c_str();
// Application->OnMessage = HandleMessages;
SetMouseHook(this->Handle);
}
else
ClearMouseHook(this->Handle);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::HandleMessages(MSG &Msg, bool &Handled)
{
MessageBeep(MB_OK);
if (Msg.message == UWM_MOUSEMOVE)
{
// MessageB
// do message processing here
Handled = true;
}
}
void __fastcall TForm1::WndProc(Messages::TMessage& Message)
{
if(Message.Msg == UWM_MOUSEMOVE)
{
MessageBeep(MB_OK);
Message.Result = true;
}
else
TForm::WndProc(Message);
}
Finally i must say that i have tried an alternative as seen by the
above code.
I used the Application->OnMessage property to set it to a function
called HandleMessages, but also didn't work.
I tried also using a component the ApplicationEvents component but
that also didn't work.
Any help would be appriciated.
Technical Details
Message-ID:
<4090ce56@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,ae4b21ed9340f943,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-04-29 03:09:31 PST From: "Alex Sarafian" <demelene_gr@yahoo.gr> Newsgroups: borland.public.cppbuilder.nativeapi Subject: MouseHook,Message From DLL to Application while not on Focus Date: Thu, 29 Apr 2004 12:43:55 +0300 Lines: 275 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: <4090ce56@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1083231830 150.140.3.118 (29 Apr 2004 02:43:50 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail