Click here to Skip to main content
11,408,708 members (60,699 online)
Click here to Skip to main content

A Windows Service Application

, 29 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple Windows service application to fork and monitor applications.


Recently, I came across a situation where I needed to make an application launch automatically when the PC starts up. And the problem was that the program that I needed to launch was designed as a GUI application. The best way I could think of to make it launch automatically was to create a service based application, so that once the user logs on to the computer, the application would be executed automatically by the service and monitor the application. Then, I thought of appending more than one application into a service and run it till the service is killed or stopped.


I searched a number of sites for some code that could help me create a simple Windows service application which can fork and monitor some applications, but I only found a lot of code on how to manage the current Windows services on the system. Later, I started to create my own with the help of MSDN.


This is a simple Windows based services application which is created using Win32 - VC++. The usage of this application is - it will act as a Windows service which is able to fork and monitor any number of sub processes which are running under this service. The applications can be of any type like GUI, non-GUI etc.

This application will help you know about the following topics related to a service application:

  1. How to create/install a new Windows Service.
  2. How to map some applications to a particular Windows Service, and control the application by the service. (How to connect to the Control Service Manager.)
  3. How to start/run Window services.
  4. How to stop/kill Window services.
  5. How to uninstall Windows services.

Code Usage

The code is very simple, and for the most part, self-explanatory. The application executable is given the name "WinService.exe". The user has to pass some command line arguments while executing this application, through the command prompt to make use of its different features.

""ApplicationName  FeatureExtension"

Note: The default service name would be "Sundar_Service". The user can change the service name in the code and rebuild the application.

Feature Extensions

ApplicationName -i  / -I - Create / Install New service
ApplicationName - Connect to Service Control Manager
ApplicationName -s / -S - Run / Start the service
ApplicationName -k / -K - Stop / End the service
ApplicationName -u / -U - Uninstall / Kill the service from windows

Create / Install New Service

"WinService -i" or "WinService -I"

Sample Image

When you type the above line on the command prompt and press Enter, you are able to view a new service in the Windows Services window with the name "Sundar_Service".

VOID Install(char* pPath, char* pName);

The Install method creates a service object and adds it to the specified service control manager database.

SC_HANDLE schService = CreateService(
            schSCManager,     /* SCManager database      */
            pName,            /* name of service         */
            pName,            /* service name to display */
            SERVICE_ALL_ACCESS,  /* desired access       */
              SERVICE_INTERACTIVE_PROCESS , /* service type  */
            SERVICE_AUTO_START,   /* start type              */
            SERVICE_ERROR_NORMAL, /* error control type      */
            pPath,                /* service's binary        */
            NULL,                 /* no load ordering group  */
            NULL,                 /* no tag identifier       */
            NULL,                 /* no dependencies         */
            NULL,                 /* LocalSystem account     */
            NULL        );        /* no password             */

Sample Image

Connect to SCM (Service Control Manager)

"WinService" or "WinService"

Sample Image

While executing the above command on the command prompt, the child processes which are mapped under the service "Sundar_Service" will get started. And these processes will be monitored by the service, so that the application can be started again if it gets killed for any reason.

VOID ExecuteSubProcess();

The "ExecuteSubProcess" method is defined with the "_beginthread" method for thread creation, which ensures the child process continues execution till the service is stopped by the user or Windows. This will also call the "StartServieCtrlDispatcher" method.


The "StartServiceCtrlDispatcher" connects the main thread of a service process to the service control manager, which causes the thread to be the service control dispatcher thread for the calling process.

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)

The callback functionality mapped through the lpServiceStartTable structure will register the "ServiceHandler" method to the service and update its status. Also, this will execute its child applications.

VOID WINAPI ServiceHandler(DWORD fdwControl)

The "ServieHandler" method will control service functions, like "Start", "Stop", "Pause" etc.

Note: Child applications are attached as "1.exe", "2.exe", "3.exe", and "4.exe" where the user can update or change the application. This sample application will animate an icon on the system tray of Windows.

Run / Start Service

"WinService -s" or "WinService -S"

Sample Image

When the above line is executed on the command prompt, the service "Sundar_Service" will be started.

VOID RunService(char* pPath, char* pName)

The user-defined RunService method will use the "StartService" method to start a service after getting the service control manager control handle and the service handle.

if(StartService(schService, 0, (const char**)NULL))

Sample Image

Sample Image

Stop / End Windows Service

"WinService -k" or "WinService -K"

Sample Image

This command will stop the service by sending the "SERVICE_CONTROL_STOP" control code to service the control manager using the "ControlService" method. This will lead to executing the user case functionality in the "ServiceHandler" method to stop the service.


Uninstall / Kill the Service from Windows

"WinService -u" or "WinService -U"

Sample Image

This command will uninstall the service from the Windows Service Manager database, after all its child processes have been stopped.


The "DeleteService" method marks the specified service for deletion from the service control manager database.

Sample Image


Sample Image

VOID WriteLog(char* pFile, char* pMsg)

The "WriteLog" method would log all the errors and the status of the service in the application path as "Sundar_Service.Log", with the service name as the log file name.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shanmuga Sundar.V
India India
Module Lead working in leading MNC company, india - bangalore.

