当在64箱计算机中使用32个bin应用程序时,terminateProcess()失败
TerminateProcess() Fails when called upon a 32 bin application in a 64 bin machine
请浏览以下代码:
LPSTR commandBuffer = ""C:\Program Files (x86)\Notepad++\notepad++.exe"";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_MINIMIZE;
if (CreateProcess(NULL,
commandBuffer,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi)) {
Sleep(5000);
UINT exitCode = 0;
if (!TerminateProcess(pi.hProcess, exitCode))
std::cout << GetLastError() << std::endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
当我在64个bin Windows 7机器上使用32 bin应用程序(记事本 exe)创建一个过程时,TerminateProcess()
使用错误代码5(访问拒绝)失败,但是如果我使用64位应用程序创建一个进程(即LPSTR commandBuffer = ""C:\Program Files\depends22_x64\depends.exe""
)在同一台计算机上没有错误。我需要理解为什么会发生这种情况,以及我如何能够正确终止在64位Windows 7机器上使用32位应用程序创建的过程。
此代码是错误的:
if (TerminateProcess(pi.hProcess, exitCode))
std::cout << GetLastError() << std::endl;
终止程序在故障上返回0,成功返回0。c if()语句将非零值评估为true,零为false。因此,您需要写:
if (TerminateProcess(pi.hProcess, exitCode) == 0)
std::cout << GetLastError() << std::endl;
至于实际问题。我无法在Windows 10 64位上复制它,但是如果它持续存在,请尝试以下内容:
而不是使用从CreateProcess()
返回的过程句柄,而是使用OpenHandle()
在从CreateProcess()
返回的过程ID上明确指定PROCESS_TERMINATE
。然后使用该句柄使用ProcessTerminate()
终止该过程。
您还需要考虑的是,诸如Notepad 之类的应用程序在已经运行时会自动终止自身,而是激活已经运行的过程,也许告诉已经运行的实例以打开文件或某些此类文件。在这种情况下,终止创建过程对之前已经运行的过程没有影响。
相关文章:
- 没有找到相关文章