Tuesday 2 April 2013

Running commands after exe exit


I needed a EXE to call it's related uninstall.exe when it had finished running. There are many solutions to this but I was after something short and sweet.

Here's what I came across.
http://stackoverflow.com/a/3458433/2237946

so modified to my requirements..


std::string g_PostScript;
int main()
{

g_PostScript+="notepad.exe \"hello.txt\" & "; //add a command
g_PostScript+="uninstall.exe & "; //and another one..

//time to exit
ApplyPostScript();
return 0;
}
void ApplyPostScript()
{
if (g_PostScript.length()==0)
return;
g_PostScript="cmd.exe /C ping 1.1.1.1 -n 1 -w 3000 > Nul & "+g_PostScript;
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};

CreateProcess(NULL, (LPSTR)g_PostScript.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}