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
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 dont have to enclose a journal hooks 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 hotkeysspecial keys you can press at any time to launch specific Windows applicationsto 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 programs File menu, the user can type as he or she likes.
----------------------------------------------
| |
|---------------------------------------------- | |
| --------------------------- |
| |Heres the text... | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| --------------------------- |
| |
----------------------------------------------
When the user selects the Play item in the File menu, we play the recorded keystrokes back in an entirely different programWindows notepad.
----------------------------------------------
| |
|---------------------------------------------- | --------------------------- |
| |Heres the text... | |
| | -------------------------------------------
| | |Untitled - NotePad
|
| | |---------------------------------------------- |
| | |Heres the text... |
| | | |
| | | |
| -------------- | | |
| | |
---------------- | |
| |
| |
| |
| |
----------------------------------------------
We begin putting the Journal program together now. This is a fairly easy hook program, because we wont 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 itemsRecord and Playto the programs 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 weve 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 procedurewhich 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 weve 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 weve 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.
.
.
.
|