有没有办法从服务中 运行 一个可执行文件?

Is there a way to run an executable from within a service?

我使用这里的模板开始了我的服务设计:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb540475(v=vs.85).aspx

我只想打开记事本来证明我可以在服务中做一些事情,因为最终我想 运行 在将启动 nginx 服务器的单独进程中启动批处理文件。然后我想运行一个停止服务器的批处理文件来停止它。看起来很简单。

我创建了一个进程,运行它在 C++ 项目的 main 中(这个 main 注册了一个服务 main,在服务启动时调用)。

在 visual studio 的调试器中,我看到 createprocess returns 正常,然后记事本打开。

然后我将该代码移动到已注册的主服务中,添加 30 秒的睡眠延迟,启动该服务,然后将 visual studio 的调试器附加到该进程。我看到代码在创建记事本进程时遇到了我的断点。

然后创建进程函数returns很好,但是由于某些原因记事本打不开。

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

if (!CreateProcess(
        "C:\Windows\System32\Notepad.exe",   // No module name (use command line)
        NULL,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi           // Pointer to PROCESS_INFORMATION structure
        )
        )
    {
        printf("CreateProcess failed (%d).\n", GetLastError());
    }

我成功地运行bat文件并写了一个文件。路径设置错了。非常感谢

服务 运行 在没有桌面的单独用户帐户下。您可以 运行 完美地正常处理,但您无法显示任何 UI.

转到http://nginx-win.ecsds.eu/ 并下载 Install_nginx_php_services.zip,阅读脚本如何用于 nginx 和 php 以及其他任何东西,所用工具的来源可以在 MS 的网站上找到。