Click to See Complete Forum and Search --> : Mouse hooking


harsha.kadekar
April 16th, 2010, 10:35 AM
I need a dll to hook in all the applications and when the user clicks the left mouse button in any one of the application window like firefox or iexplorer it has to take the snap

my dll is hooking correctly but not intercepting the mouse events pls help me out

here is my code

// MouseHook1a.h --> this is the header file of dll file

#ifndef _MOUSEHOOK1a_H_
#define _MOUSEHOOK1a_H_

#include <windows.h>
#include <WindowsX.h>

extern "C" __declspec(dllexport) void CaptureScreen();
extern "C" __declspec(dllexport) void CaptureMouse(int x,int y);
extern "C" __declspec(dllexport) BOOL SetMouseHook(HWND hWnd);
extern "C" __declspec(dllexport) BOOL ClearMouseHook(HWND hWnd);


#endif

//MouseHook1a.cpp --> this is the .cpp file of dll

#include "MouseHook1a.h"

#pragma data_seg(".SHRD")
HWND hWndServer = NULL;

#pragma data_seg()

#pragma comment (linker, "/section:.SHRD,rws")

HINSTANCE hInst = NULL;
HHOOK hHook = NULL;

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);
BOOL __stdcall DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID reserved)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:hInst = hInstance;
return TRUE;

case DLL_PROCESS_DETACH:
case DLL_THREAD_DETACH:if(hWndServer!=NULL)
{
ClearMouseHook(hWndServer);
}
return TRUE;
}
}

BOOL SetMouseHook(HWND hWnd)
{
if(hWndServer!=NULL)
return FALSE;

hHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseProc,hInst,0);

if(hHook!=NULL)
{
hWndServer=hWnd;
return TRUE;
}
return FALSE;
}

BOOL ClearMouseHook(HWND hWnd)
{
if(hWnd!=hWndServer)
return FALSE;
BOOL UnHooked= UnhookWindowsHookEx(hHook);
if(UnHooked)
hWndServer=NULL;
return UnHooked;
}


LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPMSG msg;
MOUSEHOOKSTRUCT *MouseHookStruct;

if(nCode<0)
{
return CallNextHookEx( hHook, nCode, wParam, lParam );
}

msg = (LPMSG)wParam;
MouseHookStruct = (MOUSEHOOKSTRUCT *)lParam;

if(msg->message==WM_LBUTTONDOWN)
{
CaptureScreen();
CaptureMouse(MouseHookStruct->pt.x,MouseHookStruct->pt.y);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}


void CaptureScreen()
{
//code for taking snap
}

void CaptureMouse(int x ,int y)
{
//code for taking snap
}

SweetCorn
April 16th, 2010, 01:08 PM
Hi!

The reason it's not working is because wParam is not a LPMSG. use this:

if(wParam == WM_LBUTTONDOWN)

See if this works. (And you don't need to cast to LPMSG)

Cheers

harsha.kadekar
April 24th, 2010, 09:49 AM
Thank you :-)