[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-06-28
7
Medium Priority
?
234 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
6 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
Industry Leaders: 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 1500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

834 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