TerminateProcess() 不会关闭应用程序

发布于 2024-11-08 02:02:40 字数 587 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-11-15 02:02:41

根据 MSDNfMask 必须设置为 SEE_MASK_NOCLOSEPROCESS 才能设置 .hProcess。我会添加一个测试来查看它是否为 NULL。附带说明一下,我使用 CreateProcess

编辑:

这是使用 CreateProcess 执行此操作的方法:

PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;


CreateProcess(  NULL,
                "C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.google.com/",
                NULL,
                NULL,
                FALSE,
                NORMAL_PRIORITY_CLASS,
                NULL,
                NULL,
                &si,
                &pi );


Sleep(4000);
TerminateProcess(pi.hProcess, 0);

您应该添加错误检查并可以使用以下命令查询默认浏览器的路径: AssocQueryString 像这样:
AssocQueryString(0,ASSOCSTR_EXECUTABLE,"http","open", szExe, &cchExe);

According to MSDN, fMask must be set to SEE_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 using CreateProcess.

Edit:

This is how you do it using CreateProcess:

PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;


CreateProcess(  NULL,
                "C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.google.com/",
                NULL,
                NULL,
                FALSE,
                NORMAL_PRIORITY_CLASS,
                NULL,
                NULL,
                &si,
                &pi );


Sleep(4000);
TerminateProcess(pi.hProcess, 0);

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);

不醒的梦 2024-11-15 02:02:41

您可以通过检查返回的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文