CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Mouse hooking

  1. #1
    Join Date
    Apr 2010
    Posts
    8

    Question Mouse hooking

    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

    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
    }
    Last edited by Marc G; April 17th, 2010 at 10:17 AM. Reason: Added code tags

  2. #2
    Join Date
    Apr 2010
    Posts
    9

    Re: Mouse hooking

    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

  3. #3
    Join Date
    Apr 2010
    Posts
    8

    Re: Mouse hooking

    Thank you :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured