singleinstance.cpp

来自「一个在visualC++开发的密码探测器很有研究和参考价值好东西和大家分享」· C++ 代码 · 共 43 行

CPP
43
字号
#include "stdafx.h"
#include "SingleInstance.h"

//***********************************************
CSingleInstance::CSingleInstance() : m_hMutex(NULL)
{
}

//***********************************************
CSingleInstance::~CSingleInstance()
{
	if(m_hMutex != NULL)
	{
		ReleaseMutex(m_hMutex);
		CloseHandle(m_hMutex);
	}
}

//***********************************************
bool CSingleInstance::Create(LPCTSTR szMutexName)
{
	_ASSERTE(szMutexName);
	_ASSERTE(lstrlen(szMutexName));

	bool bSuccess = false;

	try
	{
		// First get the handle to the mutex
		m_hMutex = CreateMutex(NULL, FALSE, szMutexName);
		if(m_hMutex != NULL)
		{
			// Test the state of the mutex
			// If the state is signaled, we successfully opened the mutex
			if(WaitForSingleObject(m_hMutex, 0) == WAIT_OBJECT_0)
				bSuccess = true;
		}
	}
	catch(...) {}

	return bSuccess;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?