Categories: Uncategorized

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.

admin

Recent Posts

Obtain global IP in bash

In current Internet environment, every PC is assigned a private IP. If global IP is…

3 years ago

Create log file in the directory of the script in Python

__file__ is the script file itself. From python3.9 above, it is an absolute path, otherwise…

3 years ago

RichTextBox’s font changes automatically in C#

You need to clear these two flags IMF_DUALFONT and IMF_AUTOFONT to keep font unchanged.

3 years ago

Test post to check image URL

This is an image inserted by wordpress 'Add Media'

3 years ago

msbuild says ‘target does not exist’

I don't know what is going wrong but... How to fix it Open the solution…

3 years ago

Creating a break point that hits on calling win32 api

Create a break point that hits when CreateProcess was called Enter a Function Breakpoint Enter…

4 years ago