Comments and Discussions

QuestionCreate a windows GUI service Pinmemberarunselvan6989@gmail.com26-Jun-14 0:20 
QuestionMessage Removed Pinmemberarunselvan6989@gmail.com18-Jun-14 20:26 
BugServices not running in Windows 7 machine PinmemberMember 819679019-Jan-14 19:07 
GeneralRe: Services not running in Windows 7 machine PinmemberBrannigansLaw20-Nov-14 13:52 
Questionservice not running in windows 7 PinmemberMember 819679018-Nov-13 22:30 
QuestionIt works on warm start, but it doesn't work on cold start Pinmemberleolsh200011-Sep-13 0:53 
QuestionInstall Automatically Pinmemberrajendran_t2-May-13 4:04 
GeneralMy vote of 5 PinmvpMichael Haephrati7-Mar-13 0:04 
Questionu did good job !!! PinmemberMember 986162124-Feb-13 22:30 
Questionproblem in pause Pinmembersamira baigy14-Aug-12 1:33 
QuestionAttachProcessNames() do not show when started Pinmemberalan9323-Jul-12 14:45 
QuestionError 87? The parameter is incorrect? Pinmemberalan9313-Jul-12 10:50 
QuestionHigh CPU usage after starting service Pinmemberstanlysamuel27-Jun-12 1:56 
AnswerRe: High CPU usage after starting service Pinmemberdcube923-Jul-12 6:41 
QuestionHow could i start windows service as administrator? Pinmemberkadvanimayur21-Jun-12 21:24 
QuestionHigh CPU usage after starting service Pinmemberh12126646-Jun-12 18:16 
AnswerRe: High CPU usage after starting service Pinmemberdcube923-Jul-12 6:42 
GeneralMy vote of 5 PinmemberSutharsan9-Mar-12 21:05 
Questionhow to start a service in local system Pinmembersanathbt13-Oct-11 0:09 
Questionservice not running in windows 7 PinmemberAvinash Pachar3-Aug-11 0:16 
AnswerRe: service not running in windows 7 Pinmembertischerr21-Nov-12 5:02 
GeneralRe: service not running in windows 7 Pinmembermapartha5-Dec-12 23:38 
GeneralMy vote of 5 PinmemberMember 771198813-Jun-11 21:57 
GeneralMy vote of 3 Pinmemberg_satish11-Jun-11 22:18 
General100% CPU Hog PinmemberMember 36461377-Jun-11 23:31 
GeneralRe: 100% CPU Hog PinmemberMember 36461377-Jun-11 23:43 
GeneralHello Brother PinmemberSutharsan15-Dec-10 2:48 
now I am also working in windows services kinda applications only. I was searching for some examples I found u. Thank u. - Sutharsan -
GeneralMy vote of 5 PinmemberShanmuga Sundar.V9-Oct-10 2:59 
QuestionUpdate the service on win7 64bit without reboot Pinmemberberni229-Sep-10 23:18 
AnswerRe: Update the service on win7 64bit without reboot PinmemberMember 819679018-Nov-13 20:56 
GeneralMy vote of 5 Pinmemberbadma_nathan21-Sep-10 18:09 
Generalany further details Pinmemberanilpremchand12-May-10 21:32 
GeneralThank you so much ...... Pinmemberjaifaizal1-Mar-10 17:24 
Question1053 error Pinmemberharsi8-Feb-10 4:38 
AnswerRe: 1053 error Pinmemberjohnpradeep3010-Oct-12 22:20 
Generalgood article PinmemberDonsw29-Nov-09 10:17 
QuestionSunder_Service is not working in Vista PinmemberRajan Pandey4-Aug-09 2:31 
AnswerRe: Sunder_Service is not working in Vista Pinmemberberni229-Sep-10 23:36 
Answerfork/join PinmemberOsberto29-Sep-12 9:25 
GeneralMy vote of 2 Pinmembermilindrogye3-Jul-09 21:51 
GeneralRe: My vote of 2 Pinmembermullen_kelly29-Jul-09 4:59 
QuestionWhat are the parameters to CreateService() to run as NetworkService? PinmemberAbhimankrishna25-Jun-09 1:36 
GeneralExcellent Work Pinmemberchamil madusanka25-Jun-09 0:13 
Questionhow to make the service to get stopped by itself once it has performed its job? PinmemberGilly Kumar23-May-09 12:49 
GeneralStop service in XP User Mode Pinmemberuwxzzuwxzz19-Apr-09 16:56 
GeneralService to monitor application in PCs in network Pinmembersantu6-Mar-09 2:52 
GeneralRe: Service to monitor application in PCs in network Pinmembersks200810-Mar-09 10:29 
GeneralNot able to start an exe as a service which exists in d://flotest/flo.exe Pinmembersks20082-Mar-09 4:09 
GeneralRe: Not able to start an exe as a service which exists in d://flotest/flo.exe Pinmembersks20083-Mar-09 4:53 
GeneralRe: Not able to start an exe as a service which exists in d://flotest/flo.exe Pinmembersks20083-Mar-09 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 29 Nov 2006
Article Copyright 2006 by Shanmuga Sundar.V
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid