Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Typically, a Windows service -- used to be called NT service -- is a console application that does not have a message pump. A Windows service can be started without the user having to log into the computer and it won't die after the user logs off. However, it is hard -- sometimes impossible -- to use many existing ActiveX controls within a console application.

On the other hand, MFC and VB applications are Windows applications, so using ActiveX controls in MFC or VB programs is extremely easy. It would be nice to make your MFC and VB programs run like a Windows service, so that:

It is possible to write a Windows service as a Windows program, but here I am proposing a much easier solution. I have included with this article the source code for a simple Windows service program that can start and shut down other programs. All you need to do is install this service and modify the INI file. Here are the advantages of using this simple Windows service:

A little story

My desktop PC in the office was recently re-imaged by the support folks of my company. The first time I signed in, I noticed that a Java program was busy doing something in the background. When I looked deeper, it became clear that this Java program was started by a modified version of the Windows service explained in this article. Later, I found out that another development team in my company got this "great open source tool" from the internet and they modified it to run their Java programs :-).

XYNTService

XYNTService.exe is the name of the executable for this Windows service program. You may get this Windows service from other websites, but Code Project is the official place to get the most recent version. You can freely use and modify the source code included with this article. I am aware that there are other utility programs providing almost the same functionality as XYNTService. However, as you will see, XYNTService has more features and is a lot easier to use. For example, no editing of the registry is required. Here is how to use the program:

By default, the installed service will be started automatically when you reboot the computer. You can also start and shut down the service from the Control Panel or the Administrative Tools using the Services option. When the service is started, it will create all of the processes you defined in the XYNTService.ini file one by one. When the service is shut down, it will terminate each of the processes it created in reverse order. The XYNTService.ini file should be placed in the same directory as the executable. Here is a sample of the file:

[Settings] 
ServiceName = XYNTService 
CheckProcessSeconds = 30 
[Process0] 
CommandLine = c:\winnt\system32\notepad.exe 
WorkingDir= c:\ 
PauseStart= 1000 
PauseEnd= 1000 
UserInterface = Yes 
Restart = Yes 
[Process1] 
CommandLine = java.exe MyPackage.MyClass
Restart = No
UserName = 
Domain =
Password =

The ServiceName property specifies the name you want to use for this NT service. The default name is XYNTService. If you copy the executable and the INI file into a different directory and then modify the ServiceName property in the INI file, you can install and configure a different service!

The sections [Process0], [Process1], ..., etc. define the properties related to each of these processes. As you can see, there are two processes to create in this example. notepad.exe and java.exe are the names of these programs. You can specify the parameters for each of these processes in the CommandLine property. You must specify the full path of the executable file for the corresponding process in the CommandLine property unless the executable is already in the system path.

The CheckProcessSeconds property specifies if and how often you want to check the processes started by XYNTService. If the property has value 0, then no checking is done. If, for example, the property value is 30 then every 30 seconds XYNTService will query the operating system to see if the processes it started are still running. The dead ones will be restarted if the Restart property value (explained later) is defined as Yes for that process. The default value of this property, if you don't specify it, is 600 (10 minutes).

Note: In the previous versions of XYNTService, the ProcCount value specified how many processes were started by XYNTService. This is no longer required. The CheckProcess value is the number of minutes instead of seconds. The current version will work as before if you use CheckProcess instead of CheckProcessSeconds; you should not use both.

The WorkingDir property is the working directory of the current process. If you don't specify this property, then the working directory of the current process will be c:\winnt\system32. The PauseStart property is the number of milliseconds the service will wait after starting the current process and before starting the next process. This is useful where the next process depends on the previous process. For example, the second process has to "connect" to the first process in such a way that it does not run until the first process is finished with initialization. If you don't specify the PauseStart property, the default value will be 100 milliseconds.

When XYNTService is shut down, it will post WM_QUIT messages to the processes it created first and then call the Win32 function TerminateProcess. The PauseEnd property is the number of milliseconds the service will wait before TerminateProcess is called. This property can be used to give a process (started by XYNTService) a chance to clean up and shutdown itself. If you don't specify the PauseEnd property, the default value will be 100 milliseconds.

The UserInterface property controls whether a logged-in user can see the processes created by XYNTService. However, this only works when XYNTService is running under the local system account, which is the default. In this case, processes created by XYNTService will not be able to access a specific user's settings, such as mapped network drives, etc. You can configure XYNTService to run under a user account, which is done easily from the Control Panel or Administrative Tools. Just select Services and then double click XYNTService in the installed services list to bring up a dialog box.

