简体   繁体   English

在批处理文件中运行命令时安装 Windows 服务失败

[英]Installing Windows Service fails when command run in batch file

I'm converting a legacy VC 6 C++ Windows Service application to Visual Studio 2022我正在将旧版 VC 6 C++ Windows 服务应用程序转换为 Visual Studio 2022

The conversion is completed and, if I open a CMD prompt with Admin rights, I can install the service using this command:转换完成,如果我以管理员权限打开 CMD 提示符,我可以使用以下命令安装服务:

Service.exe /Install服务.exe /安装

It installs instantly and starts correctly.它会立即安装并正确启动。

However, I need to install it within an installation script, which loads a bunch of services by calling a batch file which contains the commands to start them.但是,我需要在安装脚本中安装它,该脚本通过调用包含启动它们的命令的批处理文件来加载一堆服务。 It is run with elevated permissions.它以提升的权限运行。 However, which it reaches the above line (Service.exe /Install) it hangs.但是,它到达上面的行(Service.exe /Install)它挂起。 There is no error message and I cannot even terminate it using CTRL + C. The only way around it is to close the CMD prompt.没有错误消息,我什至无法使用 CTRL + C 终止它。解决它的唯一方法是关闭 CMD 提示符。 The service is not installed.该服务未安装。

My service does not appear in the Task manager's list of processes when the batch file hangs.当批处理文件挂起时,我的服务没有出现在任务管理器的进程列表中。

I've tried adding the full path to the service in the batch file but it makes no difference.我尝试在批处理文件中添加服务的完整路径,但没有任何区别。 Running this batch file from an elevated command prompt (rather than the installer script) runs into the same problem.从提升的命令提示符(而不是安装程序脚本)运行此批处理文件会遇到同样的问题。

I'm tearing my hair out over this (almost bald now :-)) - can anyone provide any suggestions?我为此撕毁了我的头发(现在几乎秃了:-)) - 任何人都可以提供任何建议吗?

Thanks谢谢

Andy安迪

Debugging found a logic error in the start up code.调试发现启动代码中有逻辑错误。

Basically, calling Service.exe /Install directly from the command line just passes "Service.exe /Install" as the command line, whereas calling it from a batch file / CreateProcess() passes the whole path to the service in the command line and the parser was not taking that into account.基本上,直接从命令行调用 Service.exe /Install 只是将“Service.exe /Install”作为命令行传递,而从批处理文件 / CreateProcess() 调用它会将整个路径传递给命令行中的服务,并且解析器没有考虑到这一点。 Maybe it behaved differently in older Windows versions, but the parser was really badly written.也许它在旧 Windows 版本中的行为有所不同,但解析器写得真的很糟糕。

I'm glad it was me who wrote it :-)我很高兴是我写的:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM