Who's starting my service ??

  • Thread starter Thread starter Soren S. Jorgensen
  • Start date Start date
S

Soren S. Jorgensen

Hi,

How do I see, from within my main method, in what context my assembly,
containing my service program, is executed ??

I want to be able to detect if the assembly is executed by the service
system (say started from the Services applet) or if it was executed from a
command line (in which case the service won't start).

The reason for this is that on my service assembly I have created the
ability to install/uninstall the service program from a command line. Say
you call the assembly like: "MyService.exe -install" from the command line
the assembly will be installed as a service. Executing the assembly without
arguments will run the service. So if user simply executes the assembly from
a command line (without arguments) will cause an error as the service must
be started by the system. I'd like to detect this up front, and tell the
user he/she has done something not allowed.

Any help appreciated...

Btw. running .NET 1.1

Kr. Soren
 
Soren,

I would check the current working directory. If it is not the system
directory, then you know that the service is not running your program.

From the documents for the ServiceBase class:

The current working directory of the service is the system directory, not
the directory in which the executable is located.

Hope this helps.
 
| Hi,
|
| How do I see, from within my main method, in what context my assembly,
| containing my service program, is executed ??
|
| I want to be able to detect if the assembly is executed by the service
| system (say started from the Services applet) or if it was executed from a
| command line (in which case the service won't start).
|
| The reason for this is that on my service assembly I have created the
| ability to install/uninstall the service program from a command line. Say
| you call the assembly like: "MyService.exe -install" from the command line
| the assembly will be installed as a service. Executing the assembly
without
| arguments will run the service. So if user simply executes the assembly
from
| a command line (without arguments) will cause an error as the service must
| be started by the system. I'd like to detect this up front, and tell the
| user he/she has done something not allowed.
|
| Any help appreciated...
|
| Btw. running .NET 1.1
|
| Kr. Soren

With a litle help from System.Management and WMI...


using System;
using System.Management;
using System.Diagnostics;
....
string parent = GetParentProcess(Process.GetCurrentProcess().Id);
if(parent.ToLower() != "services")
// not started by SCM

}

private string GetParentProcess(int procId)
{
string parentProcess = null;
string q = string.Format("select ParentProcessId from
Win32_Process where Handle={0}", procId);
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(new SelectQuery(q)))
{
ManagementObjectCollection processes = searcher.Get();
foreach(ManagementObject process in processes)
{
Process parent =
Process.GetProcessById((int)(uint)process.Properties["ParentProcessId"].Value);
parentProcess = parent.ProcessName;
}
}
return parentProcess;
}

Willy.
 
Back
Top