Solved

How to modify the Log On user for a windows service?

Posted on 2006-06-28
7
227 Views
Last Modified: 2010-04-23
I have developed a windows service using vb.net with a small forms application to start it, stop it, and set it to auto-start with the operating system.

I need to be able to modify the security context for the service, which is available in the service properties under the Log On tab.

How do I do this?

Thanks.
0
Comment
Question by:cresset
  • 3
  • 3
7 Comments
 
LVL 41

Expert Comment

by:graye
ID: 17001477
This is normally done from the "service installer" portion of your service.  Or, obviously, it can by using the control panel applet after the service is installed.

VB.Net has rather nice ServiceInstaller and ServiceProcessInstaller components for this purpose.   You can set the account in the ServiceProcessInstaller.

http://msdn2.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller.aspx
0
 

Author Comment

by:cresset
ID: 17001562
At present my forms application doesn't have a ServiceProcessInstaller.   I can start, stop, and set autostart on the service without one, although I'm happy to accept that I may be doing it wrong!   I'm trying to emulate the SQL Server Service Manager functionality just to make things easier for less computer literate users.  

Do this make a difference to your proposed solution?

Thanks for your help.


0
 
LVL 41

Expert Comment

by:graye
ID: 17002664
OOOOohhhh... you're not writing the service itself.   You're writing a WinForm client application that will control the service?

If so, you can disregard the service-related gobbly-gook that I wrote about above.

The Dot Net Framework has a built-in ServiceController class that will do *some* of the thing you would typically expect...

        ' add a reference to System.ServiceProcess.dll
        Dim sc As ServiceController
        sc = New ServiceController("TermService", PC)
        sc.Stop()
        sc.WaitForStatus(ServiceControllerStatus.Stopped)

However, it wasn't desgined to change the "conditions" of the service, the "log on user", "start mode", etc.  For that, I'd recommend Windows Management Instrumentation (WMI).  Here is an example to demonstrate the concepts (it just changes the start mode).

        ' add a reference to System.Management
        Dim obj As ManagementObject
        Dim inParams, outParams As ManagementBaseObject

        obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='TermService'")

        ' change the Start Mode to Disabled
        If obj("StartMode") <> "Disabled" Then
            ' Get an input parameters object for this method
            inParams = obj.GetMethodParameters("ChangeStartMode")
            inParams("StartMode") = "Disabled"

            ' do it!
            outParams = obj.InvokeMethod("ChangeStartMode", inParams, Nothing)
            Result = Convert.ToInt32(outParams("returnValue"))
            If Result <> 0 Then
                Throw New Exception("ChangeStartMode method error code " & Result)
            End If
        End If

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_service.asp
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cresset
ID: 17005698
Jeez!   I think I'm going to scratch my head for a bit.   I've been setting the conditions for start mode by updating the registry directly.  

I think I might re-write that bit so it uses your method...

It's late.   I'll test this in the morning before awarding you hundreds of points and building a small shrine to your brilliance!

0
 

Author Comment

by:cresset
ID: 17007279
OK.   Had a good look at this.   While it allows you to change the username (and even set it to .\LocalSystem), there doesn't appear to be any mechanism for setting the password.  

I've found scripting examples from following the link you provided, but I can't see how they can be converted to visual basic.

Any further help gratefully received!
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 17008606
The Change() method of the Win32_Service class allows you to change both the Login User and Password

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/change_method_in_class_win32_service.asp

Yeah, I'll admit... this is a complicated example to use as your first introduction to WMI...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question