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