Note that there are some explanatory texts on larger screens.

plurals
  1. POERROR_INVALID_HANDLE on TerminateProcess (VS C++)
    text
    copied!<p><em>Disclaimer: This is part of the requirement of the program, so it's not meant for anything bad. Feel free to point out any misuse if you spot one. I'm a beginner in C++.</em></p> <p>Basically, I'm trying to restart <code>Outlook.exe</code> on Windows using C++.</p> <p>And this is the code I used to restart Outlook.</p> <pre><code>#include &lt;TlHelp32.h&gt; void RestartOutlook() { PROCESSENTRY32 Pc = { sizeof(PROCESSENTRY32) }; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); MODULEENTRY32 Mo = {sizeof (MODULEENTRY32) }; if(Process32First(hSnapshot, &amp;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, &amp;fdwExit); TerminateProcess(process, fdwExit); char * path; if (Module32First(hModuleSnapshot, &amp;Mo)) { path = Mo.szExePath; STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&amp;si, sizeof(si)); si.cb = sizeof (si); CreateProcess(path, NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &amp;si, &amp;pi); } } }while(Process32Next(hSnapshot, &amp;Pc)); } } </code></pre> <p>The funny part is, this piece of code works perfectly fine on Windows 7. While on Windows XP (SP3), I get duplicated Outlook. The <code>GetLastError</code> gives me <code>6: ERROR_INVALID_HANDLE</code>. I am really clueless after hours of research.</p> <p>Any idea?</p> <p>Anyway, C++ is not my field. I do webs :)</p> <p>And the code above is a mixture of the following sources:</p> <blockquote> <p>1: <a href="http://www.istorya.net/forums/programming/107435-how-can-i-kill-a-process-using-c.html" rel="nofollow">http://www.istorya.net/forums/programming/107435-how-can-i-kill-a-process-using-c.html</a></p> <p>2: <a href="http://code.activestate.com/recipes/576362-list-system-process-and-process-information-on-win/" rel="nofollow">http://code.activestate.com/recipes/576362-list-system-process-and-process-information-on-win/</a></p> </blockquote> <p><em>Environment: Windows 7, Windows XP, VS2010, Outlook 2003, Outlook 2007, Outlook 2010</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload