How to detect if the current process (or dll) run as service ???

  • Thread starter Thread starter Thierry
  • Start date Start date
T

Thierry

Hi.

I am working on a managed dll.
What code can I use in C# to detect if my dll is running under a
service (like IIS) or called from a 'normal' program.

Thanks

Thierry
 
Check the value of the System.Environment.UserName property. If it is a
non-user account, such as "SYSTEM" or "Network", then it is safe to assume
that the process is running as a service.

Good luck,
Derek Stone
EliteVB.com
 
That is not necessarily correct because the user could change the identity
that the service is running under.

The problem is that a service is nothing more than an executable. Here are
a couple things that you might try. I have not actually done these but they
make logical sense if you can get them to work.

1) All services inherit from ServiceBase. You might check the call stack
to see if the very first method called belonged to a class that inherited
from ServiceBase.
2) Or you could use the ServiceController to get the names of all services
registered in the system and compare them with your application. If there
is a match, you may be running in a service. This is not perfect I guess
since it might be possible to have a service and a regular application with
the same name.
 
Back
Top