Creating a Win32 Service (Windows Embedded Standard 2009)


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.

  1. From the File menu, choose New.

  2. On the Projects tab, choose Win32 Console Application.

  3. Type a project name, and then choose OK.

  4. In the Win32 Console Application Wizard, select An empty project, and then choose Finish.

  5. In the New Project Information dialog box, choose OK.

  6. From the Project menu, choose Add to Project, and then choose Files.

  7. In the Insert Files into Project dialog box, browse for the Samples folder on your installation media, choose MBI, and then choose MessageBoxInstrument.c.

  8. Choose OK.

  9. Repeat steps 6 through 8 to add MessageBoxInstrument.h to the project.

  10. No additional libraries or settings are required.

  11. 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.

  12. Choose OK.

  13. 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.

  • Before you install the sample service using InstSrv.exe, be sure that the Microsoft Windows 2000 Resource Kit is installed on your computer.

    1. Copy the sample service executable file to the %SystemRoot%\System32 path.
    2. 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.

  1. Choose Browse, navigate to C:\Documents and Settings\All Users\Start Menu\Programs\Administrative Tools, and then open Computer Management.

  2. In the Computer Management window, expand the Services and Applications node, and then choose Services.

  3. Open the sample service.

  4. 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.

  5. 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.

  6. 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.

  7. To stop the sample service, in the <sample service> Properties dialog box previously opened, select the General tab, and then choose Stop.

  1. Type the following command at a command prompt.

    C:\Windows\System32>  INSTSRV <sample service> REMOVE
  2. Reboot the system.

Community Additions