Solved

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

Posted on 2006-06-28
7
224 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now