Creating a Win32 Service
You can create a Win32 service that periodically scans the desktop for displayed windows. This method provides greater flexibility than the Enable Default Reply method because you can search for any window attribute, such as whether the window is visible or whether it is a modal dialog window. By using Win32 APIs, you can systematically step through windows to obtain information.
A sample service with MessageBoxInstrument.c and MessageBoxInstrument.h files exists in the Samples\MBI folder of your installation media. This service uses several Win32 functions, including EnumDesktopWindows, EnumChildWindows, GetWindowText, and SendMessage. For a complete list of functions for creating a Win32 service, see this Microsoft Web site, and then search for the following topics:
- Win32 Service
- Windows Stations and Desktop
- Windows Overview and Functions
For the following example, the Mbi.exe sample Win32 service is used. The service schedules itself to periodically read every window on the desktop, looking for a specific window. It searches for the Message Service window produced by a NET SEND command that contains the text This is a test of the MBI service.
The Mbi.exe service schedules itself by using a waitable timer that is set to signal once per second. You determine how the window enumeration is scheduled or started.
EnumDesktopWindows, EnumWindows, and EnumChildWindows functions provide the handle to the window that is being inspected. Thus, when the window is found, the application can perform a specific action, such as paging a technician or rebooting the system. In the case of Mbi.exe, the WM_CLOSE message is sent to close the window.
To build the sample service using Microsoft Visual C++
- From the File menu, choose New.
- On the Projects tab, choose Win32 Console Application.
- Type a project name, and then choose OK.
- In the Win32 Console Application Wizard, select An empty project, and then choose Finish.
- In the New Project Information dialog box, choose OK.
- From the Project menu, choose Add to Project, and then choose Files.
- In the Insert Files into Project dialog box, browse for the Samples folder on your installation media, choose MBI, and then choose MessageBoxInstrument.c.
- Choose OK.
- Repeat steps 6 through 8 to add MessageBoxInstrument.h to the project.
- No additional libraries or settings are required.
- To change the build type to Release, from the Build menu, choose Set Active Project Configuration, and then choose the Win32 Release configuration of your project.
- Choose OK.
- Build the project.
Once the application is built, you can either write your own install application for the sample service, or use the Instsrv.exe application distributed with the Microsoft Windows 2000 Resource Kit.
To install the sample service using InstSrv
Before you install the sample service using InstSrv.exe, be sure that the Microsoft Windows 2000 Resource Kit is installed on your computer.
- Copy the sample service executable file to the %SystemRoot%\System32 path.
- Type the following command from a command prompt.
C:\Windows\System32>INSTSRV.EXE <sample service name>%SystemRoot%\SYSTEM32\<sample service name>.exe
The service appears in the Computer Management application.
To run the sample service using the Computer Management application
- Choose Browse, navigate to C:\Documents and Settings\All Users\Start Menu\Programs\Administrative Tools, and then open Computer Management.
- In the Computer Management window, expand the Services and Applications node, and then choose Services.
- Open the sample service.
- On the Log On tab, select Allow service to interact with desktop.
If you do not complete this step, the sample Service.exe process leaks memory, so that the process cannot be forcefully terminated. If this occurs, you must reboot your device.
- On the General tab, choose Start.
For demonstration purposes, the sample service displays a console on the desktop. The service then runs through every window on the desktop once per second.
- To start the sample service search, open another command prompt and type the following command.
C:\Windows\System32> NET SEND <COMPUTERNAME> "This is a test of the MBI service"
A window momentarily displays the test message, and then automatically closes.
- To stop the sample service, in the <sample service> Properties dialog box previously opened, select the General tab, and then choose Stop.
To remove the sample service
- Type the following command at a command prompt.
C:\Windows\System32> INSTSRV <sample service> REMOVE
- Reboot the system.
© 2006 Microsoft Corporation. All rights reserved.