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...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/sahi...sts/35295.aspx
Hope that helped :)
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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!