Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

How to get the ServiceName

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jan Nielsen - 23 Dec 2004 14:36 GMT
I'm implementing a windows service using C# and dotnet. This service must be
able to be registered multiple times, and based on it's servicename I would
like to lookup parameters for the current instance in registry.
I think
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\myService\Parameters
would be the logical place to store these parameters. But I can't seem to
find a way to retrieve the servicename of the current instance of my
service.

I believe that if I had written this service in C, I could get the
servicename as it's passed as the first argument to the ServiceMain
function.
Is there a way to retrieve this information in a C# service ???

I've allready tried to to get this information from the arguments passed to
the OnStart function, but it seems like the first argument has been stripped
away.

Thanks in advance,
Jan Nielsen
Shariq Khan - 23 Dec 2004 15:21 GMT
Jan:

I write my service by inheriting from the System.ServiceProcess.ServiceBase
class. Couldn't you use the ServiceName property to get the name of the
service. See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemserviceprocessservicebaseclassservicenametopic.asp


Hope I understood you question, and this helps.
Cheers,

Shariq Khan
shariq@shariqkhan.com

> I'm implementing a windows service using C# and dotnet. This service must
> be able to be registered multiple times, and based on it's servicename I
[quoted text clipped - 16 lines]
> Thanks in advance,
> Jan Nielsen
Jan Nielsen - 23 Dec 2004 16:52 GMT
No, I don't think so. ServiceBase.ServiceName is initially an empty string
which it set to a hardcoded value (default "Service1").
I guess the value passed to ServiceMain by SCM is the name that the service
is registered with in the registry.

I believe the two values must match exactly when you have several services
in the same process. In my case I register the the same exe-file multiple
times, which makes it impossible for the two values to match in all
instances.

Kind regards,
Jan Nielsen
Steven Cheng[MSFT] - 24 Dec 2004 03:41 GMT
Hi Jan,

Thanks for your posting. As for the get ServiceName prorblem you mentioned,
here is some of my understandings:

When you build a NTService via .net. In its intaller , it will hardcode a
ServcieName(unique) and a DisplayName(MAYBE NOT unique)

such as
====================
this.serviceInstaller1.DisplayName = "MyNTService1\'s DisplayName";
this.serviceInstaller1.ServiceName = "MyNTService1";
this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
============================
and this ServiceName will identify this serviceInstance under the
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ registry and also in
the system's SCM.
Also, the ServiceBase.DisplayName and ServiceController.ServiceName
property also means this value.

And the DisplayName is the just the User Friendly DisplayName in the SCM.
(the ServiceController.DisplayName means this value)

So is the "ServiceName" you want to get  means the "DisplayName" ? If so ,
we can use the ServiceController.GetServices() to get all the
localmachine's installed services and get the running service via its
ServiceName property, then you can use the ServiceController.DisplayName to
access the user friendly name.

For example:

#NOTE that I've aslo try using the System.Environment.CommandLine to get
the commandline arguments of the service process, but it just contains the
exe file's path , not the ServiceName we want, you can also try it to
verify this.
===============================
protected override void OnStart(string[] args)
{
        StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory+
"\\" + "log.txt",true,System.Text.Encoding.UTF8);

try
{
               
    sw.WriteLine(" ");
    sw.WriteLine("------start event--------");
    sw.WriteLine("ServiceName: " + this.ServiceName);
           
    ServiceController[] services = ServiceController.GetServices();
    foreach(ServiceController sc  in services)
    {
        if(sc.ServiceName == this.ServiceName)
        {
            sw.WriteLine("DisplayName: " + sc.DisplayName);
            sw.WriteLine("ServiceType: " + sc.ServiceType.ToString());
        }
    }
               
    sw.WriteLine("Start at: " + DateTime.Now.ToString("YYYY-MM-DD   
HH:MM:SSfffffzzz"));

    sw.WriteLine("CommandLine: " + System.Environment.CommandLine);

}
finally
{
    sw.Close();
}
       
}
==================================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jan Nielsen - 24 Dec 2004 14:13 GMT
Hi Steven,

Thanks for your posting and trying to help, but unfortunately not exactly
what I was looking for.

I know the difference between the ServiceName and DisplayName, and it is the
ServiceName I need as I want to lookup some parameters in the registry.
I've not looked at the ServiceInstaller yet. So far I'm just registering my
service using the SC.exe utility.
With SC I can register my service multiple times, ex:
   sc.exe create ServiceName1 binPath= D:\myservice.exe
   sc.exe create ServiceName2 binPath= D:\myservice.exe

When I start the service "ServiceName1" I would like it to find it's
parameters in
HKLM\SYSTEM\CurrentControlSet\Services\ServiceName1\Parameters.
This way I can register my service as many time I want to, and still each
instance can have it's own configuration.

I got this idea from the old NT4 ressource kit utility SRVANY.EXE. This is a
service wrapper that can start any application that is not made as a real
service. Srvany can be registered with any name as long as it unique in the
system, and still it's able to lookup the value
HKLM\SYSTEM\CurrentControlSet\Services\"ServiceName"\Parameters\Application
to find out which application it should start.
This is exactly what I want my service to do, but I think this servicename
is delivered by the SCM to the ServiceMain function which I believe is
handled by the .net framework.

I found this other posting on the net:
http://www.dotnettalk.net/Receive_ServiceName_as_Argument_using_SCM-6151428-1292
-a.html

I seems to be the exact same problem.
The suggested solution, as I understand it, is to put the servicename at the
end of the ImagePath in registry, to have it passed to the executable as an
ordinary argument. Within the service this can be read as the second
argument (the path to the executable is the first argument).
I think it could be registered somewhat like this:
   sc.exe create ServiceName1 binPath= "D:\myservice.exe ServiceName1"

This is probably working, but requires the service to be registered in a
rather special way. I would like my service to be able to retrieve the real
servicename and not some argument. And I think SRVANY.exe proves it can be
done, the question is whether it can be done in a dotnet service.

Kind regards,
Jan Nielsen
Steven Cheng[MSFT] - 27 Dec 2004 07:50 GMT
Hi Jan,

Thanks for your followup. From your further description, I've got the your
concerns on this problem. However, based on my research, there seems hasn't
such a means to get the info in a .net based nt Service. For examle, if you
have  a service class compiled into Servicecls.exe. Then, you use  sc.exe
or .net installer to install it into SCM twice(with different ServiceName).
Then, when you start either one in the SCM, the
Servicecls.exe will be launched, but in the servicecls.exe 's Main
function, we haven't any means to determine which one in the SCM is
started.
I think currently, the means you mentioned that appending the ServiceName
as a commandline paramter  in the execute path maybe a possbile workaround.
Otherwise, we may need to provide different execute path for exe file for
the different services.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.