在yourApp::InitInstance()中创建一个Mutex。首次运行时会成功,以后再运行就会失败,然后退出程序。

解决方案 »

  1.   

    http://www.csdn.net/expert/topic/357/357445.shtm
      

  2.   

    如果你的程序的title不会变,那最简单的是findwindow()
      

  3.   

    // 检测是否是第一事例
    CreateMutex(NULL,TRUE,"我的应用程序代号");
    if(ERROR_ALREADY_EXISTS==GetLastError()) 
    {
             // 已经运行了一个进程
      

  4.   

    好想有个findwindow 函数你自己看一下
      

  5.   

    BOOL CxxApp::InitInstance()
    {
        CreateMutex(NULL,TRUE,AfxGetAppName());
        if(GetLastError()==ERROR_ALREADY_EXISTS)
        {
            AfxMessageBox("程序已经运行");    //或不写
            return FALSE;
        }
    ....
    }方案一: 查找窗口类 
    方案二: 设置窗口属性 
    方案三: 使用文件映像 
    方案四: 使用原子 
    方案五: 使用同步对象 
    方案一: 使用注册类查找的方法实现单实例运行 
    例: 
    ①C???App::InitInstance 
    BOOL C???App::InitInstance()
    {
        if (!FirstInstance())
          return FALSE;
        beRegisted=TRUE;
      m_pMainWnd =new CMainWnd();
        m_pMainWnd->CreateEx(0,"fmdmainwnd",
                _T("CodeHelper"),WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,NULL);
        m_pMainWnd->ShowWindow(SW_SHOW);
        m_pMainWnd->UpdateWindow();
        return TRUE;
    }
    其中beRegisted为C???App成员 
    public:
        BOOL beRegisted;
    ②C???App::FirstInstance 
    BOOL CMainApp::FirstInstance()
    {
        CWnd *PrevCWnd, *ChildCWnd;
        if (PrevCWnd = CWnd::FindWindow("fmdmainwnd",NULL))
        {
          ChildCWnd=PrevCWnd->GetLastActivePopup();
          PrevCWnd->BringWindowToTop();
          if (PrevCWnd->IsIconic())
              PrevCWnd->ShowWindow(SW_RESTORE);
          if (PrevCWnd != ChildCWnd)
              ChildCWnd->BringWindowToTop();
          return FALSE;
        }
        else
          return TRUE;  // First instance. Proceed as normal.
    }
    ③C???App::InitApplication 
    BOOL CMainApp::InitApplication() 
    {
        CWinApp::InitApplication();
        WNDCLASS wndcls;
        //注册自定义类
          memset(&wndcls, 0, sizeof(WNDCLASS));
        //窗口基本类型
          wndcls.style=CS_DBLCLKS ¦ CS_HREDRAW ¦ CS_VREDRAW;
        wndcls.lpfnWndProc=::DefWindowProc;
          wndcls.hInstance=AfxGetInstanceHandle();
          wndcls.hIcon=LoadIcon(IDR_MAIN);
          wndcls.hCursor=LoadStandardCursor(IDC_ARROW);
          wndcls.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
          wndcls.lpszMenuName=MAKEINTRESOURCE(IDR_MAIN);
          wndcls.lpszClassName="fmdmainwnd";
        return AfxRegisterClass(&wndcls);
    }
    ④C???App::ExitInstance 
    int CMainApp::ExitInstance() 
    {
        if(beRegisted)
            ::UnregisterClass("fmdmainwnd", AfxGetInstanceHandle());
        return CWinApp::ExitInstance();
    }方案二:设置窗口附加属性 int C???Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
        ......
        ::SetProp(m_hWnd, AfxGetApp()->m_pszExeName, (HANDLE)1);
        ......
    }
    void C???Dlg::OnDestroy() 
    {
        ......
        ::RemoveProp(m_hWnd, AfxGetApp()->m_pszExeName);    
        ......
    }
    BOOL CEllipseWndApp::InitInstance()
    {
        // Create a Semaphore with a name of application exename
        HANDLE 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;
        }
        C????Dlg dlg;
        m_pMainWnd = &dlg;
        int nResponse = dlg.DoModal();
        if (nResponse == IDOK)
        {
            // TODO: Place code here to handle when the dialog is
        }
        else if (nResponse == IDCANCEL)
        {
            // TODO: Place code here to handle when the dialog is
        }
        return FALSE;
    }方案三:使用文件映像 
    另文有述方案四:使用原子 
    BOOL C???App::InitInstance()
    {
        .....
        if(GlobalFindAtom("AdBreaker"))  //找原子
            return false;
        ATOM GlobalAtom=GlobalAddAtom("AdBreaker"); //添加原子
        CAdBreakerDlg dlg;
        m_pMainWnd = &dlg;
        int nResponse = dlg.DoModal();
        .....    
        GlobalDeleteAtom(GlobalAtom);
        return FALSE;
    }方案五:使用同步对象 InitInstance中 
        HANDLE m_hMutex = 
        CreateMutex(NULL,TRUE,m_pszAppName);
        if (GetLastError()==ERROR_ALREADY_EXISTS){
            return FALSE;
        }另:在
    http://www.csdn.net/expert/topic/159/159112.shtm
    中liuqiannan(二迷糊)    的方案也不错:回复人:liuqiannan(二迷糊)    (2001-6-14 22:28:00)  得0分 
    用一个共享数据段,定义一个用来计数的长整形
    #pragma data_seg(".OnlyOne")
        long 1Count=-1;
    #pragma data_seg()用DEF文件定义共享属性
    SECTIONS
        .OnlyOne READ WRITE SHARED在初始化阶段,如InitInstance开始处,加入
    BOOL bFirstInit=(InterlockedIncrement($1InstanceCount)==0)
    if(!bFirstInit)
    {
          AfxMessageBox("该程序已经运行",MB_OK¦MB_ICONSTOP);
          InterlockedDecrement($1InstanceCount);
          return FALSE;
    }
    程序结束后,调用:
    InterlockedDecrement(&1InstanceCount);