设为首页 加入收藏

TOP

在c++程序中重启自己的一种方法
2014-11-23 21:27:46 来源: 作者: 【 】 浏览:16
Tags:程序 重启 自己 方法
在工程.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 __fastcall TfrmMain::btnReExcuteClick(TObject *Sender)
{
g_bIsRunAgain = true;
Close();
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用GDI+实现半透明渐变的特效窗口 下一篇编程秘籍篇:C语言高效编程的四大..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: