Background:
In the development of the game program, often encounter this situation, run the game update program need to restart the update, in-game changes in the video and other related settings need to restart the game program, how to do this?
Solution:
One solution is to wait to start a new program, but this implementation in the extreme case of a problem, assuming that a game program on a physical machine only allow to start a process, if the shutdown of the old process for some reason caused by delay, then start a new process will fail, imagine the game update after the end , which prompts the player to restart the game, and the game does not restart, this experience is pretty bad.
Another solution, in order to ensure that program a closed and then open with program B, in the Windows environment, there is a very common method-batch processing, because the methods in the batch is executed sequentially, close program a open program B to ensure synchronous execution, the killing process and the ability to start the process to write batch processing, Where a restart is required, the call API executes the batch.
/*author:coffeecatbrief:all the code below has been tested.date:2014/10/21*/#include "stdafx.h" #include <IOSTREAM&G T;using namespace Std;void Restart () {file* pf;::D eletefile (L "Restart.cmd");//Make sure to delete the last left batch file errno_t Err =:: _wfopen_s (&PF, L "Restart.cmd", L "w");//"W" means open an empty file for writing. If the file exists, its contents will be destroyed. if (err = = 0)//restart.cmd does not exist, err = = 0{char szexename[1024]; GetModuleFileNameA (NULL, Szexename, 1024);//Gets the name of the process fprintf (PF, "@echo off\n:a\n taskkill/f/im restart.exe \ n" \ "\"%s\ "\ndel%%0", szexename); Restart.exe is the name of the process that needs to be restarted//taskkill/f (specifies the process to be forcibly terminated)/im (image name can be understood as specifying the name of the process name to terminate)//%%0 represents%0,del%%0 in a batch (delete batch itself) Fclose (PF); } else {cout << "can not open restart.cmd" << Endl; } startupinfo si; Process_information Pi; ZeroMemory (&si, sizeof si); ZeroMemory (&pi, sizeof pi); SI.CB = sizeof Si; Si.dwflags = Startf_useshowwindow; Si.wshowwindow = Sw_hide; TCHAR winsysdir[1024]; ZeroMemory (winsysdir, sizeof WINSYSDIR); :: GetSystemDirectory (Winsysdir, 1024); std::wstring appName = Winsysdir; AppName + = L "\\cmd.exe"; BOOL BRet = CreateProcess (Appname.c_str (), L "/C Restart.cmd",//execute string specified command and then eventually break null, NULL, FALSE, 0, NULL, NULL, &A Mp;si, &PI); if (BRet = = FALSE) {int err = GetLastError (); cout << "Can not restart test, error code:" << err << Endl; }} int main () {int input, curpid;curpid = GetCurrentProcessId (); cout << "Current process Id:" << curpid <& Lt Endl;cout << need to restart? (Input 1 for restart, else does not restart) "<< endl;cin >> input;if (input = = 1) {restart ();} System ("pause"); return 0;}
Executing the above code, entering 1 closes the current process and re-opens a new process.
Operation Result:
The following is process a, which is closed when 1 is entered.
The following is process B, and process A is closed and process B is created.
You can see that the PID of the two processes is different and fits the expectations.
How to implement a program restart (in Windows environment)