Using a Service Under Windows

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >


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

  • 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

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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