//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   Instance.cpp
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:

#include "StdAfx.h"
#include "Instance.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// CInstance class implementation.

CInstance::CInstance( void )
{
    m_hInstanceMutex = NULL;
    m_bFirstInstance = true;
}

CInstance::CInstance( LPCTSTR lpszUniqueName )
{
    m_hInstanceMutex = NULL;
    m_bFirstInstance = false;

    if ( lpszUniqueName != NULL )
    {
        if ( ( m_hInstanceMutex = ::CreateMutex( NULL, FALSE, lpszUniqueName ) ) != NULL )
        {
            m_bFirstInstance = ( GetLastError() != ERROR_ALREADY_EXISTS );
        }
    }
}

CInstance::CInstance( const CInstance &rRhs )
{
    m_hInstanceMutex = rRhs.m_hInstanceMutex;
    m_bFirstInstance = rRhs.m_bFirstInstance;
}

CInstance::~CInstance( void )
{
    if ( m_hInstanceMutex != NULL )
    {
        ::CloseHandle( m_hInstanceMutex );
        m_hInstanceMutex = NULL;
    }
}

CInstance & CInstance::operator = ( const CInstance &rRhs )
{
    m_hInstanceMutex = rRhs.m_hInstanceMutex;
    m_bFirstInstance = rRhs.m_bFirstInstance;
    return *this;
}

bool CInstance::IsFirst( void ) const
{
    return m_bFirstInstance;
}

