那么用什么方法防止一个程序运行多个实例?请指点,谢谢 。
3 个解决方案
#1
方法很多啊
随便提两个, 1. 用findwindow查找是否有你的窗口存在
2. 程序一开始建立一个内核对象(命名的),如果对象已经存在了, 那么说明已经...soso
随便提两个, 1. 用findwindow查找是否有你的窗口存在
2. 程序一开始建立一个内核对象(命名的),如果对象已经存在了, 那么说明已经...soso
#2
根据下面例子修改!
BOOL CGas2BankApp::InitInstance()
{
//确保只有一个应用程序实例
m_hMutex = ::CreateMutex(NULL,TRUE,m_pszExeName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
TRACE0("已有实例运行 , 调出上一实例\n");
//查找前一实例的主窗口
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while(pPrevWnd)
{
if(::GetProp(pPrevWnd->GetSafeHwnd(),m_pszExeName))
{
if(pPrevWnd->IsIconic())
pPrevWnd->ShowWindow(SW_RESTORE);
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Gas2Bank"));
LoadStdProfileSettings(0); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CGas2BankDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CGas2BankView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
//设置属性
::SetProp(m_pMainWnd->GetSafeHwnd(),m_pszExeName,(HANDLE)1);
return TRUE;
}
void CMainFrame::OnDestroy()
{
::RemoveProp(GetSafeHwnd(),AfxGetApp()->m_pszExeName);
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
}
BOOL CGas2BankApp::InitInstance()
{
//确保只有一个应用程序实例
m_hMutex = ::CreateMutex(NULL,TRUE,m_pszExeName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
TRACE0("已有实例运行 , 调出上一实例\n");
//查找前一实例的主窗口
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while(pPrevWnd)
{
if(::GetProp(pPrevWnd->GetSafeHwnd(),m_pszExeName))
{
if(pPrevWnd->IsIconic())
pPrevWnd->ShowWindow(SW_RESTORE);
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Gas2Bank"));
LoadStdProfileSettings(0); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CGas2BankDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CGas2BankView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
//设置属性
::SetProp(m_pMainWnd->GetSafeHwnd(),m_pszExeName,(HANDLE)1);
return TRUE;
}
void CMainFrame::OnDestroy()
{
::RemoveProp(GetSafeHwnd(),AfxGetApp()->m_pszExeName);
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
}
#3
谢谢。
#1
方法很多啊
随便提两个, 1. 用findwindow查找是否有你的窗口存在
2. 程序一开始建立一个内核对象(命名的),如果对象已经存在了, 那么说明已经...soso
随便提两个, 1. 用findwindow查找是否有你的窗口存在
2. 程序一开始建立一个内核对象(命名的),如果对象已经存在了, 那么说明已经...soso
#2
根据下面例子修改!
BOOL CGas2BankApp::InitInstance()
{
//确保只有一个应用程序实例
m_hMutex = ::CreateMutex(NULL,TRUE,m_pszExeName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
TRACE0("已有实例运行 , 调出上一实例\n");
//查找前一实例的主窗口
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while(pPrevWnd)
{
if(::GetProp(pPrevWnd->GetSafeHwnd(),m_pszExeName))
{
if(pPrevWnd->IsIconic())
pPrevWnd->ShowWindow(SW_RESTORE);
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Gas2Bank"));
LoadStdProfileSettings(0); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CGas2BankDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CGas2BankView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
//设置属性
::SetProp(m_pMainWnd->GetSafeHwnd(),m_pszExeName,(HANDLE)1);
return TRUE;
}
void CMainFrame::OnDestroy()
{
::RemoveProp(GetSafeHwnd(),AfxGetApp()->m_pszExeName);
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
}
BOOL CGas2BankApp::InitInstance()
{
//确保只有一个应用程序实例
m_hMutex = ::CreateMutex(NULL,TRUE,m_pszExeName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
TRACE0("已有实例运行 , 调出上一实例\n");
//查找前一实例的主窗口
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while(pPrevWnd)
{
if(::GetProp(pPrevWnd->GetSafeHwnd(),m_pszExeName))
{
if(pPrevWnd->IsIconic())
pPrevWnd->ShowWindow(SW_RESTORE);
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Gas2Bank"));
LoadStdProfileSettings(0); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CGas2BankDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CGas2BankView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
//设置属性
::SetProp(m_pMainWnd->GetSafeHwnd(),m_pszExeName,(HANDLE)1);
return TRUE;
}
void CMainFrame::OnDestroy()
{
::RemoveProp(GetSafeHwnd(),AfxGetApp()->m_pszExeName);
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
}
#3
谢谢。