Thread: handle messages even when the window isn't active

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by new_in_c++ View Post
    Yes, i have already read it. I'm asking how it managed to read keys even though the window was invisible. It hasn't got any of those hooks or low level keyboard procedures.
    It was also a console program that triggered virus scanners...

    It was using GetAsyncKeyState() in a loop... probably pounding the system in the process.

    Maybe if you tell me exactly what you're trying to accomplish ...
    Last edited by CommonTater; 05-01-2011 at 08:53 AM.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    53
    Quote Originally Posted by CommonTater View Post
    Maybe if you tell me exactly what you're trying to accomplish ...
    I'm trying to make a virtual mouse which is controlled by the keyboard. It will be able to emulate mouse clicks and movements(i hope).

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Interresting project... MouseKeys have been a standard windows feature (since Win98) under "accessibility", so we know it can be done.

    You will need to make the DLL as I showed you for that. You will need to handle keybd_event notices at that level, but they're pretty easy.

    You'll also need a small "mother" program to keep the DLL loaded. Usually these small programs show an icon in the system tray so you can get to any settings... such as which keys do what or how fast the mouse moves etc. So this will need to turn itself into a Windows API program.

    The good news is that once you get the hang of setting the hook it's pretty easy going from there...

  4. #19
    Registered User
    Join Date
    Sep 2009
    Location
    Lagos, Nigeria
    Posts
    3

    How to write code to Load DLL into main program

    Quote Originally Posted by CommonTater View Post
    As an example of form.... there's the DLL from that mouse hook project...
    Code:
    #define WIN32_LEAN_AND_MEAN
    #define _WIN32_WINNT 0x400
    #pragma comment(linker, "/heap:128")
    #define MMAPI  __declspec(dllexport)
    
    #include <windows.h>
    
    /////////////////////////////////////////////////////////////////
    //  Handle double click
    //  
    
    BOOL RevButtons = 0;
              
    void SendClick(void)
      { if (!RevButtons)
          { mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
            mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
            mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
            mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); } 
        else
          { mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
            mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
            mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
            mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0); } }
    
    /////////////////////////////////////////////////////////////////
    // Mouse mod process
    //
    int lastdir = 1;          // wheel tracker
    BOOL mbutton = 0;         // settings
    BOOL hysterisis = 0; 
    HHOOK mmhook = NULL;      // mouse hook handle
    PMSLLHOOKSTRUCT mhs;
    
    MMAPI LRESULT CALLBACK MouseTracker(int code, WPARAM wparm, LPARAM lparm)
      { if (code < 0)
          return CallNextHookEx(mmhook,code,wparm,lparm); 
        // get struct address
        mhs = (PMSLLHOOKSTRUCT)lparm; 
        // process the message
        switch (wparm)
          { case WM_MBUTTONDOWN :  // replace with lbutton dblclick
              { if (mbutton) 
                  { SendClick();
                    return 1; }
                break; }  
            case WM_MBUTTONUP :
              { if (mbutton)
                  { return 1; }
                break; }    
            case WM_MOUSEWHEEL :  // drop 1 msg on wheel reverses
              { if (hysterisis)
                  if (((lastdir < 0) && ((int)mhs->mouseData > 0)) ||
                      ((lastdir > 0)  && ((int)mhs->mouseData < 0)))
                    { lastdir = (int)mhs->mouseData;
                      return 1;   } } }
        return CallNextHookEx(mmhook,code,wparm,lparm); }
        
    /////////////////////////////////////////////////////////////////
    //  Enable features
    //
    MMAPI void WINAPI SetMButton(BOOL dblclk)
      { mbutton = (dblclk & 1); }
    
    MMAPI void WINAPI SetMWheel(BOOL rollback)
      { hysterisis = (rollback & 1); }
      
    /////////////////////////////////////////////////////////////////
    // Set the windows hook
    // if goforit = 1 hook is set, 0 to release
    // Returns hook status 1 = active, 0 = not
    //
    HINSTANCE hdll;   // dll handle
    
    MMAPI BOOL WINAPI SetMHook(BOOL goforit)
      { if (goforit)
          { if (mmhook)
              return 1; 
            mmhook      = SetWindowsHookEx(WH_MOUSE_LL,&MouseTracker,hdll,0); 
            RevButtons  = GetSystemMetrics(SM_SWAPBUTTON); }
        else
          { if (mmhook)
              { if (UnhookWindowsHookEx(mmhook))
                    mmhook = NULL; } }
        return (mmhook != NULL); }      
    
    /////////////////////////////////////////////////////////////////
    //  DLL entry point
    //
    BOOL APIENTRY DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
      { if (reason == DLL_PROCESS_ATTACH)
          hdll = hinst;
        return 1; }
    Load the DLL... call the SetMHook function and the DLL is then injected into each process. There just wasn't any other way to do it.
    I am a beginner in windows programming, I want to know the code that will load a DLL into a main program.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Wellll... you could try LoadLibrary()

    Since you're working at winapi level, you really should get a copy of the Windows SDK... It's a near total disclosure of Windows API functions and information.

    You might also want to pay a visit to the Forger's Win API tutorial for a good start in GUI mode programming.
    Last edited by CommonTater; 05-24-2011 at 08:16 AM.

  6. #21
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by CommonTater View Post
    Adeyblue... the guys who wrote the Operating System disagree with you... what can I say?
    Well technically it's the people who wrote the documentation who disagrees with him. And in this case the docs are wrong.
    The documentation for SetWindowsHookEx says
    "All global hook functions must be in libraries."
    but they should actually say something like
    "All global hook functions, except the low level hooks, must be in libraries."
    See How to set a Windows hook in Visual C&#35; .NET for reference
    Low-level hook procedures are called on the thread that installed the hook. Low-level hooks do not require that the hook procedure be implemented in a DLL.

  7. #22
    Registered User
    Join Date
    Sep 2009
    Location
    Lagos, Nigeria
    Posts
    3
    Thank you, Common Tater, for your respond to my request.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by _Mike View Post
    Well technically it's the people who wrote the documentation who disagrees with him. And in this case the docs are wrong.
    The documentation for SetWindowsHookEx says
    "All global hook functions must be in libraries."
    but they should actually say something like
    "All global hook functions, except the low level hooks, must be in libraries."
    See How to set a Windows hook in Visual C# .NET for reference
    Thing I don't get, for the life of me... what's so wrong with putting the SetWindowsHook call in a DLL ... you're going to have to have a DLL anyway so you can catch the key/mouse/window events by injecting it into each process... so what's the problem?

    Seems to me this is mostly argument for argument's sake...

  9. #24
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    you're going to have to have a DLL anyway so you can catch the key/mouse/window events by injecting it into each process
    The ignorance of this threads content is legendary

  10. #25
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Discussing keyloggers in this forum is against the forum guidelines. Before I close this thread my main question is in order to create a 'virtual mouse' why do you need a window that is minimized, hidden, or otherwise invisible to the user to receive keyboard input? Windows was designed so that one window would have the keyboard focus. You should have a very good reason for going against this design. Low level keyboard hooks are probably not the answer here but perhaps a re-design is. Just b/c the low level keyboard hook works does not mean you should be using that in this situation.

    Last week there was a topic on key loggers. How did it work?

    6. Messages relating to cracking, (erroneously called "hacking" by many), copyright violations, or other illegal activities will be deleted. Due to the overlapping boundaries of code with malicious intent, and other legitimate uses of it, the moderators will assess each potential infraction on a case by case basis.
    So far no illegal activity has been discussed here and since the guidelines do not clearly call out keyloggers I will use my best judgement here and leave the thread open.
    Last edited by VirtualAce; 05-29-2011 at 11:12 AM.

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by VirtualAce View Post
    Discussing keyloggers in this forum is against the forum guidelines. Before I close this thread my main question is in order to create a 'virtual mouse' why do you need a window that is minimized, hidden, or otherwise invisible to the user to receive keyboard input? Windows was designed so that one window would have the keyboard focus. You should have a very good reason for going against this design. Low level keyboard hooks are probably not the answer here but perhaps a re-design is. Just b/c the low level keyboard hook works does not mean you should be using that in this situation.

    So far no illegal activity has been discussed here and since the guidelines do not clearly call out keyloggers I will use my best judgement here and leave the thread open.
    Hey Ace... We aren't discussing keyloggers... the OP is trying to make a key-mouse tool similar to the Windows tool that lets you manipulate the mouse from your keyboard arrow keys. It's a perfectly legit project... and don't worry, I'm not about to help anyone write anything I consider suspicious. The DLL I posted demonstrates how to inject a DLL and catch mouse messages to activate the wheel button for double clicks. Again a perfectly legit use of code and time.

    The keylogger question came up because the OP is getting a dump truck full of conflicting information and is trying to weed his way through it. I don't envy the guy, it shure has been heeped on nice and deep. I'm just trying to help him understand the problem... I'm not sure what the others are hoping to accomplish...

    I apologize if this appears a borderline discussion... However you can rest assured I would not be in it if I thought we were creating a monster.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    The ignorance of this threads content is legendary
    Please go stroke your ... ummm... ego somewhere else.

  13. #28
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Hey Ace... We aren't discussing keyloggers...
    I ascertained that from reading through the thread a second time which is why I'm leaving it open.

  14. #29
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by CommonTater View Post
    Thing I don't get, for the life of me... what's so wrong with putting the SetWindowsHook call in a DLL ... you're going to have to have a DLL anyway so you can catch the key/mouse/window events by injecting it into each process... so what's the problem?

    Seems to me this is mostly argument for argument's sake...
    The reason why I would avoid dll injection unless it was absolutely required (and I don't think it is for this kind of application) is that bugs/mistakes in the dll risk taking down more than just your own application if it crashes.
    But properly coded there's nothing wrong with using dlls, and I'm sorry if I made it seem as such.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send Key To Active Window
    By thatsgreat2345 in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2006, 02:17 PM
  2. active window ...
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2006, 08:05 AM
  3. Re: Active Child Window
    By icc_81 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2004, 03:33 PM
  4. Setting active window
    By bennyandthejets in forum Windows Programming
    Replies: 1
    Last Post: 10-14-2003, 09:22 AM
  5. How to Handle the messages of Clild windows ??
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2003, 04:32 AM

Tags for this Thread