Lesson 6: Using Spy

In this lesson, you will learn how to use Spy++, another Visual C++ utility. This useful, popular utility can display information about all current processes, their threads, and all open windows—even hidden windows.

After this lesson, you will be able to:

  • Execute the Spy++ utility and use it to display a graphical tree of relationships among system objects, including processes, threads, and windows.
  • Use Spy++ to search for specified windows, threads, processes, or messages.
  • View the properties of selected windows, threads, and processes.
  • Use the Spy++ Finder tool to select a window for monitoring.
  • View a real-time record of messages that a window sends and receives.
Estimated lesson time: 15 minutes

Spy++ Views

Start Spy++ the same way as you would the Dependency Walker, by selecting it from the Microsoft Visual Studio 6.0 Tools menu. The utility displays four main views:

  • Windows view shows a list of open windows.
  • Processes view lists current processes.
  • Threads view lists all current threads.
  • Message log lists the messages that a window receives.

These four views are activated by commands in the Spy menu, or by the first four buttons on the toolbar. Each time a command is invoked, it creates a view as a new child window, so the tool buttons do not toggle between existing views. Instead, you should create a view only once, and thereafter switch between views by making a selection in the Window menu. Like the Dependency Walker, Spy++ is an MDI program, so you can tile views within the client area.

Figure 13.10 shows how the Windows and Processes views might look when arranged in the Spy++ window. The views show that Visual C++ is currently running (named MSDEV in the Processes view) and has created several child windows.

click to view at full size.

Figure 13.10 Windows and Processes views in the Spy++ window

A Spy++ view represents a snapshot, so applications that begin running after Spy++ are not automatically added to the lists. Similarly, applications that terminate while Spy++ is executing are not automatically removed from the list. To update a view, give the view focus and click the Refresh command on the Window menu.

Windows View

The Windows view displays a list of all windows currently open. It does not matter if the window is hidden or not; many open windows in a typical Windows session are invisible, serving their various applications only as message receivers. When Spy++ begins or when the view is refreshed, the utility compiles a list of all windows and displays the list in the Windows view.

The list is a normal tree view with small plus and minus icons that collapse or expand the list into levels. The tree's hierarchy indicates window parentage—that is, the relation between windows in which one window can create others as child windows. Click any plus icon adjacent to a window to expand the list to include that window's children. If the plus icon does not appear next to a window, it means that the window does not have any children.

A typical list in the Windows view can include many windows, so it's sometimes difficult to locate the entry for a particular window. In these cases, use the Window Finder tool.

  • To use the Window Finder tool
    1. Arrange the screen so that Spy++ and the window you want to investigate are both visible on the screen.
    2. On the Spy++ toolbar, click Find Window, or choose Find Window from the Search menu.
    3. Drag the Finder Tool icon (shown in Figure 13.11) out of the Find Window dialog box. As the dragged icon passes over an exposed window, the window appears framed in thick black lines, and the Find Window dialog box notes the window's handle. Drop the icon over the window you want, and close the Find Window dialog box. You can then locate the window's entry in the list by scanning for its window handle.
    4. Figure 13.11 The Finder Tool icon

    Processes View and Threads View

    Each process running in the Windows multitasking environment creates one or more threads; each thread can create any number of windows. Use the Processes view to examine a particular process, which usually corresponds to an executing program. Processes are identified by module names, or are designated system processes.

    The Threads view lists all currently executing threads along with the names of their owner processes. Expand a thread entry to see a list of its associated windows.

    Message Log

    For debugging purposes, the message log is perhaps the most useful of Spy++'s capabilities. It keeps a record in real-time of all messages that a window sends or receives. This allows you to examine a list of messages to see in what order they arrive, which is particularly useful for capturing window initialization messages.

    A convenient way to begin message logging for a window is by using the Window Finder tool described earlier.

  • To log messages using Window Finder
    1. Drag and drop the Finder Tool icon over a target window as you did previously. In this instance, click the Messages radio button at the bottom of the Find Window dialog box before closing the dialog box.
    2. As you work in the target window, Spy++ compiles a list of messages the window sends and receives, as shown in Figure 13.12.

      click to view at full size.

      Figure 13.12 Spy++ message log

      The Spy++ message log is divided into four columns, which are described in Table 13.6.

      Table 13.6 The Four Columns of the Spy++ Message Log

      Column Description
      1 Index number showing the message count
      2 Window handle
      3 Message code: either S (sent), R (received), P (posted), or s (message was sent, but security prevents access to the return value)
      4 Message, parameters, and return values

      By default, the list includes all messages, including mouse movement and keystroke messages such as WM_KEYDOWN and WM_KEYUP. This means the list can get crowded in a hurry, but Spy++ can filter the log to only those messages that interest you most.

    3. On the Messages menu, click Options, then click the Messages tab.
    4. Click the Clear All button to clear all the check boxes, then select only those check boxes for the messages you want to investigate.
    5. End message tracking either by clicking the button with the stoplight icon on the Spy++ toolbar, or by clicking Stop Logging on the Messages menu.

    Viewing Window Messages in Spy++

    In this practice exercise, you will launch the Spy++ utility and use it to spy on another application. By turning on the utility's message logging feature, you can view in real time the messages belonging to a selected window.

  • To start Spy++
    1. On the Programs menu, click Start.
    2. Click Microsoft Visual Studio 6.0, click Microsoft Visual Studio 6.0 Tools, and then click Spy++.
    3. When the utility appears, click the Find Window tool on the toolbar and drag the Finder Tool icon and drop it onto another window. If no other application is running, drop the Finder Tool icon onto the desktop.
    4. Click the Messages radio button at the bottom of the Find Window dialog box, and then close the dialog box.
    5. Experiment by moving the mouse pointer over the selected window. You will see the many messages that the window sends and receives pertaining to mouse movement, such as WM_MOUSEMOVE and WM_SETCURSOR.

    Lesson Summary

    This lesson described Spy++, a Visual C++ utility that reports information about executing programs. Spy++ can provide invaluable insight into an application's behavior that is not available through the Visual C++ debugger.

    Spy++ displays four main views that show a list of windows, processes, threads, and messages pertaining to a particular window. The Window Finder tool aids in associating any exposed window with the list of windows that Spy++ displays. The message tracking feature intercepts and logs all messages that enter and leave a window, showing chronological order and the values of message parameters. The message log can be filtered to monitor messages of particular interest only.



    Microsoft Press - Desktop Applications with Microsoft Visual C++ 6. 0. MCSD Training Kit
    Desktop Applications with Microsoft Visual C++ 6.0 MCSD Training Kit
    ISBN: 0735607958
    EAN: 2147483647
    Year: 1999
    Pages: 95

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net