Solved

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

Posted on 2006-06-28
7
223 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

18 Experts available now in Live!

Get 1:1 Help Now