Click here to Skip to main content
15,880,469 members
Articles / Desktop Programming / Win32
Alternative
Tip/Trick

How to avoid multiple instances of your Windows application

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
27 Jan 2011CPOL 9.7K   4   1
Solution II used to use following simple way to manage the single instance of the app.//Usually I append the UID to the application name to achieve the unique object name.TCHAR szAppName[] = _T("Application_Name_999e7ba3-e8fc-4c21-985b-ab11f39ce759");HANDLE hMutex =...
Solution I

I used to use following simple way to manage the single instance of the app.

//Usually I append the UID to the application name to achieve the unique object name.

TCHAR szAppName[] = _T("Application_Name_999e7ba3-e8fc-4c21-985b-ab11f39ce759");
HANDLE  hMutex	  = NULL;

bool IsSecondInstance()
{
	bool isSecondInstance	= false;
	hMutex			= CreateMutex(NULL, NULL, szAppName);
	int iLastError          = GetLastError();

	if(hMutex && (ERROR_ACCESS_DENIED == iLastError || ERROR_ALREADY_EXISTS == iLastError))
	{
		ReleaseMutex(hMutex);
		
		hMutex			= NULL;
		isSecondInstance	= true;
	}

	return isSecondInstance;
}


Code usage is as follows:

int  main()
{
	if(IsSecondInstance())
	{
		MessageBox(NULL, _T("One Instance Is Already Running."), _T("Information"), 0);
		return 0;
	}

	/*App Code Goes Here...*/

	ReleaseMutex(m_hMutex);
	m_hMutex    =   NULL;
	
	return 0;
}


Solution II
In the meanwhile, I modified the logic to achieve the OOPs concept.

C#
class CAppGuard
{
private:
    HANDLE      m_hMutex;
    const TCHAR *m_pszAppName;

public:
    CAppGuard(TCHAR *szAppName = NULL): m_hMutex(NULL), m_pszAppName(szAppName)
    {}

    bool IsSecondInstance()
    {
        bool isSecondInstance   = false;
        m_hMutex                = CreateMutex(NULL, NULL, m_pszAppName);
	int iLastError		= GetLastError();

	if(hMutex && (ERROR_ACCESS_DENIED == iLastError || ERROR_ALREADY_EXISTS == iLastError))
        {
            ReleaseMutex(m_hMutex);

            m_hMutex            = NULL;
            isSecondInstance    = true;
        }
        
        return isSecondInstance;
    }

    ~CAppGuard()
    {
        if(m_hMutex)
            ReleaseMutex(m_hMutex);

        m_hMutex    =   NULL;
    }
};


Code usage is as:

C#
//Append the UID to the application name to achieve the unique object name.

TCHAR szAppName[] = _T("Application_Name_999e7ba3-e8fc-4c21-985b-ab11f39ce759");

int  main()
{
        CAppGuard objAppGuard(szAppName);

        if(objAppGuard.IsSecondInstance())
   {
        MessageBox(NULL, _T("One Instance Is Already Running."), _T("Information"), 0);
        return 0;
    }

    /*App Code Goes Here...*/

    return 0;
}


As the named kernel objects can be assessed across the terminal sessions, they can be named by prefixing the namespace. i.e. Global namespace or Local namespace.

i.e. "Global\\ObjectName", "Local\\ObjectName"

By considering Ajay's comment what minimum changes I could do is specify the app name as:

TCHAR szAppName[] = _T("Global\\Application_Name_999e7ba3-e8fc-4c21-985b-ab11f39ce759");


or

TCHAR szAppName[] = _T("Local\\Application_Name_999e7ba3-e8fc-4c21-985b-ab11f39ce759");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Hello All !
This is Mallinath S. Karkanti, from India. I'm working as a Software Developer in one of the Middle Scale Company... !

Comments and Discussions

 
GeneralThanks Deksha Shenoy. Pin
Malli_S27-Jan-11 19:56
Malli_S27-Jan-11 19:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.