Click here to Skip to main content
15,889,281 members
Articles / Programming Languages / XML
Article

Running applications like services

Rate me:
Please Sign up or sign in to vote.
4.54/5 (6 votes)
11 Feb 2007CPOL1 min read 36.9K   761   43   3
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.

C#
// 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:

C#
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:

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:

XML
<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)


Written By
Web Developer
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIts a bit like.... Pin
robvon12-Feb-07 9:51
robvon12-Feb-07 9:51 
Useful idea.

See Also:

The Perfect Service by J. Ambrose Little

www.15seconds.com/issue/040624.htm
GeneralRe: Its a bit like.... Pin
Wojtek Jeziorczak12-Feb-07 20:45
Wojtek Jeziorczak12-Feb-07 20:45 
Generalthanks Pin
Hari Om Prakash Sharma11-Feb-07 21:56
Hari Om Prakash Sharma11-Feb-07 21:56 

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

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