use*_*443 1 windows batch-file windows-7
我需要从批处理文件(使用sc start XXX)启动服务,但仅限于配置了自动启动类型.
我读了指令,sc /?我尝试先调用sc qc XXX命令以查询它的配置,然后在结果上使用findstr,但sc qc XXX命令后出现以下错误:
[SC] QueryServiceConfig FAILED 122:
The data area passed to a system call is too small.
[SC] GetServiceConfig needs 718 bytes
Run Code Online (Sandbox Code Playgroud)
指定的服务不作为已安装的服务存在.
这很奇怪,因为我可以sc config XXX从命令行调用和停止/启动它.
我错过了什么吗?有没有更好的方法呢?
好的,我刚想通了.
首先,我必须道歉,因为最初的错误实际上是:
[SC] QueryServiceConfig FAILED 122:
The data area passed to a system call is too small.
[SC] GetServiceConfig needs 718 bytes
Run Code Online (Sandbox Code Playgroud)
并不是
[SC] OpenService FAILED 1060:
Run Code Online (Sandbox Code Playgroud)
正如我先说的那样.
显然,我必须向我的服务显式添加缓冲区大小:sc qc XXX 1000
在这之后,我注意到BINARY_PATH_NAME字段对于XXX非常长,所以我猜默认内存分配是不够的.
现在,既然我基本上欠我的职业生涯StackOverflow,我会发布我的完整代码:)
rem start a service, but only if it is configured as automatic, and only if it isn't running already
for /F "tokens=3 delims=: " %%H in ('sc qc %xxx% 1000^| findstr "START_TYPE"') do (
if /I "%%H" EQU "AUTO_START" (
rem check if service is stopped
for /F "tokens=3 delims=: " %%H in ('sc query %xxx% ^| findstr "STATE"') do (
if /I "%%H" EQU "STOPPED" (
echo net start %xxx%
net start %xxx%
) else (
echo %xxx% is already running
)
)
) else (
echo Skipping %xxx% since it's not defined as automatic start
)
)
Run Code Online (Sandbox Code Playgroud)