如何以编程方式调用“作为服务登录属性”窗口?我可以使用命令行和 mmc 执行此操作吗?
最佳答案
根据评论中的要求,我有一些非常简单的代码可以设置已注册服务的用户名和密码。当然,这需要在服务安装时完成,也就是当您拥有提升的权限时。代码恰好在 Delphi 中,但将其移植到另一种语言应该是微不足道的。函数调用都是 Windows API 调用,文档可以在 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;
我不确定你是如何注册你的服务的(你还没有说)但是很可能你正在做的服务注册已经能够设置用户名和密码。
如果您刚好正在调用 CreateService
然后在安装期间应该设置用户名和密码。
关于windows - 如何以编程方式调用 "Log on as a service Properties"窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324684/