CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2004
    Posts
    28

    Post Hook procedure (SetWindowHookEx)

    Hey

    I am trying to use SetWindowHookEx() API to inject my code into another process, but I am kinda lost with the hook procedure that should be placed in the .dll file

    is there an explaination about how to build a hook procedure?

    thanks in advance!

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: Hook procedure (SetWindowHookEx)

    There are several good examples on SetWindowsHookEx on both CodeGuru and CodeProject (and in MSDN as well)... search

    Can you be more specific about the hook procedure problem you're having?

    Also, read about "systemwide hooks" in MSDN: http://msdn.microsoft.com/library/de...dn_hooks32.asp

  3. #3
    Join Date
    Jul 2004
    Posts
    28

    Re: Hook procedure (SetWindowHookEx)

    this is my callback function

    Code:
    LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam,  LPARAM lParam)
    {
    	if (nCode<0)
    		return CallNextHookEx(0,nCode,wParam,lParam);
    	if (wParam == WM_RBUTTONDOWN)
    	{
    		MessageBox(GetForegroundWindow(), "Yes", "Works", MB_OK);
    	}	
    	return CallNextHookEx(0,nCode,wParam,lParam);
    }

    and in onbutton function I set:
    Code:
    	HOOKPROC wlm;
    	HHOOK check;
    	HINSTANCE WLMDLL;
    
    	WLMDLL = LoadLibrary((LPCTSTR) "MyHook.dll");
    	wlm = (HOOKPROC)GetProcAddress(WLMDLL, "MouseProc");
    
    	check = SetWindowsHookEx(WH_MOUSE,wlm,WLMDLL,0);
    as you can see I am trying to do a mouse hook, this try is to get any right click, but later when I get it to work, I want it to be for a defined application that when it gets right click the the popup pops up

    can you see what I am doing wrong in my code?

    thanks in advance!

  4. #4
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: Hook procedure (SetWindowHookEx)

    If you really want a global hook (specifying 0 as last arg to SetWindowHookEx), the hook proc must reside in a DLL.

    I'm not sure how you have setup everything... Have you exported the MouseProc function? (ie does GetProcAddress work?) Check every return values when installing the hook.

    I wrote a simple test in C with VC2003... see attachment.
    Attached Files Attached Files

  5. #5
    Join Date
    Jul 2004
    Posts
    28

    Re: Hook procedure (SetWindowHookEx)

    thanks alot, I am taking a look at your code..

  6. #6
    Join Date
    Jan 2009
    Posts
    1

    Re: Hook procedure (SetWindowHookEx)

    Hi All,
    If we want a global hook (specifying the thread id of the process whose keyboard and mouse inout we want to block as last arg to SetWindowHookEx), then where the hookproc must reside, will it be a part of dll or will it be a part of exe .

    please reply,

    Thanks

    Shailesh

  7. #7
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: Hook procedure (SetWindowHookEx)

    It depends on the hook type. Some hooks can actually reside in an EXE, but most of them require a DLL.

    You can also have a thread specific hook in your own app, ie the thread id must belong to the process that called SetWindowHookEx.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Hook procedure (SetWindowHookEx)

    By the way, never could understand those plays with LoadLibrary and GetProcAddress, though this point is most common in the samples I saw, here and there. Personally me typically place the SetWindowsHookEx call into dll and link to the latter by means of import library with no problem.
    Best regards,
    Igor

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