Click here to Skip to main content
15,893,594 members
Articles / Programming Languages / C++/CLI

A security neutral mutex class for the managed platform

Rate me:
Please Sign up or sign in to vote.
3.93/5 (6 votes)
5 May 20064 min read 38.9K   173   6  
An article on a security neutral mutex class that can be used on any managed platform.
#pragma once

class CMutexSecurityNuetralUnmanaged
{

private:
	const char* m_szMutexName;
	HANDLE m_hMutex;

public:
	CMutexSecurityNuetralUnmanaged(const char*  szMutexName):m_szMutexName(szMutexName)
	{
		m_hMutex = NULL;
	}

	~CMutexSecurityNuetralUnmanaged()
	{
	}

	 bool WaitOne()
	 {
		bool rtn = false;
		SECURITY_DESCRIPTOR sd;
		SECURITY_ATTRIBUTES sa;
		try{
			if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) return FALSE;			
			if (!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE))  return FALSE;

			sa.nLength = sizeof(sa);
			sa.lpSecurityDescriptor = &sd;
			sa.bInheritHandle = FALSE;
			m_hMutex = CreateMutex(&sa, true, m_szMutexName);
			WaitForSingleObject( m_hMutex, 10000L);
			
			rtn = true;
		}
		catch(...){
			rtn = false;
		}
		return rtn;
	 }

	 bool Done()
	 {
		 bool rtn = false;
		 try{
			ReleaseMutex(m_hMutex);
			CloseHandle(m_hMutex);
			rtn = true;
		}
		catch(...)
		{
			rtn = false;
		}
		return rtn;
	 }

};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions