37 using sys_handle = HANDLE;
38 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
40 using sys_handle = int;
41 const sys_handle invalid_handle = (sys_handle)-1;
48 using sys_char = TCHAR;
50 using sys_char = char;
57 using sys_string = std::basic_string<stdex::sys_char>;
65 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
71 if (
this != std::addressof(other)) {
72 if (m_h != invalid_handle)
74 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
81 other.m_h = invalid_handle;
86 if (
this != std::addressof(other)) {
87 if (m_h != invalid_handle)
90 other.m_h = invalid_handle;
97 if (m_h != invalid_handle)
106 if (m_h != invalid_handle) {
108 m_h = invalid_handle;
115 inline operator bool() const noexcept {
return m_h != invalid_handle; }
120 inline sys_handle
get() const noexcept {
return m_h; }
129 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
131 if (
close(h) >= 0 || errno == EBADF)
134 throw std::runtime_error(
"failed to close handle");
140 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
144 HANDLE process = GetCurrentProcess();
145 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
147 UNREFERENCED_PARAMETER(inherit);
148 if ((h_new = dup(h)) >= 0)
151 throw std::runtime_error(
"failed to duplicate handle");
160 class safearray_accessor
163 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
165 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
167 throw std::invalid_argument(
"SafeArrayAccessData failed");
170 ~safearray_accessor()
172 SafeArrayUnaccessData(m_sa);
175 T* data()
const {
return m_data; }
185 struct SafeArrayDestroy_delete
190 void operator()(_In_ LPSAFEARRAY sa)
const
192 SafeArrayDestroy(sa);
199 struct SysFreeString_delete
204 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:63
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:120
virtual void close()
Closes object.
Definition system.hpp:104
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:140
static void close(sys_handle h)
Closes object.
Definition system.hpp:126