Restart Application

May 9, 2011 at 7:19pm
Is there a Windows API function for restarting an app? If so, I can't find it.

If not, does anybody know an easy way to code for an application close/restart?
May 9, 2011 at 8:11pm
Use GetModuleFilename() + CreateProcess() or ShellExecute() to launch a new instance of your application and then exit the current one.
May 9, 2011 at 8:24pm
CreateToolhelp32Snapshot(...): Takes a snapshot of all processes running in memory: http://msdn.microsoft.com/en-us/library/ms682489(VS.85).aspx

Process32First(...): Gets the first process listed from the previous call to CreateToolhelp32Snapshot(...), this should be the "System" process: http://msdn.microsoft.com/en-us/library/ms684834(VS.85).aspx

Process32Next(...): Gets info on the next process in the snapshot: http://msdn.microsoft.com/en-us/library/ms684836(VS.85).aspx

Both Process32First(...) and Process32Next(...) fill in the variables to a structure called a PROCESSENTRY32: http://msdn.microsoft.com/en-us/library/ms684839(VS.85).aspx which has a data member szExeFile that contains the process name, you should check this variable against the name of the process you are looking for. Another member of note is th32ProcessID which will be the Process ID you pass to the next function.

OpenProcess(...) to get the processes handle: http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx

GetExitCodeProcess(...) to get the exit code for the process you wish to terminate: http://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx

ExitProcess(...) to close the process: http://msdn.microsoft.com/en-us/library/ms682658(VS.85).aspx

CreateProcess(...) to open a new instance of the process: http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx

Really it's much simpler then it seems, there's just a bunch of stuff to do if you want it to work everytime.
May 9, 2011 at 8:36pm
Thanks. I'll try them to see which one works for me the best.
May 9, 2011 at 8:50pm
No no no, you need to use them all to do this right. Sorry friend you don't get to pick and choose.
May 9, 2011 at 9:07pm
So what you're saying is that Webjose's method won't work?
May 9, 2011 at 9:10pm
I replied with this in mind: Lamblion wants to restart its own application.
Computergeek01 replied with this in mind: Lamblion wants to restart some other application currently running.

Now pick. My solution to restart your own app; Computergeek01's to restart another app.
May 9, 2011 at 9:22pm
Ah! That make's sense now. I'll try yours first, as that seems the simplest.
May 9, 2011 at 9:23pm
+1 to webJose, this is exactly what happend. One of the benefits of a public forum is getting multiple answers in case someone misinterperates you.
May 9, 2011 at 9:53pm
Well I appreciate BOTH answers. webJose's method is what I want, but it's good to know the other method too.
May 10, 2011 at 12:44am
This won't work --

1
2
3
4
5
6
7
8
void BOSSRestartBoss()
{
	HWND hTempWnd=hWinMainWnd;
	char szFileName[MAX_PATH]="";

	GetModuleFileName(NULL,szFileName,MAX_PATH);
	ShellExecute(GetDesktopWindow(), szFileName, "OPEN", NULL, NULL, SW_SHOWDEFAULT);
	SendMessage(hTempWnd,WM_CLOSE,0,0);


The program closes alright, but it won't restart.
May 10, 2011 at 2:12am
Nevermind, it DOES work. In the above code, I had the szFileName and the "open" parameters backward.
Topic archived. No new replies allowed.