The Restart property is used to decide whether you want XYNTService to restart a dead process. If this property is No, which is the default if you don't specify any value, then the corresponding process will not be restarted. If this property is Yes, then the dead process will be restarted by XYNTService. See the CheckProcess property above on how often dead processes are restarted. You can bounce (stop and restart) any process defined in the INI file from the command line. For example, the following command...

XYNTService -b 2

...will stop and restart the process defined in the [Process2] section of the INI file. XYNTService can also be used to start and stop other services from the command line. Here are the commands to start (run) and stop (kill) other services:

XYNTService -r NameOfServiceToRun
XYNTService -k NameOfServiceToKill

In particular, you can use the above commands to start and stop XYNTService from the command line! Important note: You cannot start XYNTService by running it from the command prompt without an argument.

All errors while running XYNTService are written into a log file in the same directory as the executable. The error code in the log file is a decimal number returned by the GetLastError API. You can look for it on MSDN.

Impersonating a user (new feature)

The UserName, Domain, and Password properties are used to impersonate a user. If UserName and Password are not empty, then the corresponding process will be started using the specified user account. If Domain is empty, then the specified user account must be an account on the local machine instead of a network account. In order to impersonate a user, the service must be run by a local system. Also, the corresponding user account must have the "logon as service" privilege. If you configure a service to run under a user account, that account will acquire the "logon as service" privilege. Please note that you won't be able to see the user interface of an impersonated process.

Since we may need to put a user password in the INI file, it is important to make sure that the INI file is only accessible by administrators and the local system. The following command will protect the current folder, making it accessible only by administrators and the local system.

cacls . /g system:F administrators:F

Frequently asked questions

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralTerminate childs
Gustavo Felisberto
0:47 12 Mar '10  
I had an issue when my service was a batch file that was running another process (in my case starting a java application). Terminating the service would just terminate the spawned CMD.exe.

I created a patch that will terminate any child processes. It was tested in Windows 2000 Server and Vista. Maybe it can be written better, but it was done in a few minutes. Hope it helps someone.

http://pastebin.com/NzK9qxF7[^]
QuestionCommand Prompt Window Displayed when service is started
gwlarue
13:03 14 Feb '10  
this is almost exactly what I was looking for when I began my search. Smile
Except, when my application gets started as a service, a command prompt window is displayed.
Is this a function of the xyntservice example, or my application?
I'd like to suppress this window......
thanks much,
AnswerRe: Command Prompt Window Displayed when service is started
gwlarue
13:08 14 Feb '10  
sorry for the post. did not see all the pages at the bottom, DOH!
I see someone else already has resovled this issue by adding the "hidden" option.
sorry for the "noise".
GeneralJoyToKey.exe shows up in the processes list in task manager however it doesn't work
codemonkeyman2
12:43 8 Feb '10  
JoyToKey is free program for emulating keyboard through gamepad, good for media center etc... I was using your fine app to have that program started even before loggin in. And it does starts up that program as i can see it in the processes list in task manager. However, none of the joystick keys emulate any of the keyboard keys. I can manually start the joytokey program and every thing work. just google joytokey and will be able to download that program. Thanks in advance for looking into it.
GeneralRe: JoyToKey.exe shows up in the processes list in task manager however it doesn't work
loyal ginger
10:18 9 Feb '10  
That's an interesting test. You said non of the joystick keys emulate any of the keyboard keys. That is probably because the program was started using the "system" credential. When you logged on to a user account, that account could not interact with the already started joytokey program because you had different credentials.

To solve the problem, try to start the program using the same credential.
Generalis possible to restart the service again if it is stopped?
Le@rner
2:08 25 Jan '10  
Hi all,

i taking help of your article its such a nice article and helps me alot.

i have one query,if service is stopped there is any option to restart the service automatically.

please help me for this.

thanks in advance.

To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.

GeneralRe: is possible to restart the service again if it is stopped?
RozenOren
6:16 25 Jan '10  
Hi,

[settings]
CheckProcessSeconds = seconds to check if your process is running.

