【问题标题】:Automatic delay start for services in C++C++中服务的自动延迟启动
【发布时间】:2015-12-15 20:30:45
【问题描述】:

我正在尝试创建一个运行可执行文件.exe 的服务。问题是服务没有启动.exe,每次我重新启动。它缺乏一致性。但是当我手动将启动类型设置为自动(延迟启动)时,它工作得很好。如何通过 C++ 将此启动类型设置为自动(延迟启动)?

这是我用来自动启动的代码:

我应该如何将“Service_auto_start”更改为自动(延迟启动)?

SC_HANDLE schService = CreateService
(
    schSCManager,                   /* SCManager database      */
    pName,                          /* Name of service         */
    pName,                          /* Service name to display */
    SERVICE_ALL_ACCESS,             /* Desired access          */
    SERVICE_WIN32_OWN_PROCESS|
      SERVICE_INTERACTIVE_PROCESS , /* Service type            */
    SERVICE_AUTO_START,             /* Start type              */
    SERVICE_ERROR_NORMAL,           /* Error control type      */
    pPath,                          /* Service's binary        */
    NULL,                           /* No load ordering group  */
    NULL,                           /* No tag identifier       */
    NULL,                           /* No dependencies         */
    NULL,                           /* LocalSystem account     */
    NULL
);

【问题讨论】:

    标签: c++ visual-c++ service windows-installer


    【解决方案1】:

    ChangeServiceConfig2 函数可以做到这一点。

    BOOL WINAPI ChangeServiceConfig2(
      __in          SC_HANDLE hService,
      __in          DWORD dwInfoLevel,
      __in          LPVOID lpInfo
    );
    

    dwInfoLevel 设置为SERVICE_CONFIG_DELAYED_AUTO_START_INFO 以将启动类型修改为延迟自动启动。

    请注意,Windows Server 2003 和 Windows XP 不支持此值。 另外,这只是潜水描述而不是延迟服务启动

    【讨论】:

      【解决方案2】:

      您应该使用 Windows Installer 5.0 及更高版本中提供的MsiServiceConfig Table

      这意味着该功能仅在 Windows 7/8 和 Server 2008R2/2012 中可用。这符合你的故事吗?如果是这样,请继续阅读。否则,一种替代方法可能是在您的 MSI 中创建一个注册表值 (DelayedAutoStart DWORD = 1 )。

      您没有提及您用于制作 MSI 的创作工具。如果该工具未公开此基础功能,则应将工具切换为具有此功能的工具。自定义操作不能很好地替代标准操作。

      【讨论】:

      • AM 使用 Visual Studio 安装和部署
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 2012-06-16
      相关资源
      最近更新 更多