From: http://hi.baidu.com/meakhella/item/e8bfb0de28e5d9e43cc2cb44
Void restart (bool bnormal)
{
Process_information Info;
Startupinfo startup;
Char szpath [128];
Char * szcmdline;
Getmodulefilename (afxgetapp ()-> m_hinstance, szpath, sizeof (szpath ));
Szcmdline = getcommandline ();
Getstartupinfo (& startup );
Bool bsucc = CreateProcess (szpath, szcmdline, null, null,
False, normal_priority_class, null, null, & startup, & info );
If (bnormal & bsucc)
{
Cwnd * pwnd = afxgetmainwnd ();
If (pwnd! = NULL)
{
Pwnd-> postmessage (wm_close, 0, 0 );
}
Else
Exitprocess (-1 );
}
Else
Exitprocess (-1 );
}
VC Program Restart
In many cases, we need to re-run the currently running program. In this case, we must re-run the program immediately after the program is closed to form a new process;
Instance code:
1. Add code to the restart button event:
G_bisrunagain = true; // controls whether to run the variable again.
This-> sendmessage (wm_close );
Add on_wm_close to message_map
2. Add code to onclose () Message Processing:
If (g_bisrunagain)
{
Char pbuf [max_path]; // variable for storing the path
Getcurrentdirectory (max_path, pbuf); // get the current directory of the program
Strcat (pbuf ,"\");
Strcat (pbuf, afxgetapp ()-> m_pszexename );
Strcat (pbuf, ". EXE ");
Cstring strpath = (cstring) pbuf;
Startupinfo startinfo;
Process_information procstruct;
Memset (& startinfo, 0, sizeof (startupinfo ));
Startinfo. cb = sizeof (startupinfo );
: CreateProcess (
(Lpctstr) strpath,
Null,
Null,
Null,
False,
Normal_priority_class,
Null,
Null,
& Startinfo,
& Procstruct );
}
Cdialog: onclose ();
By checking the information, you can restart the dialog box program. The procedure is as follows:
1 showwindow (sw_hide); // hide this dialog box
2 winexec (strpath, sw_show); // start the execution file in the strpath.
3 onok (); // exit the program in the current execution dialog box
In addition, the function getmodulefilename (null, Buf, sizeof (BUF) is used to obtain the path of the execution file of the current program.
Source code:
1 void cf_rockclientdlg: onbtnrestart ()
2 {
3 // todo: add your control notification handler code here
4 char Buf [256];
5: getmodulefilename (null, Buf, sizeof (BUF ));
6 cstring strpath = Buf;
7 showwindow (sw_hide); // hide this dialog box
8 winexec (strpath, sw_show); // start the execution file in the strpath.
9 onok (); // exit the program in the current execution dialog box
10}
VC software restart
Generally, the software is automatically restarted in an event.
For example, in the subdialog box of a dialog box project, I click "OK" to display the function of re-enabling the software.
Subdialog box:
If (MessageBox ("changed settings, need to restart the software to take effect", "notice", mb_yesno) = idyes)
{
// The wm_onsetrestart message is a custom message.
// Implement a Boolean switch to evaluate whether the response to the wm_close message is triggered by this button (otherwise, the software will always restart)
Afxgetmainwnd ()-> sendmessage (wm_onsetrestart );
Afxgetmainwnd ()-> sendmessage (wm_close );
}
In the onclose dialog box:
Void ctransfiledlg: onclose ()
{
Char pbuf [max_path];
// Obtain the full application path, which is much easier to use than getcurrentdirectory
Getmodulefilename (null, pbuf, max_path );
Startupinfo;
Process_information proc_info;
Memset (& startupinfo, 0, sizeof (startupinfo ));
Startupinfo. cb = sizeof (startupinfo );
// The Most Important Part
If (m_bsetrestart)
: CreateProcess (pbuf, null, false,
Normal_priority_class, null, null, & startupinfo, & proc_info );
Cdialog: onclose ();
}