在c++程序中重启自己的一种方法

转自:http://www.ieehoo.com/Tech/167/View_109863.html#top

 

==============================================

 

在工程.cpp文件(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;
}


主窗口的单元头文件(Unit1.h)中加入:
extern bool g_bIsRunAgain;


主窗口的单元.cpp(Unit1.cpp)中加入:
bool g_bIsRunAgain = false;
//----------------------------------------------------------------------------
// 关闭程序
void __fastcall TfrmMain::BTnCloseClick(TObject *Sender)
{
Close();
}
//----------------------------------------------------------------------------
// 重启应用程序
void __fa

stcall TfrmMain::btnReExcuteClick(TObject *Sender)
{
g_bIsRunAgain = true;
Close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值