原创 阻止程序多次运行的方法

2008-10-20 19:58 3694 4 4 分类: 软件与OS

BOOL CAddressAssistApp::InitInstance()
{
 hMutex = CreateMutex(NULL, FALSE, "端口助手");
 if (GetLastError() == ERROR_ALREADY_EXISTS)
 {
  AfxMessageBox("对不起,已有一程序在运行!", MB_OK);
  return FALSE;
 }
 IsInitializeWinIo = InitializeWinIo();
 if(!IsInitializeWinIo)
 {
  AfxMessageBox("加载Winio驱动错误!");
  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


 CAddressAssistDlg dlg;
 m_pMainWnd = &dlg;
 int nResponse = dlg.DoModal();
 if (nResponse == IDOK)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with OK
 }
 else if (nResponse == IDCANCEL)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with Cancel
 }


 // Since the dialog has been closed, return FALSE so that we exit the
 //  application, rather than start the application's message pump.
 return FALSE;
}


int CAddressAssistApp::ExitInstance()
{
 // TODO: Add your specialized code here and/or call the base class
 ReleaseMutex(hMutex);
 if(IsInitializeWinIo)
 {  
  ShutdownWinIo();
 }
 
 return CWinApp::ExitInstance();
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
4
关闭 站长推荐上一条 /3 下一条