高手求解,程序重启问题
我的程序设计中要设计一个用户注销功能(当前用户退出,再重新用一个用户进行登录)登录的代码是写在WinMain里,该如何做呢?我的想法点一个按钮是退出当前程序再次运行当前程序,并显示登录界面.谢谢
代码如下:
//---------------------------------------
#include <vcl.h>
#include "UnitLink.h "
#include "UnitLogin.h "
#include "UnitPublic.h "
#pragma hdrstop
//---------------------------------------
USEFORM( "win\UnitAbout.cpp ", FrmAbout);
USEFORM( "win\UnitAddCard.cpp ", FrmAddCard);
USEFORM( "win\UnitAddParkingSpace.cpp ", FrmAddParkingSpace);
USEFORM( "win\UnitAmendPass.cpp ", FrmAmendPassword);
USEFORM( "win\UnitCardCondition.cpp ", FrmCardCondition);
USEFORM( "win\UnitCardManage.cpp ", FrmCardManage);
USEFORM( "win\UnitDM.cpp ", DM); /* TDataModule: File Type */
USEFORM( "win\UnitLink.cpp ", FrmLink);
USEFORM( "win\UnitLogin.cpp ", FrmLogin);
USEFORM( "win\UnitMain.cpp ", FrmMain);
USEFORM( "win\UnitParkingSpace.cpp ", FrmParkingSpace);
USEFORM( "win\UnitParkingSpaceCondition.cpp ", FrmParkingSpaceCondition);
USEFORM( "win\UnitSelectDoorPos.cpp ", FrmSelectDoorPos);
USEFORM( "win\UnitSelectParkingSpace.cpp ", FrmSelectParkingSpace);
USEFORM( "win\UnitSystemSetting.cpp ", FrmSystemSetting);
USEFORM( "Report\UnitOPLogCondition.cpp ", FrmOPLogCondition);
USEFORM( "Report\UnitOpLogReport.cpp ", FrmOpLogReport);
USEFORM( "win\UnitWait.cpp ", FrmWait);
//---------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
HANDLE mutex;
const char mutexname[]= "ManageSystem.exe ";
mutex = OpenMutex(MUTEX_ALL_ACCESS, false, mutexname);
if (mutex == NULL)
{
mutex = CreateMutex(NULL, true, mutexname);
}
else
{
Application-> MessageBoxA( "管理系统已运行! ", "提示 ",MB_OK|MB_ICONINFORMATION);
return(FALSE);
}
Application-> Initialize();
Application-> Title = ReadConfigString( "SystemInfo ", "SystemName ");
WUserPurviewFull = new TUserPurviewFull(Application,GetConnectionStr());
TFrmLink *FrmLink = new TFrmLink(Application,WUserPurviewFull); // 测试连接数据是否成功的界面
FrmLink-> ShowModal();
if(FrmLink-> LinkResult == 1)
{
Application-> Terminate();
delete WUserPurviewFull;
delete FrmLink;
return 0;
}
delete FrmLink;
FrmLogin = new TFrmLogin(Application,WUserPurviewFull);//登录界面,要求输入用户名密码
FrmLogin-> InsertUserName();
FrmLogin-> ShowModal();
if (FrmLogin-> ReturnState == 1)
{
delete FrmLogin;
delete WUserPurviewFull;
return 0;
}
else
{
delete FrmLogin;
}
Application-> CreateForm(__classid(TDM), &DM);
Application-> CreateForm(__classid(TFrmMain), &FrmMain);
Application-> CreateForm(__classid(TFrmCardCondition), &FrmCardCondition);
Application-> CreateForm(__classid(TFrmParkingSpaceCondition), &FrmParkingSpaceCondition);
Application-> CreateForm(__classid(TFrmSelectDoorPos), &FrmSelectDoorPos);
Application-> CreateForm(__classid(TFrmSelectParkingSpace), &FrmSelectParkingSpace);
Application-> CreateForm(__classid(TFrmOPLogCondition), &FrmOPLogCondition);
Application-> Run();
}
catch (Exception &exception)
{
Application-> ShowException(&exception);
}
catch (...)
{
try
{
throw Exception( " ");
}
catch (Exception &exception)
{
Application-> ShowException(&exception);
}
}
delete WUserPurviewFull;
return 0;
}
//------------------------------------
[解决办法]
想法可行,加10分!
[解决办法]
在WinMain return前,做个判断,如果需要重启,就执行以下代码:
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
/*
........
........
*/
if(需要重启)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
if(!CreateProcess((LPCTSTR)(Application-> ExeName.c_str()),NULL,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
{
return 0;
}
}
return 0;
}
[解决办法]
何必要重新启动?
象XP那样的注销不就可以了?
如果要重启
在工程.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);
}
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=641&d=283gya
}
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();
}
[解决办法]
设计成按钮不按钮有什么关系啊?
我给你写的if的判断重启的条件,你设置一个全局变量不就好了吗?
就好像楼上的兄弟所贴的那代码的意思
[解决办法]
mark