Initialize(); Application->CreateForm(__classid(TfrmMain), Application->Run(); } catch (Exception excepti" />
龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

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

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
在工程.cpp文件(Project1.cpp)中加入: #include "Unit1.h" WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { try { Application->Initialize(); Application->CreateForm(__classid(TfrmMain), Application->Run(); } catch (Exception excepti

  在工程.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();

  }

  

  

精彩图集

赞助商链接