[Process0]
Restart = Yes (confirm to xyntservice to restart your process if it's not running)

Regards,

Oren R.
GeneralRe: is possible to restart the service again if it is stopped?
Le@rner
18:25 26 Jan '10  
Hi ,

i m already do this but its restarting the process.

i want to restart the service if it is stop.

thanks.

To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.

GeneralRe: is possible to restart the service again if it is stopped?
RozenOren
20:40 26 Jan '10  
Hi,

I do not know of a way to restart the service itself.
but you can write a script to check the status of the service and stop and start it if the status is not running.
GeneralRe: is possible to restart the service again if it is stopped?
Le@rner
18:05 27 Jan '10  
can you please explain me with any example?

thanks.

To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.

GeneralRe: is possible to restart the service again if it is stopped?
RozenOren
9:59 28 Jan '10  
Hi,

You can copy the following lines to a notepad and save as cmd file.
I didn't check it but it should work.

This script will use the sc command to check if the service is running. If not, it will rty to stop and start the service.
You can set this batch to run from task scheduler every 1 minute.

------START

@echo off

for /f "tokens=4" %%a in ('sc query "YOUR_SERVICE_NAME_HERE" ^| find /i "STATE"') DO (
if NOT %%a==RUNNING (
sc stop YOUR_SERVICE_NAME_HERE
sc start YOUR_SERVICE_NAME_HERE
)
)

------END

Hope it will help you.

Oren
GeneralRe: is possible to restart the service again if it is stopped?
Le@rner
18:16 28 Jan '10  
this batch file really works.

but i dont know how can create task scheduler problematically.

can u please help me for this.

thanks.

To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.

GeneralRe: is possible to restart the service again if it is stopped?
RozenOren
23:24 28 Jan '10  
Can you please explain what you mean by "....create task scheduler problematically." ?
GeneralRe: is possible to restart the service again if it is stopped?
loyal ginger
6:50 31 Jan '10  
I think he wants to know how to run this script periodically to check the service status. He might want to use the Windows Scheduler to schedule the script to run. He wants to know how to programmatically put this task to the Windows Task Scheduler's list.
GeneralRe: is possible to restart the service again if it is stopped?
Le@rner
20:12 31 Jan '10  
yes sir,that is exactly what i mean.

please tell me ow to programmatically put this task to the Windows Task Scheduler's list.

thanks.

To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.

GeneralRe: is possible to restart the service again if it is stopped?
RozenOren
23:12 31 Jan '10  
Hi,

You can use the 'at' or 'schtasks' command-line utilities in a batch to create a new scheduled task.

Use this link to learn on how to use the schtasks utility.
It shows all command-line options and examples.

http://technet.microsoft.com/en-us/library/cc772785%28WS.10%29.aspx[^]
GeneralWhy does not my program start as a service using your program?
Shaoli Yang
22:14 13 Jan '10  
Hello!
At first thank you for your answer for my question!
My program does not start as a service using your program,can you help me? I can send my exe file to you using mail.Thank you! Laugh
GeneralRe: Why does not my program start as a service using your program?
loyal ginger
11:33 14 Jan '10  
Try to see if you can start notepad.exe, just to verify that you have the xyntservice installed correctly.

You need to give more details to the problem you are having. For example, did you use task manager to see if your program is running without showing a window?
JokeFantastic!
Mango45
10:53 10 Jan '10  
I just wanted to say that this program was perfect for what I needed to do. It solved all the problems I was having with srvany.exe and it is much easier to use. Thank you Mr. Liu!
GeneralRunning a logger application as a service wont write to file
RozenOren
9:29 31 Dec '09  
Hi,

I wrote a small application for writing specific lines to a text log file.
The application is working just fine if running in normal mode (double click).
When running as a service, the application wont write to the log file.

I'd appreciate your help.

Thanks,

Oren R.
AnswerRe: Running a logger application as a service wont write to file
JohnO1234
9:39 6 Jan '10  
The default directory for services is windows/system32

One way to fix is to use the full path in your logger application.
GeneralRe: Running a logger application as a service wont write to file
RozenOren
9:55 6 Jan '10  
So simple.

I really appreciate your answer and help. Big Grin

Thanks. Thumbs Up
GeneralOfficial Cleanse
hinerime
18:57 9 Feb '10  
really nice post this one.and i read your comment and i totally agree with u,

Official Cleanse
GeneralRunning XYNTService as a timer trigger
Rupertvz
11:19 23 Nov '09  
Hi Everyone,

I found this very handy and useful tool to trigger my small monitoring application - which should be called at set intervals.

However, I discovered calling my application every 5 minutes - it only worked for 2 - 4 hours and then the auto triggering by the service stopped.

The XYNTservice was still running, but the service log entries also stopped at this time.

Could this be related to the 127 max process instances?

My app is suppose to run for about 5 - 10 seconds every time. Then kills itself.

The requirement is for the XYNTservice to keep calling my app at 5 minute intervals for an unlimited time (no-stop).

any help or advise will be appreciated Roll eyes

Regards
Rupert
GeneralRe: Running XYNTService as a timer trigger
Xiangyang Liu 刘向阳
3:38 24 Nov '09  
The 127 max process is a limit on how many processes you can run simultaneouly. There is no code in XYNTService to prevent your program to be run after 2 - 4 hours. The log file, you should see "Failed to start program 'xxx', error code = yyy" if you program stopped running.



Last Updated 22 Feb 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010