Bonjour,
je rencontre actuellement un petit soucis avec un hook. J'essaye de poser un hook souris, et d'effectuer un traitement lorsque le pointeur de la souris entre dans une certaine zone.
Le code du hook est contenu dans ma dll, et j'ai fait un petit programme de test, qui, lorsque que je clique sur un bouton, appelle la procédure d'installation du hook qui se trouve dans la dll. C'est lors de l'appel de la fonction SetWindowsHookEx que ca pose problème, le handle est égal a NULL, et je ne comprend pas pourquoi ...
Voici le code de ma dll :
[code]
//---------------------------------------------------------------------------
#include <windows.h>
#pragma hdrstop
// Exported functions
extern "C" __declspec(dllexport)bool InstallMouseHook();
extern "C" __declspec(dllexport)bool RemoveMouseHook();
// Declarations
LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam);
// Global variables
HHOOK HookHandle;
HINSTANCE DllInstance;
bool IsInRect=false;
QList<QList<QList<QAction *>>> Menus;
QRect Rect1;
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
DllInstance=hinst;
return 1;
}
//---------------------------------------------------------------------------
bool InstallMouseHook()
{
HookHandle=SetWindowsHookEx(WH_MOUSE,
reinterpret_cast<HOOKPROC>(MouseProc),DllInstance,0);
if (HookHandle==NULL)
{
MessageBox(0,"bali balo","Whatever",MB_OK | MB_ICONEXCLAMATION);
return false;
}
else return true;
}
//---------------------------------------------------------------------------
bool RemoveMouseHook()
{
if(UnhookWindowsHookEx(HookHandle)==0)
{
MessageBox(0,"balo bali","Whatever",MB_OK | MB_ICONEXCLAMATION);
return false;
}
else return true;
}
//---------------------------------------------------------------------------
void SendArray(QList<QList<QList<QAction * >>>tab)
{
Menus = tab;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code<0)
{
return CallNextHookEx(HookHandle,code,wParam,lParam);
}
QDesktopWidget *Bureau= new QDesktopWidget();
int x = Bureau->primaryScreen();
Rect1 = Bureau->availableGeometry(x);
QRect *Rect2 = new QRect(Rect1.x(),0,1,1);
QRect *Rect3 = new QRect(0,0,200,200);
POINT MousePos;
GetCursorPos(&MousePos);
MessageBox(0,"dll marche ","Whatever",MB_OK | MB_ICONEXCLAMATION);
HWND hWndUnder =WindowFromPoint(MousePos);
if ( hWndUnder == NULL)
{
if ( Rect3->contains(MousePos.x,MousePos.y))
{
MessageBox(0,"dans le rectangle","Whatever",MB_OK | MB_ICONEXCLAMATION);
}
return CallNextHookEx(HookHandle,code,wParam,lParam);
}
//---------------------------------------------------------------------------
[/code]
Et le code du bouton de mon programme de test :
[code]
void Mainwindow::mettre()
{
MyPrototype myFunction = (MyPrototype) myLib->resolve("InstallMouseHook");
if (myLib->isLoaded())
{
infoLabel->setText("lib chargée");
}
else
{
infoLabel->setText("lib non chargée");
}
if (myFunction())
{
infoLabel2->setText("fct chargée");
}
else infoLabel2->setText("fct non chargée");
}
[/code]
J'utilise Qt pour effectuer des choses un peu plus complexe, je devraia créer un menu à partir d'un xml par la suite dans la fonction mouseproc, au lieu de faire simplement une messagebox...