单例控制

来源:互联网 发布:网店美工招聘 编辑:程序博客网 时间:2024/06/06 09:07
#ifndef LimitSingleInstance_H#define LimitSingleInstance_H#include <windows.h> //This code is from Q243953 in case you lose the article and wonder//where this code came from.class CLimitSingleInstance{protected:  DWORD  m_dwLastError;  HANDLE m_hMutex;public:  CLimitSingleInstance(TCHAR *strMutexName)  {    //Make sure that you 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)  //Do not 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


1.mutex用于单例,添加"Global\"是否存在的应用程序在一台计算机上只有一个实例将保证。这可应用于终端服务正在运行或不。

 参照:http://support.microsoft.com/kb/243953/zh-cn

原创粉丝点击