7zip/CPP/Windows/MemoryGlobal.h

56 lines
990 B
C
Raw Normal View History

2021-12-27 01:00:00 +01:00
// Windows/MemoryGlobal.h
2023-06-21 02:00:00 +02:00
#ifndef ZIP7_INC_WINDOWS_MEMORY_GLOBAL_H
#define ZIP7_INC_WINDOWS_MEMORY_GLOBAL_H
2021-12-27 01:00:00 +01:00
#include "../Common/MyWindows.h"
namespace NWindows {
namespace NMemory {
class CGlobal
{
HGLOBAL _global;
public:
2023-06-21 02:00:00 +02:00
CGlobal(): _global(NULL) {}
2021-12-27 01:00:00 +01:00
~CGlobal() { Free(); }
operator HGLOBAL() const { return _global; }
void Attach(HGLOBAL hGlobal)
{
Free();
_global = hGlobal;
}
HGLOBAL Detach()
{
2023-06-21 02:00:00 +02:00
const HGLOBAL h = _global;
2021-12-27 01:00:00 +01:00
_global = NULL;
return h;
}
bool Alloc(UINT flags, SIZE_T size) throw();
bool Free() throw();
LPVOID Lock() const { return GlobalLock(_global); }
void Unlock() const { GlobalUnlock(_global); }
bool ReAlloc(SIZE_T size) throw();
};
class CGlobalLock
{
HGLOBAL _global;
LPVOID _ptr;
public:
LPVOID GetPointer() const { return _ptr; }
CGlobalLock(HGLOBAL hGlobal): _global(hGlobal)
{
_ptr = GlobalLock(hGlobal);
2023-06-21 02:00:00 +02:00
}
2021-12-27 01:00:00 +01:00
~CGlobalLock()
{
2023-06-21 02:00:00 +02:00
if (_ptr)
2021-12-27 01:00:00 +01:00
GlobalUnlock(_global);
}
};
}}
#endif