简体   繁体   English

TerminateProcess()不会关闭应用程序

[英]TerminateProcess() doesn't close the application

I am trying to use the TerminateProcess to terminate an app launched by ShellExecuteEX like this: 我正在尝试使用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 gets opened but it never closes. IE被打开,但它从未关闭。 Am I doing something wrong? 难道我做错了什么?

According to MSDN , fMask must be set to SEE_MASK_NOCLOSEPROCESS for .hProcess to get set. 根据MSDN ,必须将fMask设置为SEE_MASK_NOCLOSEPROCESS才能设置.hProcess I would add a test to see if it is NULL. 我将添加一个测试以查看它是否为NULL。 As a side note I've always had better luck using CreateProcess . 附带说明一下,我一直很幸运使用CreateProcess

Edit: 编辑:

This is how you do it using 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);

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); 您应该添加错误检查功能,并可以使用以下AssocQueryString查询默认浏览器的路径: AssocQueryString如下所示: AssocQueryString(0,ASSOCSTR_EXECUTABLE,"http","open", szExe, &cchExe);

You can get more information by checking the returned hProcess (eg, in a debugger). 您可以通过检查返回的hProcess(例如,在调试器中)来获取更多信息。 Also make sure you have the SEE_MASK_NOCLOSEPROCESS flag set. 还要确保设置了SEE_MASK_NOCLOSEPROCESS标志。

But my psychic powers say: Opening an IE document doesn't necessarily create a new process. 但是我的心理力量说:打开IE文档并不一定会创建一个新的过程。 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 . 雷蒙德(Raymond Chen)提到了这篇博客文章的一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM