| 在c++程序中重启自己的一种方法 |
|
| 作者:YesHack 文章来源:YesHack.Com 更新时间:2006-9-20 22:40:17
【字体:小 大】 |
在工程.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(); }
|
友情提示:如果您对本文章的内容存在疑问请到点此进入论坛进行讨论
|
|
| 教程录入:YesHack 责任编辑:YesHack |
|
|
上一个教程: 踏入C++中的雷区——C++内存管理详解
下一个教程: C/C++ 程序设计员应聘常见面试试题深入剖析 |
| |