ERROR_INVALID_HANDLE on TerminateProcess (VS C ++)

Disclaimer: this is part of the program’s requirement, so it doesn’t mean anything bad. Feel free to point out any abuse if you notice it. I am new to C ++.

Basically, I am trying to restart Outlook.exe on Windows using C ++.

And this is the code that I used to restart Outlook.

 #include <TlHelp32.h> void RestartOutlook() { PROCESSENTRY32 Pc = { sizeof(PROCESSENTRY32) }; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); MODULEENTRY32 Mo = {sizeof (MODULEENTRY32) }; if(Process32First(hSnapshot, &Pc)){ do{ if(!_stricmp(Pc.szExeFile, "outlook.exe")) { DWORD pid = Pc.th32ProcessID; HANDLE hModuleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid); //kill outlook HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); DWORD fdwExit = 0; GetExitCodeProcess(process, &fdwExit); TerminateProcess(process, fdwExit); char * path; if (Module32First(hModuleSnapshot, &Mo)) { path = Mo.szExePath; STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof (si); CreateProcess(path, NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); } } }while(Process32Next(hSnapshot, &Pc)); } } 

The funny thing is: this piece of code works fine on Windows 7. On Windows XP (SP3), I get duplicated Outlook. GetLastError gives me 6: ERROR_INVALID_HANDLE . I am really elusive after hours of research.

Any idea?

In any case, C ++ is not my field. I make web pages :)

And the above code is a mixture of the following sources:

1: http://www.istorya.net/forums/programming/107435-how-can-i-kill-a-process-using-c.html

2: http://code.activestate.com/recipes/576362-list-system-process-and-process-information-on-win/

Environment: Windows 7, Windows XP, VS2010, Outlook 2003, Outlook 2007, Outlook 2010

+4
source share
3 answers

I found the culprit.

The reason for this line is:

 HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); 

According to http://msdn.microsoft.com/en-us/library/ms684880(v=vs.85).aspx , PROCESS_ALL_ACCESS too large for Windows XP / NT or more details:

The PROCESS_ALL_ACCESS flag size has increased in Windows Server 2008 and Windows Vista. If an application compiled for Windows Server 2008 and Windows Vista runs on Windows Server 2003 or Windows XP / 2000, the PROCESS_ALL_ACCESS flag is too large, and the function that defines this flag does not work with ERROR_ACCESS_DENIED. To avoid this problem, specify the minimum set of access rights required for the operation.

Definitely I am compiling this program on 7, working on XP, certainly causing a problem.

So there is a solution, change PROCESS_ALL_ACCESS to PROCESS_TERMINATE , which

 HANDLE process = OpenProcess(PROCESS_TERMINATE, TRUE, pid); 

Done!

Thanks @DReJ for the quick answers :)

+4
source

I get that you want Outlook to restart, but calling TerminateProcess in Outlook seems to be bad in the first place. What if it is in the middle of writing a data file?

The best way would be to find all the top-level windows owned by Outlook, send them WM_CLOSE, and then wait for the process to complete. (You may also need to make sure that the user is looking at draft messages that lead to the “are you sure” query, although if you do this first, I assume that you know that the user is not in the middle of something then?)

Even better would be to use the Outlook automation interface and tell it to explicitly disable it.

+2
source

Your problem may be related to this piece of code.

 DWORD fdwExit = 0; GetExitCodeProcess(process, &fdwExit); TerminateProcess(process, fdwExit); 

Firstly, with GetExitCodeProcess you get the status STILL_ACTIVE , and after that you end the process with this status, which I do not think is right. Remove GetExitCodeProcess from your code and try TerminateProcess(process, 0); instead of this.

+1
source

Source: https://habr.com/ru/post/1335538/


All Articles