Using a Service Under Windows


This article was contributed by Elmue.

Environment: Windows NT/2000/XP

Description

This demo shows how to create, start, stop, and delete a service under Windows NT/2000/XP.

This program demonstrates the use of these API commands:

  • OpenSCManager()
  • CloseServiceHandle()
  • CreateService()
  • OpenService()
  • SetServiceStatus()
  • DeleteService()
  • RegisterServiceCtrlHandler()
  • StartServiceCtrlDispatcher()
  • LPSERVICE_MAIN_FUNCTION
  • LPHANDLER_FUNCTION

Try this demo with an Administrator Account and a normal User or Guest Account! This program logs all commands it receives and executes into the logfile C:\DemoService.txt.

This program is used in two modes:

  • Service.exe (without parameters) runs the program with the GUI (see screenshot)
  • Service.exe /Service runs it as a Service (Windows calls Service.exe with the "/Service" parameter if the already installed Service is started)

In a Service, it is not possible to set a normal Timer (SetTimer()) because the service does not own a window and can't receive Window Messages.

For demonstration, I set a waitable timer that writes every five seconds to the log file, even when no user is logged on.

Downloads

Download demo project - 10 Kb
Download source - 15 Kb

IT Offers

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Go Deeper

  • The penetration of virtual servers is approaching 50 percent in IT infrastructures, yet administrators are only backing up, on average, 68 …
  • This buyers guide provides independent research and test results to help you determine your endpoint protection requirements and identify …
  • Regardless of the size of your business, a Denial of Service attack can disrupt your organization's website and network services. Download …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds