TerminateProcess() 不会关闭应用程序
我正在尝试使用 TerminateProcess 来终止由 ShellExecuteEX 启动的应用程序,如下所示:
SHELLEXECUTEINFO ExecuteInfo;
ExecuteInfo.fMask = SEE_MASK_FLAG_NO_UI; /* Odd but true */
ExecuteInfo.hwnd = NULL;
ExecuteInfo.cbSize = sizeof(ExecuteInfo);
ExecuteInfo.lpVerb = NULL;
ExecuteInfo.lpFile = "http://www.microsoft.com";
ExecuteInfo.lpParameters = "";
ExecuteInfo.lpDirectory = NULL;
ExecuteInfo.nShow = SW_SHOW;;
ShellExecuteEx(&ExecuteInfo);
//WaitForSingleObject(ExecuteInfo.hProcess, 0);
Sleep(4000);
TerminateProcess(ExecuteInfo.hProcess, 0);
IE 打开但永远不会关闭。我做错了什么吗?
I am trying to use the TerminateProcess to terminate an app launched by ShellExecuteEX like this:
SHELLEXECUTEINFO ExecuteInfo;
ExecuteInfo.fMask = SEE_MASK_FLAG_NO_UI; /* Odd but true */
ExecuteInfo.hwnd = NULL;
ExecuteInfo.cbSize = sizeof(ExecuteInfo);
ExecuteInfo.lpVerb = NULL;
ExecuteInfo.lpFile = "http://www.microsoft.com";
ExecuteInfo.lpParameters = "";
ExecuteInfo.lpDirectory = NULL;
ExecuteInfo.nShow = SW_SHOW;;
ShellExecuteEx(&ExecuteInfo);
//WaitForSingleObject(ExecuteInfo.hProcess, 0);
Sleep(4000);
TerminateProcess(ExecuteInfo.hProcess, 0);
IE gets opened but it never closes. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 MSDN,
fMask
必须设置为SEE_MASK_NOCLOSEPROCESS
才能设置.hProcess
。我会添加一个测试来查看它是否为 NULL。附带说明一下,我使用CreateProcess
。编辑:
这是使用 CreateProcess 执行此操作的方法:
您应该添加错误检查并可以使用以下命令查询默认浏览器的路径:
AssocQueryString
像这样:AssocQueryString(0,ASSOCSTR_EXECUTABLE,"http","open", szExe, &cchExe);
According to MSDN,
fMask
must be set toSEE_MASK_NOCLOSEPROCESS
for.hProcess
to get set. I would add a test to see if it is NULL. As a side note I've always had better luck usingCreateProcess
.Edit:
This is how you do it using CreateProcess:
You should add error-checking and could query the path of the default browser using:
AssocQueryString
like this:AssocQueryString(0,ASSOCSTR_EXECUTABLE,"http","open", szExe, &cchExe);
您可以通过检查返回的 hProcess(例如,在调试器中)来获取更多信息。另请确保您设置了
SEE_MASK_NOCLOSEPROCESS
标志。但我的通灵能力告诉我:打开 IE 文档并不一定会创建一个新进程。而且,如果确实如此,它可能不是您想象的那样:您可能创建的进程可能已经产生了另一个进程来实际托管文档。 Raymond Chen 在这篇博文的一半左右提到了这一点。
You can get more information by checking the returned hProcess (e.g., in a debugger). Also make sure you have the
SEE_MASK_NOCLOSEPROCESS
flag set.But my psychic powers say: Opening an IE document doesn't necessarily create a new process. And, if it does, it may not be the one you think it is: The process you may have created may have spawned yet another process to actually host the document. Raymond Chen mentions that about halfway down this blog post.