Click here to Skip to main content
Click here to Skip to main content
Go to top

Running applications like services

, 11 Feb 2007
Rate this:
Please Sign up or sign in to vote.
How to run an application without a GUI or logging in.

Introduction

Sometimes you might need to run a standard Windows applications without its GUI or without logging in to Windows. Normally, you would use a service - set it to start automatically using a given user account. But, if the application is already built/installed, you need something like ProcessHost, described below.

The code

The idea is quite simple - write a service (ProcessHost) which will spawn other processes. The OnStart method of the Windows Service template runs through the configuration and uses the System.Diagnostics.Process.Start() static method to start processes.

// set process properties using configuration
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = proc.Arguments;
psi.FileName = proc.FileName;
psi.WorkingDirectory = proc.WorkingDirectory;

// start process and save it in processDesc
processDesc[descIndex].Config = proc;
processDesc[descIndex].Image = Process.Start(psi);

The System.Diagnostics.Process.Kill() method is used to stop processes:

processDesc[i].Image.Kill();
processDesc[i].Image.WaitForExit();

Don't forget to register the ProcessHost as a service using the .NET Framework InstallUtil.exe tool.

Configuration

Configuration is stored in an XML file (PHConfig.xml, in the example) which complies with the PHConfig.xsd schema.

PHConfig.xml:

<processes xmlns="urn://wjeziorczak-net/Schemas/ProcessHostConfig" />
  <processinfo />
    <filename />c:\windows\system32\mspaint.exe</filename />
    <workingdirectory />d:\</workingdirectory />
  </processinfo />
  <processinfo />
    <filename />c:\windows\notepad.exe</filename />
    <workingdirectory />d:\</workingdirectory />
    <arguments />file.txt</arguments />
  </processinfo />
  <processinfo />
    <filename />c:\windows\system32\calc.exe</filename />
  </processinfo />
</processes />

PHConfig.xsd:

<schema id="PHConfig" 
 xmlns="urn://wjeziorczak-net/Schemas/ProcessHostConfig"
 xmlns:xs=http://www.w3.org/2001/XMLSchema
 xmlns:mstns="urn://wjeziorczak-net/Schemas/ProcessHostConfig"
 targetnamespace="urn://wjeziorczak-net/Schemas/ProcessHostConfig"
 elementformdefault="qualified" />
  element name="Processes" />

Settings of every process must contain the executable file and additionally, the working directory and the argument string (command line arguments, in normal execution). At runtime, the configuration is deserialized into an object of the Processes class. That class is generated from the schema file using the xsd.exe tool.

What's next?

You can try to use more properties from the System.Diagnostics.ProcessStartInfo class. The ProcessHost will start a process in the same context as it is working, but if you set the Domain, UserName, and the Password properties, then the process will be started in the context of the given account - this would mimic the service's "log on as" feature. After changing PHConfig.xsd, run xsd.exe (xsd.exe PHConfig.xsd /c /n:WJeziorczak.ProcessHost) to update the Processes class in the PHConfig.cs file.

License

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

Share

About the Author

Wojtek Jeziorczak
Web Developer
Poland Poland
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralIts a bit like.... Pinmemberrobvon12-Feb-07 9:51 
GeneralRe: Its a bit like.... PinmemberWojtek Jeziorczak12-Feb-07 20:45 
Generalthanks PinmemberHari Om Prakash Sharma11-Feb-07 21:56 

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 11 Feb 2007
Article Copyright 2007 by Wojtek Jeziorczak
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid