Click here to Skip to main content
13,044,584 members (62,051 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 11 Feb 2007

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.


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:


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


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


<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 />


<schema id="PHConfig" 





 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.


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


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.... Pin
robvon12-Feb-07 9:51
memberrobvon12-Feb-07 9:51 
GeneralRe: Its a bit like.... Pin
Wojtek Jeziorczak12-Feb-07 20:45
memberWojtek Jeziorczak12-Feb-07 20:45 
Generalthanks Pin
Hari Om Prakash Sharma11-Feb-07 21:56
memberHari 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Feb 2007
Article Copyright 2007 by Wojtek Jeziorczak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid