windows - 如何以编程方式调用 "Log on as a service Properties"窗口?

标签 windows administration system-administration mmc

如何以编程方式调用“作为服务登录属性”窗口?我可以使用命令行和 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/

相关文章:

windows - 如果不成功则暂停命令而不是自动关闭

C++/(MFC dummy) 和纯 Win MessageBox() - 如何删除消息队列或以其他方式删除现有的鼠标点击/按键缓冲区

iphone - 除了 Firefox 扩展之外,Mac 上还有其他好的 SQLite 图形管理工具吗?

linux - 如何在多个用户的家创建一个目录?

linux - "--target list"在qemu安装中的含义

linux - 如何在 Linux 中创建不可读的文件

windows - 是否可以在不链接到 c 库且不使用 ExitProcess() 的情况下从汇编代码创建 Windows exe?

c++ - 这种宏有什么意义?

php - Magento:检测管理员是否登录到前端页面

user-interface - 编辑waseem的管理GUI