System global variable in win32

class CSessionGlobalBool  
{
public:
	explicit CSessionGlobalBool(LPCSTR pName) {
		m_pName = (LPSTR)LocalAlloc(LMEM_FIXED, lstrlenA(pName)+sizeof(char));
		lstrcpy(m_pName, pName);
	}
	~CSessionGlobalBool(){
		LocalFree(m_pName);
	}
 
	operator bool() {
		return get();
	}
 
	operator =(const bool b) {
		HANDLE h = CreateEvent(NULL,
			TRUE,
			FALSE,
			m_pName);
 
		if(b)
			SetEvent(h);
		else
			ResetEvent(h);
	}
	operator =(const CSessionGlobalBool& sgb) {
		*this = sgb.get();
	}
private:
	bool get() const {
		HANDLE h = CreateEvent(NULL,
			TRUE,
			FALSE,
			m_pName);
 
		return WaitForSingleObject(h, 0) == WAIT_OBJECT_0;
	}
	LPSTR m_pName;
};

I’m using a manual-set event object. In fact this is not a system global variable but session global which each user have until she/he logged out.

The bool value read from this object is volatile, even the comparison of the same object could become false.

Leave a comment

Your email address will not be published. Required fields are marked *