Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-28
7
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 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