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

Thread: WIn 32 Hooks

  1. #1
    Join Date
    Mar 2003
    Location
    London
    Posts
    198

    WIn 32 Hooks

    Hi:
    I want my Application to notifiled each time a new window is activated..or deactivated (i am refering to any window in OS ),Perhaps using hooks is the best method ,but can someone please write a comprhensive code for it
    another query is :
    how can i find the absolute file path of the active application (egc:\windows\notepad.exe),
    thanks
    Ahmed

  2. #2
    Join Date
    Apr 2003
    Location
    Sydney, Australia
    Posts
    151
    Writing code that Hooks processes requires a lot of time, effort and money. I don't think people will be that nie as to do your project for you- after all, we don't get paid.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: WIn 32 Hooks

    Originally posted by xs2ahmed
    Hi:
    I want my Application to notifiled each time a new window is activated..or deactivated (i am refering to any window in OS ),Perhaps using hooks is the best method ,but can someone please write a comprhensive code for it
    As already mentioned there is basically a zero percent chance of getting any valuable answer for this. Everyone contributing here is doing it in his/her spare time...being volunteers. The forum is intended to help people with specific problems not to write the complete software.

    Originally posted by xs2ahmed
    another query is :
    how can i find the absolute file path of the active application (egc:\windows\notepad.exe),
    thanks
    Ahmed
    Take a look at this FAQ...

  4. #4
    Join Date
    Mar 2003
    Location
    London
    Posts
    198
    Hi:
    i think some people are getting angry with the text
    "comprehensive code"
    i did'nt mean to ask you to write full code .....
    I think if any one knew something about it ,he would have given me some tips.................................

    For Andreas Masur ... i didnt ask to write full software , and i dont think that this problem needs more than 10 lines of code.........

    The first part of my question is already solved ....
    if any one know something about finding the path of the exe associated with a window ....please let me know...........
    thanks
    Ahmed
    Last edited by xs2ahmed; April 21st, 2003 at 03:52 AM.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by xs2ahmed
    if any one know something about finding the path of the exe associated with a window ....please let me know...........
    thanks
    Ahmed
    Did you take a look at the FAQ I posted in my previous post?

  6. #6
    Join Date
    Mar 2003
    Location
    London
    Posts
    198
    Hi:
    I think the problem is soved ..


    this was what i was asking for


    HWND GetForegroundWindow(VOID);
    //this returns the handle to active application


    //and we can use this handle inthe following func to get the full path of the exe


    UINT GetWindowModuleFileName(
    HWND hwnd, // handle to window
    LPTSTR lpszFileName, // file name buffer
    UINT cchFileNameMax // max size of file name buffer
    );


    thanks to all for helping me
    Ahmed

  7. #7

    Hooks are not that hard.....

    Here is the code of a hook function that would monitor when windows are created.... g_hGlobalHook is the a global, the handle of the hook which would be set via another function after loading the *.dll - that handle is retrieved via SetWindowsHookEx() which is called by the app that loads the *.dll to inject the hook.

    Code:
    int __stdcall MyWindowHook(int nCode,WPARAM wParam,LPARAM lParam)
    {
        if (nCode < 0)
        {
            return ::CallNextHookEx(g_hGlobalHook,nCode,wParam,lParam);
        }
        else
        {
            CWPSTRUCT * pStruct = reinterpret_cast<CWPSTRUCT *>(lParam);
            if (pStruct->message == WM_CREATE)
            {
                // Do something now that a window is being created.....
                // Just don't create another window, that includes a MessageBox() call.
            }
            return ::CallNextHookEx(::g_hGlobalHook,nCode,wParam,lParam);
        }
    }
    In fact, hooks are one of the easiest parts of the WINAPI to understand. How easy, never used one until 10 minutes ago.
    Last edited by JamesSchumacher; April 22nd, 2003 at 10:45 PM.

  8. #8
    Join Date
    Apr 2003
    Posts
    39
    the code james has put in here is just the hook handler...
    i guess the book from which he learnt about win32 hooks
    in 10 min, missed out the hook placing part..

    Hook are indeed not cryptic.... save yourself from
    the DLL maze....

    start with plain code to learn hooks... you need DLL only
    when you place an application specific hooks... and
    your hook handler needs to be in those DLL...

    have a look as this code
    ************************************************
    if(theApp.m_hGlobalHook == NULL)
    {
    theApp.m_hGlobalHook = SetWindowsHookEx(my_WH_KEYBOARD_LL,(HOOKPROC)LLevelKeyBoardHandler,AfxGetApp()->m_hInstance,0);
    if(theApp.m_hGlobalHook == NULL)
    {
    AfxMessageBox("Hook Failed");
    }
    }

    where the handler is

    LRESULT CALLBACK LLevelKeyBoardHandler(INT f_nKeyCode,WPARAM wParam,LPARAM lParam)
    {

    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;

    if((f_nKeyCode == HC_ACTION) && ((wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN)))
    {
    if((pkbhs->vkCode == VK_F12) && ((wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN)))

    // global hook for F12 key.....
    }

    }
    ******************************************

    Just have a simple win32 console binary....
    and have fun with hooks...

    about the stuff which you want to do,
    is just matter of changing the hook type...
    (keyboard,mouse, wm_messages, wm_sysmessages)

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