Thread: Hook GetForegroundWindow function

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Hook GetForegroundWindow function

    Hello, I would like some help with hooking the GetForegroundWindow() function. So that the OS will notify the program when there is a change in windows, without having to poll 60 times a minute and hog the cpu. As far as I know, this is the most efficient way of being notified. I have never done hooks before, so assistance with this is greatly appreciated.

    Thanks for the help in advance.
    Last edited by binks; 04-08-2012 at 03:52 PM.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    If you've never investigated hooks before, how do you know they're the most efficient option? Setting a hook in a function in every process might not be calling 60 times a second, it might be 0, 50, or 200 on top of the extra memory and performance impact the hook has.

    Anyway, why do you want to do this? Your window will get notifications when the user starts and stops interacting with it, that is usually enough for all but the most insidious programs.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I'm making a keylogger that notifies when the user changes windows. I'd make a game but it wouldn't look pretty...

    So if this isn't the most efficient way, then what way is?

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    The keylogger isn't meant for anything malicious, just to make for fun.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm pretty sure you'll want something more constructive on your CV if you want to get a job later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Quote Originally Posted by Salem View Post
    I'm pretty sure you'll want something more constructive on your CV if you want to get a job later on.
    What do you mean more constructive Salem? And if I want a job later on?

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    After doing more research, I have discovered I need the SetWindowsHookEx function to set the hook. So would I set int idHook as WH_SHELL to get info about the window?

    Again, my purpose is to receive the title of the current foreground window when the user changes windows. Of course this is trying to hook it. Are there better ways of receiving this information (as Salem may have suggested)?

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Dude, I think you better quit while you aren't banned.

    Consider this just a bit of friendly advice; the last person who open claimed "I'm making a keylogger" was quickly removed.

    Your claim of intentions aren't all that relevant. We don't know that you don't have malicious intentions.

    Soma

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok thanks for the warning. I quit this and there is no need for any reply. I understand where your coming from completely, so that's it then.

    @ MODS

    Lock this thread, or delete it if you can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hook a static function?
    By r2d2 in forum C Programming
    Replies: 0
    Last Post: 02-19-2010, 11:10 PM
  2. Drag Hook?
    By audinue in forum Windows Programming
    Replies: 0
    Last Post: 03-01-2009, 05:25 AM
  3. What is a hook function?
    By hzmonte in forum C Programming
    Replies: 2
    Last Post: 08-30-2007, 10:45 AM
  4. IE hook
    By maxorator in forum Windows Programming
    Replies: 5
    Last Post: 08-23-2006, 04:49 AM
  5. directx hook
    By X PaYnE X in forum Game Programming
    Replies: 0
    Last Post: 11-26-2005, 03:42 PM

Tags for this Thread