/******************************************************************************
Copyright (C) 2005 Matteo Lucarelli

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
******************************************************************************/

#include "stdafx.h"
#include "wInstanceControl.h"

bool wInstanceControl::lock(LPCTSTR id)
{
	if (mh_Mutex) return false;
	mh_Mutex=CreateMutex(NULL,TRUE,id);

	switch (GetLastError()){
		case ERROR_SUCCESS:
			return true;
		case ERROR_ALREADY_EXISTS:
			ReleaseMutex(mh_Mutex);
			CloseHandle(mh_Mutex);
			mh_Mutex=NULL;
			return false;
		default:
			return false;
	}
}

bool wInstanceControl::free()
{
	if (!mh_Mutex) return false;

	ReleaseMutex(mh_Mutex);
	CloseHandle(mh_Mutex);
	mh_Mutex=NULL;

	return true;
}

bool wInstanceControl::isfree(LPCTSTR id)
{
	HANDLE  hMutex=CreateMutex(NULL,TRUE,id);

	if (GetLastError()==ERROR_SUCCESS){
			ReleaseMutex(hMutex);
			CloseHandle(hMutex);
			return true;
	}
	return false;
}