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


Comments

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

    Reply
  • 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=http://burberry-femme-pas-cher28.webnode.fr/]burberry-femme-pas-cher28.webnode.fr[/url] , Chemise Burberry Pas Cher [url=http://burberryhomme2.webnode.fr/]burberryhomme2.webnode.fr[/url] , Burberry Pas Cher

    Reply
  • 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=http://www.nikeyasuijp.com/]nike[/url]BqaAkbFpxGxu [url=http://www.nikeyasuijp.com/nike-air-force1エアフォース1-c-14.html]ナイキ フリ[/url]IcwYhxRmhXam [url=http://www.nikeyasuijp.com/nike-air-maxエアマックス-c-12.html]nike air max[/url]QfzTzqLmxIjc [url=http://www.nikeyasuijp.com/nike-air-jordanエア-ジョーダン-c-13.html]ナイキランニング[/url]DjqGgmJuuJle

    Reply
  • 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?

    Reply
  • 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

    Reply
  • 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.

    Reply
  • 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!

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • 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?

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds