为什么 CreateProcess() 不起作用?
我有一个程序尝试在 Windows 7 中重新启动资源管理器;它首先结束该过程,然后再次启动它。
简单的?
看起来确实有效。然而,我注意到一些有趣的事情:当我尝试在兼容模式下启动一个程序(例如 XP 的功率计算器 powertoy)时,它不再起作用!什么也没有发生 -进程创建并立即退出。
如果我通过任务管理器的“运行”对话框运行资源管理器,则程序运行良好。
这是怎么回事?!
#include <windows.h>
int main()
{
PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(si)};
TCHAR path[] = TEXT("explorer");
DWORD f = 0; //I tried a variety of these flags; didn't work
return CreateProcess(NULL, path, NULL, NULL, FALSE, f, NULL, NULL, &si, &pi);
}
编辑 1:
- 事实上,它是 32 位且禁用 WOW64 重定向并没有什么区别。我把它改成64位的,问题是一样的。
- 我尝试了
ShellExecute
但它也不起作用。
编辑 2:
完全相同的代码只为我工作了几次,然后又停止工作了......嗯?
I have a program that attempts to restart Explorer in Windows 7; it first ends the process, then starts it again.
Simple?
So it looks -- it indeed seems to work. However, I noticed something funny: When I try to start a program (say, XP's Power Calculator powertoy) under compatibility mode, it no longer works! Nothing happens -- the process is created and immediately quits.
If I run Explorer through the Run dialog of Task Manager, the program runs fine.
What's going on?!
#include <windows.h>
int main()
{
PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(si)};
TCHAR path[] = TEXT("explorer");
DWORD f = 0; //I tried a variety of these flags; didn't work
return CreateProcess(NULL, path, NULL, NULL, FALSE, f, NULL, NULL, &si, &pi);
}
Edit 1:
- The fact that it was 32-bit with WOW64 redirection disabled made no difference. I made it 64-bit and the issue was the same.
- I tried
ShellExecute
but it didn't work either.
Edit 2:
The same exact code just worked for me a couple of times, and then stopped working again... huh?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是“为什么”的真正答案,但我设法自己找到了如何来修复它:
如果我使用
CreateEnvironmentBlock
,然后就可以了。但我还是没弄清楚是什么原因造成的……
This isn't really an answer to the "why", but I managed to find out how to fix it myself:
Instead of copying environmental variables from the current process, if I copy them with
CreateEnvironmentBlock
, then it works.I still haven't figured out what's causing it, though...