gpt4 book ai didi

c++ - 有没有办法从服务中运行可执行文件?

转载 作者:可可西里 更新时间:2023-11-01 11:51:05 25 4
gpt4 key购买 nike

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

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

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

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

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

然后我将该代码移动到已注册的主服务中,添加 30 秒的 sleep 延迟,启动该服务,然后将 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文件并写入了一个文件。路径设置错了。非常感谢

最佳答案

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

关于c++ - 有没有办法从服务中运行可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29439677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com