C ++中的服务的自动延迟启动 [英] Automatic delay start for services in C++

查看:1933
本文介绍了C ++中的服务的自动延迟启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a service that runs an executable file, .exe. The problem is that the service is not starting the .exe, everytime I reboot. It lacks consistency. But when I manually set the startup type to Automatic (Delayed start) it works perfectly. How can I set this startup type to Automatic (Delayed start) via C++?

这是我自动启动的代码:

This is the code I use to start automatically:

如何将Service_auto_start更改为自动(延迟启动)?

How should I change that "Service_auto_start" to Automatic (Delayed 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
);


推荐答案

ChangeServiceConfig2函数可以做到这一点。

the ChangeServiceConfig2 function can do this.

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

设定 dwInfoLevel SERVICE_CONFIG_DELAYED_AUTO_START_INFO 将开始类型修改为延迟自动开始。

set dwInfoLevel to SERVICE_CONFIG_DELAYED_AUTO_START_INFO to modify the start type to Delayed Auto start.

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

Note that this value is not supported on Windows Server 2003 and Windows XP. Also, this only dives description rather than delaying the service startup

这篇关于C ++中的服务的自动延迟启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