Window Hook

This is a discussion on Window Hook within the Windows Programming forums, part of the Platform Specific Boards category; With SetWindowsHookEx() you can either specify a dll in hMod and use 0 for dwThreadId which gives you a system ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    11

    Window Hook

    With SetWindowsHookEx() you can either specify a dll in hMod and use 0 for dwThreadId which gives you a system wide hook, or you use NULL for hMod and specify your programs thread id in dwThreadId to hook your program.
    Now if i happen to have a thread id for another program can i create a hook which will only hook that program. I have tried a couple versions of SetWindowsHookEx() with and without a dll but i cant get it to work yet, has anyone done this before?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The documentation suggests that you can. I haven't tried it.
    Quote Originally Posted by MSDN_SetWindowsHookEx
    lpfn
    [in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
    Which hook are you trying to use? Could you post your hook setup code?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    has anyone done this before?
    Yes, several times. Just pass SetWindowHookEx() the ID of the thread you want to hook.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    11
    Hi again been busy for a bit. Here is the code i've been playing with, just messing about with notepad.
    Am I right in thinking that with CreateProcess the dwThreadId member of PROCESS_INFORMATION contains the thread Id I would need to hook notepad in this program?
    If not how do you get a thread Id for another application?

    Error message i get is 1428 :

    OLE IDispatch exception code "number" from "name": "name" (Error 1428)
    You have received an error code returned by an Automation object. It is specific to the OLE object.In this case, the object returns a textual description of the error, as well as the source (usually OLE object name) of the error.
    Attached Files Attached Files
    Last edited by paul_uk; 11-28-2004 at 06:43 AM.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I'm not sure where you are getting your error description from but my list of system error codes says:
    Quote Originally Posted by MSDN System Error Codes
    1428 Cannot set nonlocal hook without a module handle. ERROR_HOOK_NEEDS_HMOD
    This makes it clear that the hook procedure must be in a DLL and you must provide its hInstance to SetWindowsHookEx().

    Also, your GetMsgProc needs the CALLBACK calling convention:
    Code:
    LRESULT CALLBACK GetMsg( int code, WPARAM wParam, LPARAM lParam )

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    11
    The Error description came from msdn, i just searched for 'error 1428', i guess maybe different things use the same codes, will try it in a dll. I was hoping to do away with the hassle of a dll, it may as well be systemwide if i got to use a dll.
    Thanks for the help dude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM

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