Click here to Skip to main content
Click here to Skip to main content

How to avoid multiple instances of your Windows application

By , 27 Jan 2011
 
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.
 
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:
 
//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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks Deksha Shenoy.memberMalli_S27 Jan '11 - 19:56 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 27 Jan 2011
Article Copyright 2011 by Malli_S
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid