38 using sys_handle = HANDLE;
39 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
41 using sys_handle = int;
42 const sys_handle invalid_handle = (sys_handle)-1;
49 using sys_char = TCHAR;
51 using sys_char = char;
58 using sys_string = std::basic_string<stdex::sys_char>;
66 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
72 if (
this != std::addressof(other)) {
73 if (m_h != invalid_handle)
75 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
82 other.m_h = invalid_handle;
87 if (
this != std::addressof(other)) {
88 if (m_h != invalid_handle)
91 other.m_h = invalid_handle;
98 if (m_h != invalid_handle)
107 if (m_h != invalid_handle) {
109 m_h = invalid_handle;
116 inline operator bool() const noexcept {
return m_h != invalid_handle; }
121 inline sys_handle
get() const noexcept {
return m_h; }
130 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
132 if (
close(h) >= 0 || errno == EBADF)
135 throw std::runtime_error(
"failed to close handle");
141 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
145 HANDLE process = GetCurrentProcess();
146 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
148 UNREFERENCED_PARAMETER(inherit);
149 if ((h_new = dup(h)) >= 0)
152 throw std::runtime_error(
"failed to duplicate handle");
161 class safearray_accessor
164 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
166 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
168 throw std::invalid_argument(
"SafeArrayAccessData failed");
171 ~safearray_accessor()
173 SafeArrayUnaccessData(m_sa);
176 T* data()
const {
return m_data; }
186 struct SafeArrayDestroy_delete
191 void operator()(_In_ LPSAFEARRAY sa)
const
193 SafeArrayDestroy(sa);
200 struct SysFreeString_delete
205 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:64
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:121
virtual void close()
Closes object.
Definition system.hpp:105
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:141
static void close(sys_handle h)
Closes object.
Definition system.hpp:127