>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
[quoted text clipped - 3 lines]
> 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!
John,
Thanks for your reply. Well, the situation is that it is a well published
interface and I cannot change it at this stage. From my perspective all I
need to know if a Windows Service is instantiating the component or
'something else'. I know it sounds like a curious requirement - it is long
story, but in a nutshell "they" can run their app as a Service or as a
Console App and "they" want me (my component) to be able to automatically
determine what mode it is running in and behave accordingly. Seems I am stuck
with this *requirement* :(
Isn't there a way I can automatically determine at runtime if the component
got instantiated inside a Windows Service or a Console App? I am sure
someone can figure out (with a touch of Reflection magic?) how to do this...
TIA TIA TIA!
-ToeKnee
> >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
[quoted text clipped - 13 lines]
>
> John Saunders
Andrew - 06 Jan 2005 01:07 GMT
There is a static method of the ServiceController class in named
GetServices() which returns an array of ServiceController objects one
representing each service on the computer search through these and find the
service then use the Status property to find if the service is running. If
the service is running do the stuff associated with the service if not
assume console
> John,
>
[quoted text clipped - 41 lines]
>>
>> John Saunders
Richard Grimes [MVP] - 06 Jan 2005 21:20 GMT
> Thanks for your reply. Well, the situation is that it is a well published
> interface and I cannot change it at this stage. From my perspective all I
[quoted text clipped - 5 lines]
> stuck
> with this *requirement* :(
<sigh> customers, don't you just hate them?
The difference between a service and a GUI (or Console) app is three fold:
1) the service can start when the machine starts
2) the service can be automatically restarted if it dies
3) the service can (and should) run under an account other than the
interactive user
Given those, here's some ideas.
#3 is the only one that you can detect. You could get the WindowsPrincipal
and check whether it is in the BUILTIN\Users role (for a GUI/console app),
but that seems a bit lame.
Another idea is to get a StackTrace and walk the stack to the top. In a GUI
or console app the top of the stack will be the entrypoint (usually Main)
for the main thread or its the thread procedure for a worker thread. If the
app is a service then the service control manager will start the service and
will also start a thread that will run in the service. The top of stack for
OnStart top will be ServiceBase.ServiceQueuedMainCallback. Unfortunately,
most services start one or more worker threads to do their work and so
there's no specific method you can look for.
Another idea, which should work, is to get the entrypoint
(Assembly.GetExecutingAssembly() will give the current assembly, EntryPoint
will be non-null if the current assembly has the entrypoint, if it is null
call GetCallingAssembly() recursively until EntryPoint is non-null).
EntryPoint returns the MethodInfo for the entrypoint. Call
MethodInfo.Reflected type to get the type that has the entrypoint method,
and then call BaseType to see if that type is derived from ServiceBase. (For
a GUI its likely to derive from Form, for a console the base type will be
Object).
> Isn't there a way I can automatically determine at runtime if the
> component
> got instantiated inside a Windows Service or a Console App? I am sure
> someone can figure out (with a touch of Reflection magic?) how to do
> this...
> TIA TIA TIA!
Personally, I think the EntryPoint solution is the best one (and it fits
your request for some reflection magic ;-) ).
Richard

Signature
www.richardgrimes.com
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)