1

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

4

1 回答 1

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,那么这就是应该设置用户名和密码的地方。

于 2011-11-30T14:31:28.520 回答