检查是否进程已经运行。
hImHere = CreateMutex(NULL, TRUE, "DialersIveBeenStartedMutex"); 
 
// 
// Is there another one of us already here? 
if ( ERROR_ALREADY_EXISTS == GetLastError() ) 

        HWND        hDialerWnd; 
 
        hDialerWnd = FindWindow(gszDialerClassName, 
                                NULL); 
 
        SetForegroundWindow(hDialerWnd); 
         
   CloseHandle( hImHere ); 
   return 0; 

解决方案 »

  1.   

    ////检查程序是否已运行/////////////////////////////////
    const CString CLASS_NAME="MyProgram";

    HANDLE hMutex=CreateMutex(NULL,FALSE,CLASS_NAME); if(hMutex==NULL)return FALSE; if(GetLastError() == ERROR_ALREADY_EXISTS )
    {
    hMutex=NULL;
    CString strWindow;
    strWindow.LoadString(IDR_MAINFRAME);
    strWindow=strWindow.Left(strWindow.Find("\n",0));
    HWND hWnd = FindWindowEx( NULL, NULL,NULL,strWindow); if ( hWnd != NULL ) {
    ShowWindow(hWnd,SW_SHOW);
    ShowWindow(hWnd,SW_RESTORE);
    BringWindowToTop(hWnd);
    SetForegroundWindow(hWnd);
    }
    return FALSE;
    }
      

  2.   

    在CYourApp::InitInstance()中:HWND hWnd = ::FindWindow(NULL,"你那程序窗口的标题");
    if(hWnd != NULL)
    {
        ::ShowWindow(hWnd, SW_SHOW);
        return FALSE;
    }
      

  3.   

    这样:
    在CYourApp::InitInstance()中:HWND hWnd = ::FindWindow(NULL,"你那程序窗口的标题");
    if(hWnd != NULL)
    {
        ::SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSZIE | SWP_SHOWWINDOW);
        return FALSE;
    }
      

  4.   

    #ifndef LimitSingleInstance_H
    #define LimitSingleInstance_H#include <windows.h> class CLimitSingleInstance
    {
    protected:
      DWORD  m_dwLastError;
      HANDLE m_hMutex;public:
      CLimitSingleInstance(TCHAR *strMutexName)
      {
        //be sure to use a name that is unique for this application otherwise
        //two apps may think they are the same if they are using same name for
        //3rd parm to CreateMutex
        m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
        m_dwLastError = GetLastError(); //save for use later...
      }
       
      ~CLimitSingleInstance() 
      {
        if (m_hMutex)  //don't forget to close handles...
        {
           CloseHandle(m_hMutex); //do as late as possible
           m_hMutex = NULL; //good habit to be in
        }
      }  BOOL IsAnotherInstanceRunning() 
      {
        return (ERROR_ALREADY_EXISTS == m_dwLastError);
      }
    };
    #endif 添加一全局变量:
    CLimitSingleInstance g_singleInstane;if (g_singleInstane.IsAnotherInstanceRunning()) {
        // ref : AloneWolf or xstnt
        ...
    }
      

  5.   

    用FindWindow不好,容易出错。最好是用互斥量CreateMutex
      

  6.   

    应是:
    CLimitSingleInstance g_singleInstane("{{A6B13EE4-A974-11d2-8DB7-00C04FB6E8F6}"}");
      

  7.   

    查一下CWinApp::m_hPrewInstance如果为NULL则说明是第一个实例,否则已经运行。
      

  8.   

    HANDLE      SystemCsdsMutex;
    BOOL CCsdsApp::InitInstance()
    ///////////////////////////设置只能运行一个实例
    {
    SystemCsdsMutex = ::CreateMutex(NULL,TRUE,"LYCHPCSDSMutex");
    if(GetLastError() == ERROR_ALREADY_EXISTS)  {
    CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
    while(pPrevWnd) {
    if(::GetProp(pPrevWnd->GetSafeHwnd(),"LYCHPCSDSMutex")) {
    if(pPrevWnd->IsIconic()) pPrevWnd->ShowWindow(SW_RESTORE);
    pPrevWnd->SetForegroundWindow();
    pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
    return FALSE;
    }
    pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
    }
    ::CloseHandle(SystemCsdsMutex);
    return FALSE;
    }
      

  9.   

    最好的办法是用全局原子方法如下:
    int WINAPI WinMain(.......)
    {
             ........
             ........
             Atom atom;
            //确保程序只被运行一次
    if(GlobalFindAtom("PROGRAM_RUNNING") == 0)
    {
                      //说明程序还没运行
    atom = GlobalAddAto("PROGRAM_RUNNING");
    }
    else
    {
             //说明程序已经运行,则这一次的实例就应该立即退出。
    return 0;
    }
        ..................
            .................
             return 0;
    }