A sytem wide MouseProc

This is a discussion on A sytem wide MouseProc within the Windows Programming forums, part of the Platform Specific Boards category; Someone can help me to make a system wide MouseProc with VC++?? Here is the code i wrote: the DLL ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    35

    A sytem wide MouseProc

    Someone can help me to make a system wide MouseProc with VC++??

    Here is the code i wrote:

    the DLL - main.c

    Code:
    #include <windows.h>
    
    #include "DLL.h"
    
    // tutte le funzioni devono essere precedute da __stdcall
    LRESULT CALLBACK __stdcall MouseProc (int nCode, WPARAM wParam, LPARAM lParam) 
    {
    	if (nCode == HC_ACTION) 
    	{
    		if (wParam == WM_LBUTTONDOWN) 
    		{
    			MessageBox (NULL, "ciao", "ciao", MB_OK);
    			return 1;
    		}
    	}
    
    	return CallNextHookEx (hMouseHook, wParam, lParam);
    }
    here I have a problem..... VC++ says hMouseHook is an undeclared variable. ok it is true, but How can I make it a non-undeclared variable?

    The DLL - exports.def

    Code:
    LIBRARY "DLL.dll"
    
    EXPORTS
    	MouseProc
    The DLL - DLL.h
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK __stdcall MouseProc (int nCode, WPARAM wParam, LPARAM lParam);
    The function calling the MouseProc:

    Code:
    void hello ()
    {
    	MYFUNCTION pFunction = 0;
    	HINSTANCE hDLL;
    	HOOKPROC a;
    	HHOOK hMouseHook
    
    	hDLL = LoadLibrary ("DLL.dll");
    
    	a = (HOOKPROC)GetProcAddress (hDLL, "MouseProc");
    	b = SetWindowsHookEx (WH_MOUSE, a, hDLL, 0);
    }
    I forgot to say that there's also this line at the beginning of the program

    typedef void (*MYFUNCTION)(int, WPARAM, LPARAM);

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Ok, so you realise that the HHOOK must exist in the same module (the dll) as the hook procedure......now this means that you must also include the functions to launch and kill that hook in the dll also (these will become exported functions)...now for another problem ; That hook must exist as the same value in all instances of that dll, and as the dll is injected to every process that recieves mouse messages, that value must exist in a shared section....

    Its a fair bit of work, but I already did this in an app a while ago here. Have a look...that will show you how to setup the section and include the functions to control the hook

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    I have some questions about your code:

    1) what are the differences between __stdcall and __declspec??

    2) what are the differences between the globals variables and the shared variable?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by darcome
    I have some questions about your code:

    1) what are the differences between __stdcall and __declspec??
    They are completely different...

    __stdcall (STDCALL) tells the compiler & linker how to pass the params to the function and says that this function is responsible for stack balancing.....C++ compilers use __cdecl by deafult, with that the calling function is responsible for stack balancing.

    WINAPI calls use STDCALL...thats why the headers include this in their declaration in one way ro another

    __declspec declares attributes of certain things.......like you may declare an exported function __declspec(dllexport)...that tells the compiler/linker that the function is to be exported, and therefore you dont need an def file

    Originally posted by darcome


    2) what are the differences between the globals variables and the shared variable?
    Global variables are available to every peice of code in that module.....

    Shared variables are for dlls, where every module that's loaded can share this value at runtime

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    yuo mean that the shared variables, can be used also in the main program also if they have been declared in the DLL, right??

    Another question, my MouseProc is this:

    Code:
    LRESULT CALLBACK __stdcall MouseProc (int nCode, WPARAM wParam, LPARAM lParam) 
    {
    	if (nCode == HC_ACTION) 
    	{
    		if (wParam == WM_LBUTTONDOWN)
    		{
    			MessageBox (NULL, "ciao", "ciao", MB_OK);
    			return 1;
    		}
    	}
    
    	return CallNextHookEx (hMouseHook,nCode,wParam,lParam);
    }
    It display a message if the Left button is pressed. But the message is displayed if I move the mouse too. so the fesktop fills with a lots of messages. Why??

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    Ops, I forgot the main thing.....

    Thank you for your help ! ! !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ToUnicodeEx and dead keys inside system wide hooks
    By RevengerPT in forum Windows Programming
    Replies: 1
    Last Post: 08-13-2009, 02:51 PM
  2. printing wide characters to strings...
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-20-2009, 11:56 PM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 04:10 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  5. Another problem swith my wide system MouseProc
    By darcome in forum Windows Programming
    Replies: 0
    Last Post: 10-03-2002, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21