473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Am I in a Service or an Application?

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!
Nov 16 '05 #1
4 2349
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!

Nov 16 '05 #2

"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!


Maybe this will help:
http://msdn.microsoft.com/library/de...classtopic.asp

/ Fredrik
Nov 16 '05 #3

"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!


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.
Nov 16 '05 #4


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" <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!

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Chakkaradeep | last post by:
Hi all, i have written a Service,now i want to execute another application (for eg;calc.exe) in the service....how will i perform it??... i tried using this.... /**************Executing a...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
3
by: Jeremy S. | last post by:
On my dev machine (XP/Pro with VS.NET 2003) I have been developing a Windows Service and installing it on the local machine by opening the Visual Studio Command Prompt and then executing . Now I...
2
by: Randall Powell | last post by:
I am in the process of developing a Windows Service which will: (1) monitor multiple network shares; (2) marshal text file transfers into an SQL Server 2000 instance; and (3) provide messaging...
3
by: =?Utf-8?B?RGFuZGFuIFpoYW5n?= | last post by:
Now I have a web application, a web service and a SQL Server database. The Web application will invoke the web service, the web service invokes the SQL Server stored procedure. I let the web...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
3
dmjpro
by: dmjpro | last post by:
plz send me a good link which can clearify me how the J2EE framework works i want the details information .... plz help thanx
5
by: sonali_reddy123 | last post by:
Hello all, I am trying to develop an application which will run as a windows service. The application should have Normal options available with service like start, stop and pause but along...
0
by: =?Utf-8?B?U2ltb25EZXY=?= | last post by:
Hi All I would like to install the same Windows Service project on the same server under different names, one for each customer. I have been able to do it but I would like an expert opinion as...
22
by: robertgregson | last post by:
Using C#, .NET3.5, Visual Studio 2008 and WCF on Windows VISTA SP1, I have written a service, service host (as a C# console application) and a client. The service uses...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.