home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 11
Putting Windows Hooks to Work

This chapter is all about Windows hooks. A hook runs behind the scenes and is tied to particular Windows events and responds when one of those events occurs. For example, we might want to pop up the Windows notepad when the user presses ^N, and we can do that with Windows hooks (in this way, some hooks operate much like DOS terminate and stay resident, TSR programs).

There are many different types of Windows hooks: SendMessage() hooks, which watch the messages being sent to various windows; debug hooks for debugging purposes; training hooks for computer-based training applications; GetMessage() hooks; foreground idle hooks; journal hooks, which record messages and can play them back; keyboard hooks; message filter hooks; system message filter hooks; shell hooks; and mouse hooks.

In this chapter, we examine the two most popular types of hooks: journal hooks and keyboard hooks. You can use journal hooks to capture events in a system-wide manner, and unlike other system-wide hooks, you don’t have to enclose a journal hook’s code in a special DLL. Keyboard hooks are self-explanatory: You use them to capture keyboard events. We learn to put keyboard hooks to work by adding hotkeys—special keys you can press at any time to launch specific Windows applications—to Windows.

We begin our exploration of hooks with a program named Journal that records keystrokes in one program and plays them back in another. Such programs have many uses. For example, you can create batch files (such as the DOS .bat files) this way, using those files to control other applications.

The Journal Program

We use the Journal program to introduce journal hooks. Here, we allow the user to record keystrokes and then play them back. After selecting the Record item in the program’s File menu, the user can type as he or she likes.

           ----------------------------------------------
          |                                              |
          |----------------------------------------------                 |                                            |
          |  ---------------------------               |
          | |Here’s the text...        |               |
          | |                          |               |
          | |                          |               |
          | |                          |               |
          | |                          |               |
          | |                          |               |
          | |                          |               |
          |  ---------------------------               |
          |                                            |
           ----------------------------------------------

When the user selects the Play item in the File menu, we play the recorded keystrokes back in an entirely different program—Windows notepad.

           ----------------------------------------------
          |                                             |
          |----------------------------------------------                 |  ---------------------------                |
          | |Here’s the text...       |            |
          | |                    -------------------------------------------
          | |                              |Untitled - NotePad
                                |
          | |                     |---------------------------------------------- |
          | |                   |Here’s the text...                     |
          | |           |                                                 |
          | |           |                                                 |
          |  --------------     |                                         |               |
          |               |                                               |
           ----------------     |                                       |
                        |                                       |
                        |                                       |
                        |                                       |
                        |                                       |
                           ----------------------------------------------

We begin putting the Journal program together now. This is a fairly easy hook program, because we won’t have to place our code in a DLL, which you often have to do with hooks. Create the Journal project now and base the view class on CEditView to display the keystrokes the user types as we record them.

Add two new items—Record and Play—to the program’s File menu, allowing the user to record and play back keystrokes.

Setting the Record Hook

We make the Record menu item active first. When the user clicks this item, we start recording keystrokes. We should keep track of the number of events we’ve recorded in order to know how many events to play back. To do this, set up a new integer, RecordedEvents, and initialize it to 0.

void CJournalView::OnFileRecord()
{
    RecordedEvents = 0;                 ⇐
        .
        .
        .

Next, we set the Windows hook, using the SetWindowsEx() function, and save the hook handle we get in a new variable named Hook.

// JournalView.cpp : implementation of the CJournalView class
//

#include ”stdafx.h”
#include ”Journal.h”

#include ”JournalDoc.h”
#include ”JournalView.h”

#ifdef _DEBUG
#define new DEBUG_NEW#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

HHOOK Hook;             ⇐
    .
    .
    .

We indicate that we want a recording journal hook by passing the constant WH_JOURNALRECORD to SetWindowsEx(). We follow this by a pointer to the hook procedure—which we call RecordProcedure()—that holds the actual code used to record keystrokes, the handle of the current code module, and the thread ID of the thread we want to hook. We set that parameter to 0 to hook all existing threads.

void CJournalView::OnFileRecord()
{
    RecordedEvents = 0;
    Hook = SetWindowsHookEx(WH_JOURNALRECORD, RecordProcedure,
GetModuleHandle(NULL), 0);              ⇐
}

Now we’ve hooked all events to our RecordProcedure(), which we write in a moment. First, we need to handle the Play menu item.

Setting the Playback Hook

When the user selects the Play item in the File menu, he or she wants to play back the recorded keystrokes, and we do that with a playback journal hook. We need a program to play the keystrokes back into, so we use the Windows Notepad program. When the user selects the Play menu item, the recorded keystrokes are played back into the Notepad.

We start by giving the Notepad program the focus: When we play back the keystroke events we’ve read as new keystrokes, they are entered in Notepad. If the Notepad is open, we can get a pointer to it of type CWnd with the FindWindow() function, which searches for windows by the text in the title bar.

void CJournalView::OnFilePlay()
{
    CWnd* WordPad = FindWindow(NULL, ”Untitled - NotePad”);       ⇐


Interacting with Other Windows

Note how useful a function FindWindow() is: It lets you get a CWnd pointer to another window, and you can execute all kinds of actions with that other window using that pointer.


        .
        .
        .


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.