Am I in a Service or an Application?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a C# component that can be used in various executables. I need a way
in the code of this component to determine if the executable that loaded it
is a Windows Service -or- a Console/Windows application? i.e. within the code
of this component how can I determine if the process that loaded it is a
Service or App? I am looking for a C# sample.... thanks!
 
ToeKnee said:
I have a C# component that can be used in various executables. I need a way
in the code of this component to determine if the executable that loaded it
is a Windows Service -or- a Console/Windows application? i.e. within the code
of this component how can I determine if the process that loaded it is a
Service or App? I am looking for a C# sample.... thanks!

Maybe this will help:
http://msdn.microsoft.com/library/d...mserviceprocessservicestartmodeclasstopic.asp

/ Fredrik
 
2 ways to do this (as far as I know) and both of them involved unmanaged
code. (Boohoo !!).

1. Check and see if ur running as a system account. (But other processes
could be running as system account, so not so good).
2. The better and sureshot way, Use
advapi32.dll-->StartServiceCtrlDispatcher (more info here --
http://msdn.microsoft.com/library/en-us/dllproc/base/startservicectrldispatcher.asp?frame=true)
and if the return value is non zero, then u are running as a service. If it
is non zero then to be sure you'll have to call Kernel32.dll-->GetLastError,
if the return value is ERROR_FAILED_SERVICE_CONTROLLER_CONNECT, that means
ur running as a console app), if you get ERROR_INVALID_DATA then something
is really wrong, and if you get ERROR_SERVICE_ALREADY_RUNNING then you're
running as a service that is already running.

If you have enough flexibility, you can force your callers to send you
commandline parameters to run it either as a console app or as a service.
Here's how http://dotnetjunkies.com/WebLog/sahilmalik/posts/35295.aspx

Hope that helped :)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
ToeKnee said:
I have a C# component that can be used in various executables. I need a way
in the code of this component to determine if the executable that loaded
it
is a Windows Service -or- a Console/Windows application? i.e. within the
code
of this component how can I determine if the process that loaded it is a
Service or App? I am looking for a C# sample.... thanks!

Get the process id of the current process and use System.Management to query
the service database using the ProccId as search criteria.
Here's how to...
....

using System.Management;

....
Process p = Process.GetCurrentProcess();
bool service = IsService(p.Id);
....

static bool IsService(int Id)
{
bool ret = false;
ManagementObjectCollection Coll;
using(ManagementObjectSearcher Searcher = new
ManagementObjectSearcher("SELECT ProcessId from Win32_Service where
ProcessId =" + Id))
{
Coll = Searcher.Get();
if (Coll.Count > 0)
ret = true;
}
return ret;
}

Willy.
 
Why not just expose a property that the user of your components can set.
For example, expose a ServiceApplication property that the user must set to
TRUE if the component is in a service application or FALSE if the component
is not in a service application. Then all you would need to do is check the
related variable when you need to do something different for a service
application.

Quick, easy and flexible.

Robby
VB.Net
 
Back
Top