Click here to Skip to main content
Licence CPOL
First Posted 11 Feb 2007
Views 22,745
Downloads 424
Bookmarked 40 times

Running applications like services

By | 11 Feb 2007 | Article
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)

About the Author

Wojtek Jeziorczak

Web Developer

Poland Poland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIts a bit like.... Pinmemberrobvon9:51 12 Feb '07  
GeneralRe: Its a bit like.... PinmemberWojtek Jeziorczak20:45 12 Feb '07  
Generalthanks PinmemberHari Om Prakash Sharma21:56 11 Feb '07  

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.

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