Using a Service Under Windows


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

This article was contributed by Elmue.

Environment: Windows NT/2000/XP


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()

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.


Download demo project - 10 Kb
Download source - 15 Kb


  • Download problems

    Posted by noamshan on 12/15/2005 11:49am

    I can't download the zip files mentioned here. If I click on the link, I get to the site map page. Can anybody help or send them over?

  • How to copy files to Network path from Service..?

    Posted by rm_pkt on 04/09/2004 01:05pm

    Hi there! My Service application need to copy some files to a network path. How can I achive this.? I'm using VC++ 6 in WinodowsXP. Any idea would be appriciated. - muthu

  • This is great!

    Posted by Legacy on 12/03/2003 08:00am

    Originally posted by: Mel

    Just a couple minor changes and it's exactly what I want. I just added two edit boxes so that I can enter the name of the service and its display name.

    Thanks for this program.

  • Give the man a break!

    Posted by Legacy on 10/23/2003 07:00am

    Originally posted by: Asconaga

    I am sure when Linus Torvalds published Linux someone said "Why do you need to do this we already have Unix". I think the person has missed the point. You are trying to contribute and in my view with an excellent article. Thanks for your work, I and many others appreciate your time!

  • How to create a service run under certain account instead of LocalSystem?

    Posted by Legacy on 09/09/2003 07:00am

    Originally posted by: hardywang

    I tried pass user name and password to CreateService function, it doesn't work.

  • Great Artice - excellent

    Posted by Legacy on 08/27/2003 07:00am

    Originally posted by: Leddy

    Thanks for the great artice, whilst I acknowledge there are others doing similar things, its great having the whole thing in a neat win32 mfc app.

    well done.

    A controlling icon would be nice, but I am looking into that.

  • What's new

    Posted by Legacy on 07/24/2003 07:00am

    Originally posted by: peter desoza

    There are already similar code available on codeguru, what's new you are presenting here?

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date