This document is the easist way to check for multiple instances and bring the first instance on to the focus.//Step 1 ***********************************
//Top of the App .cpp file
#pragma comment(linker, "/SECTION:.shr,RWS")
#pragma data_seg(".shr")
HWND g_hWnd = NULL;
#pragma data_seg()
以上每一句各实现什么功能?有何作用?
//Step 2 ***********************************
//On the App::InitInstance() add the code below:
//For single instance checking
HANDLE hMutexOneInstance = ::CreateMutex( NULL, TRUE, _T("AFX_Ideas_H__5B5F33E8_2175_4C23_BB38_A334CADD6B88")); 
bool AlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS); 
if (hMutexOneInstance != NULL) ::ReleaseMutex(hMutexOneInstance); 
if (AlreadyRunning)
{
   if (g_hWnd) /* pop up */
   { 
      ::SetForegroundWindow(g_hWnd);
      if (IsIconic(g_hWnd)) ::ShowWindow(g_hWnd, SW_RESTORE);
   }
   return FALSE; // terminates the creation
}
创建mutex是为了?
//Step 3 ***********************************
//After Creating the Main Frame, get the handle and store it on the shared variable below.g_hWnd = m_pMainWnd->m_hWnd;

解决方案 »

  1.   

    #pragma comment(linker, "/SECTION:.shr,RWS")           
    #pragma data_seg(".shr")
    HWND g_hWnd = NULL;
    #pragma data_seg()1. 设置节.shr为数据段并且为可读,可写,共享。2.创建mutex是为了,不重复运行,
    AlreadyRunning表示是否已经运行了,AlreadyRunning=TRUE
    SetForegroundWindow,ShowWindow(激活)return3 还没有运行。
      

  2.   

    1 设置节.shr为数据段并且为可读,可写,共享。
    这样做有什么作用么?2就是说CreateMutex就可以实现不让程序重复运行?不好意思可能问的问题比较傻
      

  3.   

    #pragma comment(linker, "/SECTION:.shr,RWS")
    #pragma data_seg(".shr")
    HWND g_hWnd = NULL;
    #pragma data_seg()
    //创建一个进程共享数据段,可以供所有进程访问的
    创建mutex是为了?
    为了防止多个进程同时访问上面数据段时发生冲突,建立一个多进程(线程)互斥对象,表示同一时刻只能有一个进程(线程)读写上面的数据段!