How to invoke "Log on as a service Properties" window programmatically? Can I do this using the command line and mmc?
Asked
Active
Viewed 533 times
1
-
Why do you want to do this? Wouldn't it be easier to set the user account details when you install your service. This information can all be modified easily through the SCM API. The dialog is meant to be invoked by the interactive user. – David Heffernan Nov 30 '11 at 12:46
-
David, thank for your comment! I need to invoke this Window on our installation Wizard when customer specifies a User (Log On As) without authority to launch our service. So have a deal with existing system user. – Murat Korkmaz Nov 30 '11 at 13:32
-
The right way to do this is to build a username/password dialog in to your installer and set the properies through the scm. – David Heffernan Nov 30 '11 at 13:47
-
I've already have a username/password dialog, unfortunately I am not familiar with SCM yet. Does using SCM will works on Windows 2000...7, 8? – Murat Korkmaz Nov 30 '11 at 13:54
-
yes it does. How are you currently installing service. Should be part of that process. – David Heffernan Nov 30 '11 at 14:02
-
David, if it is not a tricky could you please drop here a few lines of code in SCM API how to assign "Log on as a service" rights to a user? – Murat Korkmaz Nov 30 '11 at 14:21
1 Answers
2
As requested in comments, I have some very simple code that will set the username and password of an already registered service. Naturally this needs to be done at service install time which is when you have elevated rights. The code happens to be in Delphi but it should be trivial to port it to another language. The function calls are all Windows API calls and the documentation can be found in MSDN.
SvcMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SvcMgr=0 then begin
RaiseLastOSError;//calls GetLastError and raises appropriate exception
end;
Try
//Name is the name of service and is used here to identify the service
hService := OpenService(SvcMgr, PChar(Name), SC_MANAGER_ALL_ACCESS);
if hService=0 then begin
RaiseLastOSError;
end;
Try
if not ChangeServiceConfig(
hService,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
nil,
nil,
nil,
nil,
PChar(Username),//PChar just turns a Delphi string into a null-terminated string
PChar(Password),
nil
) then begin
RaiseLastOSError;
end;
if not ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, @ServiceDescription) then begin
RaiseLastOSError;
end;
Finally
CloseServiceHandle(hService);
End;
Finally
CloseServiceHandle(SvcMgr);
End;
I'm not sure how you are registering your service (you did not say yet) but it's quite possible that the service registration you are doing is already capable of setting username and password.
If you already happen to be calling CreateService
during install then that is the point at which username and password should be set.

David Heffernan
- 601,492
- 42
- 1,072
- 1,490