网站首页

家园论坛

老版论坛

家园博客

业界新闻

技术文档

下载中心

速查中心

图片中心

硬件资讯
上一篇:用Winsock实现语音全双工通信 下一篇:BCB6与BCB2006控件的安装
在程序中重启自己的一种方法

来源:http://www.ccrun.com 作者:Rerun 添加日期:2006-1-1 8:10:14 点击次数:
在工程.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();
}
 
设为首页 | 加入收藏 | 业务办理 | 友情链接 | 论坛版面 | 浙ICP备07502118号 |