限制程序只运行一次

1.使用互斥量来限制程序只运行一次

BOOL CExecInApp::InitInstance()
{

//deny running application  second 
/* AfxEnableControlContainer();

::CreateMutex(NULL, TRUE, m_pszExeName);
    if(ERROR_ALREADY_EXISTS == GetLastError())
    {
try{
::SetForegroundWindow(::FindWindow(NULL, "Main"));
}
catch(...)
{
AfxMessageBox("Application Error!Please close it!");
}
return false;
}
*/
AfxEnableControlContainer();

::CreateMutex(NULL, TRUE, m_pszExeName);
if(ERROR_ALREADY_EXISTS == GetLastError())
{
       CWnd* pPrevHwnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
//CWnd* pPrevHwnd = CWnd::FindWindow(NULL,m_pszExeName);
//AfxMessageBox(m_pszExeName);
       while(pPrevHwnd)
        {
//AfxMessageBox("4");

           if(::GetProp(pPrevHwnd->m_hWnd , m_pszExeName))
           {
AfxMessageBox("3");
                if(pPrevHwnd->IsIconic())
                {
AfxMessageBox("3");
                    pPrevHwnd->ShowWindow(SW_RESTORE);
                }
//pPrevHwnd->ShowWindow(SW_RESTORE);
               pPrevHwnd->SetForegroundWindow();
               pPrevHwnd->GetLastActivePopup()->SetForegroundWindow();
                return FALSE;
            }
            pPrevHwnd = pPrevHwnd->GetWindow(GW_HWNDNEXT);
        }
        TRACE("Could not fond frevious instance main window !");
        return FALSE;
}
::CreateMutex(NULL, TRUE, m_pszExeName); 创建一个互坼量

2.使用信号量来限制程序只运行一次 

BOOL CTestApp::InitInstance()
{
 
 // 用应用程序名创建信号量
 hSem = CreateSemaphore(NULL, 1, 1, m_pszExeName);
 
 // 信号量已存在?
 // 信号量存在,则程序已有一个实例运行
 if (GetLastError() == ERROR_ALREADY_EXISTS)
 {
  // 关闭信号量句柄
  CloseHandle(hSem);
  // 寻找先前实例的主窗口
  HWND hWndPrevious = ::GetWindow(::GetDesktopWindow(),GW_CHILD);
  while (::IsWindow(hWndPrevious))
  {
   // 检查窗口是否有预设的标记?
   // 有,则是我们寻找的主窗
   if (::GetProp(hWndPrevious, m_pszExeName))
   {
    // 主窗口已最小化,则恢复其大小
    // if (::IsIconic(hWndPrevious))
    ::ShowWindow(hWndPrevious,SW_RESTORE);
    
    // 将主窗激活
    ::SetForegroundWindow(hWndPrevious);
    
    // 将主窗的对话框激活
    ::SetForegroundWindow(::GetLastActivePopup(hWndPrevious));
    
    // 退出本实例
    return FALSE;
   }
   // 继续寻找下一个窗口
   hWndPrevious = ::GetWindow(hWndPrevious,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("Local AppWizard-Generated Applications"));

 LoadStdProfileSettings();  // 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(CTestDoc),
  RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  RUNTIME_CLASS(CTestView));
 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();

 return TRUE;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值