Using a Service Under Windows | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 24, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.