Solved

Windows Service Change credentials and remote access (Urgent)

Posted on 2006-11-13
10
578 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_
[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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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