7zip/CPP/Windows/Handle.h

40 lines
687 B
C
Raw Normal View History

2021-12-27 01:00:00 +01:00
// Windows/Handle.h
2023-06-21 02:00:00 +02:00
#ifndef ZIP7_INC_WINDOWS_HANDLE_H
#define ZIP7_INC_WINDOWS_HANDLE_H
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
#include "../Common/MyWindows.h"
2021-12-27 01:00:00 +01:00
namespace NWindows {
class CHandle MY_UNCOPYABLE
{
protected:
HANDLE _handle;
public:
operator HANDLE() { return _handle; }
CHandle(): _handle(NULL) {}
~CHandle() { Close(); }
bool IsCreated() const { return (_handle != NULL); }
bool Close()
{
if (_handle == NULL)
return true;
if (!::CloseHandle(_handle))
return false;
_handle = NULL;
return true;
}
void Attach(HANDLE handle) { _handle = handle; }
HANDLE Detach()
{
2023-06-21 02:00:00 +02:00
const HANDLE handle = _handle;
2021-12-27 01:00:00 +01:00
_handle = NULL;
return handle;
}
};
}
#endif