【发布时间】:2015-04-03 21:07:17
【问题描述】:
我在这里使用模板开始了我的服务设计:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb540475(v=vs.85).aspx
我只想打开记事本来证明我可以在服务中做一些事情,因为最终我想运行以在一个单独的进程中启动一个批处理文件,该进程将启动一个 nginx 服务器。然后我想运行一个停止服务器批处理文件来停止它。看起来很简单。
我创建了一个进程并在 C++ 项目的 main 中运行它(这个 main 注册了一个服务 main,以便在服务启动时调用)。
在 Visual Studio 的调试器中,我看到 createprocess 返回正常,然后记事本打开。
然后,我在注册的服务主程序中移动该代码,添加 30 秒的睡眠延迟,启动服务,然后将 Visual Studio 的调试器附加到该进程。我看到代码在创建记事本进程时遇到了断点。
然后创建进程函数返回正常,但由于某种原因记事本没有打开。
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 文件并写入文件。路径设置错误。非常感谢
【问题讨论】: