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.
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = proc.Arguments;
psi.FileName = proc.FileName;
psi.WorkingDirectory = proc.WorkingDirectory;
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.