Using a Service Under Windows

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


  • Summary document shows you the proven info regarding gucci and ways in which it might have an affect on customers.

    Posted by emeseesip on 05/07/2013 02:03am

    Basically The Most Thorough adidas Manual You Ever Read Otherwise Your Money Back [url=]グッチ バッグ[/url] Geez, wonderful item. Your organization have to see adidas right away whilst it's still in stock ! [url=]gucci キーケース[/url] nike assists everybody by adding many exceptional features and options. Its a unvaluable item for any fan of nike. [url=]グッチ ショルダーバッグ[/url] Third party statement unwraps Seven great new things surrounding nike that not a soul is discussing. [url=]シャネル バッグ[/url] This is why no one is bringing up adidas and consequently things one ought to begin doing today. [url=]chanel バッグ[/url] Different questions about adidas answered and consequently why you should definitely browse through each and every term of this specific report. [url=]財布 chanel[/url] The essentials behind gucci which you may gain from getting started today.[url=]ナイキスニーカー[/url] The best way to fully understand all that there is to find related to adidas in 5 basic steps.

  • burberry femme pas cher

    Posted by Acequeacila on 04/30/2013 05:58am

    Burberry Pas Cher burberry homme pas cher sac burberry pas cher sacoche burberry pas cher [url=][/url] , Chemise Burberry Pas Cher [url=][/url] , Burberry Pas Cher

  • Out of the ordinary article offers the run information for nike which experts claim a few buyers are aware of.

    Posted by icoppyapedcap on 04/25/2013 01:07pm

    TarOsbVrxCvh [url=]nike[/url]BqaAkbFpxGxu [url=エアフォース1-c-14.html]ナイキ フリ[/url]IcwYhxRmhXam [url=エアマックス-c-12.html]nike air max[/url]QfzTzqLmxIjc [url=エア-ジョーダン-c-13.html]ナイキランニング[/url]DjqGgmJuuJle

  • Download problems

    Posted by noamshan on 12/15/2005 03: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 06:05am

    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 12: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 12: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 12: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 12: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 12:00am

    Originally posted by: peter desoza

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

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

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

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