Solved

Windows Service Change credentials and remote access (Urgent)

Posted on 2006-11-13
10
577 Views
Last Modified: 2007-11-27
Hi,

Is it possible to programatically change the logon credentials for a windows service after it is installed?

The case is: I'm writing a windows service to be installed on a server, and controlled from an admin app. At some point the admin will need to make the service log on using a different account to perform certain operations (it doesn't matter if it has to restart).

Also I need to control the service from a remote computer in the same domain, do I have to set up a remoting connection, or only change domain user access?
0
Comment
Question by:Harlequin_
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17929941
When you says "control" the serveice do you mean stop, start pause, continue, restart ?
Wndows already provides and interface to doing this.
You can also set the account used by a service easily both locally and remotely, are you sure you want to do it programmatically ?
0
 

Author Comment

by:Harlequin_
ID: 17929998
>When you says "control" the serveice do you mean stop, start pause, continue, restart ?
Yes, plus the ExecuteCommand(int) / OnCustomCommand(int) to make the service perform custom commands on demand.

>Wndows already provides and interface to doing this.
Yes, but I am building a simplified admin tool for relatively novice users, and would like to let them use only one app for all functionality.

>You can also set the account used by a service easily both locally and remotely, are you sure you want to do it programmatically ?
Yes, for the same reasons.

The most urgent part is to be able to run custom commands from a different computer (using the admin app).
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17930241
Are you using the ServiceController class ?

ServiceController controller = new ServiceController("servicename", "remotemachinename");
controller.Start();
controller.Stop();
controller.ExecuteCommand(int);

etc ?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17930265
If you cannot only use ExecuteCommand I think you can generate a proxy class for the remote service object using controller.CreateObjRef(Type);

Still not sure how you would set/change the service account username/password.
0
 

Author Comment

by:Harlequin_
ID: 17930308
Yes, I'm using ServiceController, and I don't need other than start/stop/executecommand etc.

Do you know what permissions is needed to use the servicecontroller to control a windows service on a different computer?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17930519
I think it will depend on the local policies on the machine but I'm not sure what the defaults are.
0
 

Author Comment

by:Harlequin_
ID: 17930882
I'll do some more reading tonight. Just found out that ExecuteCommand only accepts integers betweer 128 & 256.. :-\

Do you have a complete example of using controller.CreateObjectRef(Type) - objectRef.GetRealObject - etc with all the remoting settings needed?
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17934420
I'm afraid I don't have any examples of this but I think its fairly routine remoting stuff so there should be lots of examples available.

As for changing the account and password used for running the service you need this :

[DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern Boolean ChangeServiceConfig(IntPtr hService, UInt32 nServiceType, UInt32 nStartType, UInt32 nErrorControl, String lpBinaryPathName, String lpLoadOrderGroup, IntPtr lpdwTagId, String lpDependencies, String lpServiceStartName, String lpPassword, String lpDisplayName);


For more information on how to use this method see : http://www.pinvoke.net/default.aspx/advapi32.ChangeServiceConfig
0
 

Author Comment

by:Harlequin_
ID: 17936871
I think this is as far I'll get without doing more research myself :)

Thanks for your help!
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17936947
No problem. Good luck.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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