如何以编程方式调用“以服务属性登录”窗口?

时间:2011-11-30 10:48:58

标签: windows administration system-administration mmc

如何以编程方式调用“以服务属性登录”窗口?我可以使用命令行和mmc吗?

1 个答案:

答案 0 :(得分:2)

根据评论中的要求,我有一些非常简单的代码,用于设置已注册服务的用户名和密码。当然,这需要在服务安装时完成,即当您拥有更高权限时。代码碰巧在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,那么就应该设置用户名和密码。