PC Review


Reply
Thread Tools Rate Thread

Am I in a Service or an Application?

 
 
=?Utf-8?B?VG9lS25lZQ==?=
Guest
Posts: n/a
 
      5th Jan 2005
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!


 
Reply With Quote
 
 
 
 
Fredrik Wahlgren
Guest
Posts: n/a
 
      5th Jan 2005

"ToeKnee" <(E-Mail Removed)> wrote in message
news:87B198BA-D1C7-43F4-9DBA-(E-Mail Removed)...
> 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/de...classtopic.asp

/ Fredrik


 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      5th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:87B198BA-D1C7-43F4-9DBA-(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      5th Jan 2005

"ToeKnee" <(E-Mail Removed)> wrote in message
news:87B198BA-D1C7-43F4-9DBA-(E-Mail Removed)...
>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.


 
Reply With Quote
 
Robby
Guest
Posts: n/a
 
      5th Jan 2005


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

"ToeKnee" <(E-Mail Removed)> wrote in message
news:87B198BA-D1C7-43F4-9DBA-(E-Mail Removed)...
>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!
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application interaction with another application/service/Backgroundthread prathibhas@aztecsoft.com Microsoft Dot NET Compact Framework 3 6th Feb 2008 03:19 PM
Windows Service application Occurs Application Error Event ID 1000 =?Utf-8?B?VGVkZHk=?= Microsoft Dot NET Framework 1 11th Dec 2006 01:14 PM
How to specify Application Settings bindings for a Service Application ariel.viera@gmail.com Microsoft Dot NET Framework Forms 0 4th Dec 2006 09:11 PM
exchanging data between a win32 application (service) and a c# application (service) dthom Microsoft C# .NET 9 5th Apr 2005 05:17 PM
Restarting of the service application from the service application itself Andreas Microsoft Windows 2000 Developer 4 31st Dec 2003 01:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:45 AM.