Add the following to the project. cpp file (Project1.cpp:
# Include "Unit1.h"
WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
Try
{
Application-> Initialize ();
Application-> CreateForm (_ classid (TfrmMain), & frmMain );
Application-> Run ();
}
Catch (Exception & exception)
{
Application-> ShowException (& exception );
}
Catch (...)
{
Try
{
Throw Exception ("");
}
Catch (Exception & exception)
{
Application-> ShowException (& exception );
}
}
If (g_bIsRunAgain)
{
AnsiString strPath;
STARTUPINFO StartInfo;
PROCESS_INFORMATION procStruct;
Memset (& StartInfo, 0, sizeof (STARTUPINFO ));
StartInfo. cb = sizeof (STARTUPINFO );
StrPath = Application-> ExeName;
If (! : CreateProcess (
(LPCTSTR) strPath. c_str (),
NULL,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
& StartInfo,
& ProcStruct ))
Return 0;
}
Return 0;
}
Add the following to the unit header file (Unit1.h) in the main window:
Extern bool g_bIsRunAgain;
In the unit. cpp (Unit1.cpp) of the main window, add:
Bool g_bIsRunAgain = false;
//----------------------------------------------------------------------------
// Close the program
Void _ fastcall TfrmMain: btnCloseClick (TObject * Sender)
{
Close ();
}
//----------------------------------------------------------------------------
// Restart the application
Void _ fastcall TfrmMain: btnReExcuteClick (TObject * Sender)
{
G_bIsRunAgain = true;
Close ();
}