How to restart a program (in windows) and restart windows
Background:
In the development of game programs, this often happens. After running the game update program, you need to restart the update program. After modifying the video and other related settings in the game, you need to restart the game program, how can this operation be implemented?
Solution:
One solution is to start a new program by waiting, but this implementation method may cause problems in extreme situations, assume that a game program can start only one process on a physical machine at a time. If the old process is disabled for some reason, the new process may fail to be started, after the game update program is completed, it prompts players that the game will be restarted, but the game has not been restarted. This experience is rather bad.
Another solution is to ensure that program A is closed before it is opened with program B. in windows, A very common method is batch processing, because the methods in the batch processing are executed sequentially, disabling program a to open Program B can ensure synchronous execution and write the killing and starting processes functions into the batch processing, you can call the API to execute the batch when you need to restart.
/* Author: coffeecatbrief: All the code below has been tested. date: 2014/10/21 */# include "stdafx. h "# include <iostream> using namespace std; void Restart () {FILE * pf;: DeleteFile (L" restart. cmd "); // ensure that the batch file errno_t err =: _ wfopen_s (& pf, L" restart. cmd ", L" w "); //" w "indicates opening an empty file for writing. If the file exists, its content will be destroyed. If (err = 0) // restart. if cmd does not exist, err = 0 {char szExeName [1024]; GetModuleFileNameA (NULL, szExeName, 1024); // obtain the process name fprintf (pf, "@ echo off \ n: a \ n taskkill/f/im restart.exe \ n start \" % s \ "\ ndel % 0", szExeName ); // restart.exe is the name of the process to be restarted // taskkill/f (specify the process to be forcibly terminated)/im (image name can be understood as the name of the process to be terminated) // % 0 indicates % 0 in batch processing, del % 0 (delete batch processing 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 resta Rt. cmd ", // execute the command specified by the string and then terminate NULL, NULL, FALSE, 0, NULL, NULL, & 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 <endl; cout <"Do you need to restart the program? (Input 1 for restart, else do not restart) "<endl; cin> input; if (input = 1) {Restart ();} system ("pause"); return 0 ;}
Run the preceding code. Enter 1 to close the current process and open a new process.
Running result:
Process A is as follows. It will be disabled after 1 is entered.
Process B will be created after process A is disabled.
We can see that the PID of the two processes is different, as expected.
How can I restart the Windows Shell?
Press Ctrl + Alt + Del to open the Task Manager
Menu: file-new task
For Win 2000, enter c: \ windows \ assumer.exe.
For Win 2000, enter c: \ winnt \ assumer.exe.
OK.
If not, enter:
For Win 2000, enter: c: \ windows \ system32 \ dllcache \ assumer.exe
For Win 2000, enter: c: \ winnt \ system32 \ dllcache \ assumer.exe
OK.
How to restart windows Shell
When the task manager fails, the assumer.exe process is deleted. In the Task Manager menu file-run
Enter explorer
You can restart the shell. For this reason, the external shell program of Windows uses javaser.exe