Thread: Problems with global hook! HELP!

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    35

    Problems with global hook! HELP!

    Hi 2 all!
    I have a problem with hooks! I wrote a function in my dll, wich i call from the main program. Here is the text of my dll and the main code:

    dll:
    Code:
    #include <windows.h>
    
    
    __declspec(dllexport)LRESULT CALLBACK HookProcedure(int code, WPARAM wParam, LPARAM lParam)
    {	
    	HHOOK hHook;
    	if(code<0) return CallNextHookEx(hHook, code, wParam, lParam );
    	MessageBox(NULL, "HOOK!", "hook", 0);
    	return CallNextHookEx(hHook, code, wParam, lParam );
    }
    main prog:
    Code:
    #include <windows.h>
    
    #pragma comment(lib, "myhookdll")
    __declspec(dllimport)LRESULT CALLBACK HookProcedure(int ,WPARAM, LPARAM); 
    
    ......
    
    		case WM_CREATE:
    
    			hHook = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)HookProcedure, GetModuleHandle(NULL), 0 );
    			if( hHook == NULL )
    			{
    				wsprintf(c, "%d", GetLastError());
    				MessageBox(hwnd, c, "Can't set hooK", 0);
    			}
    When my program starts, it receives mesages from keyboard and show message box. But after i minimize my program the hook doesn't work. It seems like i set an a local hook, without using dll.
    Help, plz. What is the problem?!
    im from LMoldovaZ

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You need to call SetWindowsHookEx/UnhoookWindowsHookEx inside the DLL
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    it stil doesn't work. i add to my dll two functions which set and unset the hooks. i call it from the main program (this functions are imported from dll). the problem doesn't disappear!
    im from LMoldovaZ

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    GetModuleHandle(NULL) always returns the module handle to the exe, rather than a dll. You need to use the hInstance provided by DllMain (you can save it in a global variable, and use it in your set function) or the hInstance provided by LoadLibrary if you use it (which your current code does not).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:23 AM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 02:54 PM