重启程序代码如下:
PROCESS_INFORMATION info;
STARTUPINFO startup;
char szPath[128];
char *szCmdLine;
GetModuleFileName(AfxGetApp()->m_hInstance, szPath, sizeof(szPath));
szCmdLine = GetCommandLine();
GetStartupInfo(&startup);
BOOL bSucc = CreateProcess(szPath, szCmdLine, NULL, NULL,
FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startup, &info);
if(TRUE && bSucc)
{
CWnd *pWnd = AfxGetMainWnd();
if(pWnd != NULL)
{
// WaitForSingleObject(info.hProcess, INFINITE);
// CloseHandle(info.hProcess);
// CloseHandle(info.hThread);
CMainFrame::OnClose();
// pWnd->PostMessage(WM_CLOSE, 0, 0);
}
else
ExitProcess(-1);
}
else
ExitProcess(-1);
关闭程序用PostMessage或SendMeaasge发送WM_CLOSE给指定程序即可关闭该程序。
http://hi.baidu.com/wshmouse2003/blog/item/178abcf5e5da4626bc3109af.html
在很多情况下,我们需要将当前运行的程序重新运行;此时我们就必须在程序关闭后立即重新运行程序,形成新的进程;
实例代码:
1、重启按钮事件中添加代码:
g_bIsRunAgain=true;//控制是否重新运行的变量
this->SendMessage(WM_CLOSE);
2、在OnClose()消息处理中加入代码:
if(g_bIsRunAgain)
{
char pBuf[MAX_PATH]; //存放路径的变量
GetCurrentDirectory(MAX_PATH,pBuf); //获取程序的当前目录
strcat(pBuf,"");
strcat(pBuf,AfxGetApp()->m_pszExeName);
strcat(pBuf,".exe");
CString strPath= (CString) pBuf;
STARTUPINFO StartInfo;
PROCESS_INFORMATION procStruct;
memset(&StartInfo, 0, sizeof(STARTUPINFO));
StartInfo.cb = sizeof(STARTUPINFO);
::CreateProcess(
(LPCTSTR) strPath,
NULL,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&StartInfo,
&procStruct);
}
CDialog::OnClose();
http://school.cnd8.com/vc/jiaocheng/28665.htm