7 #ifndef _INC_cOSHandle_H
8 #define _INC_cOSHandle_H
21 #define HANDLE_NULL NULL
23 #define WINHANDLE_NULL NULL
27 static const HANDLE INVALID_HANDLE_VALUE = ((HANDLE)-1);
29 typedef void* HMODULE;
30 typedef void* HINSTANCE;
45 #if defined(_MFC_VER) && ( _MFC_VER > 0x0600 )
49 #define USE_FILE_POS64
80 explicit inline cOSHandle(HANDLE h = INVALID_HANDLE_VALUE) noexcept
86 : m_h(Handle.Duplicate())
91 if (m_h != Handle.
m_h)
93 AttachHandle(Handle.Duplicate());
103 operator HANDLE () const noexcept
123 return(h != INVALID_HANDLE_VALUE);
127 return IsValidHandle(m_h);
136 const BOOL bRet = ::CloseHandle(h);
138 #elif defined(__linux__)
139 const int iRet = ::close(h);
145 if (!isValidHandle())
148 m_h = INVALID_HANDLE_VALUE;
153 void OpenHandle(
const char* pszPath, UINT uFlags, UINT uMode=0)
159 m_h = ::open(pszPath, uFlags, uMode);
174 m_h = INVALID_HANDLE_VALUE;
187 DWORD nLengthWritten = 0;
188 const bool bRet = ::WriteFile(m_h, pData, (DWORD)nDataSize, &nLengthWritten,
nullptr);
190 #elif defined(__linux__)
191 int nLengthWritten = ::write(m_h, (
const char*)pData, (
long)nDataSize);
192 if (nLengthWritten <= 0)
197 return (
HRESULT) nLengthWritten;
207 const bool bRet = ::ReadFile(m_h, pData, (DWORD)nDataSize, &nLengthRead,
nullptr);
209 #elif defined(__linux__)
210 int nLengthRead = ::read(m_h, pData, (
long)nDataSize);
211 if (nLengthRead == 0)
226 if (!::FlushFileBuffers(m_h))
227 #elif defined(__linux__)
228 int iRet = ::fsync(m_h);
243 #ifdef USE_FILE_POS64
244 LARGE_INTEGER NewFilePointer;
245 NewFilePointer.QuadPart = lOffset;
246 const bool bRet = ::SetFilePointerEx(m_h, NewFilePointer, &NewFilePointer, eSeekOrigin);
253 return ::SetFilePointer(m_h, (LONG)lOffset,
nullptr, eSeekOrigin);
256 return ::lseek(m_h, lOffset, eSeekOrigin);
263 int IOCtl(
int nCmd,
void* pArgs)
const;
264 int IOCtl(
int nCmd,
int nArgs)
const;
267 #if defined(_WIN32) && ! defined(UNDER_CE)
268 DWORD GetInformation()
const
272 DWORD dwHandleInfo = 0;
273 if (!::GetHandleInformation(m_h, &dwHandleInfo))
279 bool SetInformation(DWORD dwMask, DWORD dwFlags)
const
282 const bool bRet = ::SetHandleInformation(m_h, dwMask, dwFlags);
288 HANDLE Duplicate(HANDLE hTargetProcess = INVALID_HANDLE_VALUE, DWORD dwDesiredAccess = DUPLICATE_SAME_ACCESS,
bool bInheritHandle =
false, DWORD dwOptions = DUPLICATE_SAME_ACCESS)
const
293 HANDLE hNewHandle = INVALID_HANDLE_VALUE;
294 HANDLE hCurrentProcess = ::GetCurrentProcess();
295 if (hTargetProcess == INVALID_HANDLE_VALUE)
296 hTargetProcess = hCurrentProcess;
297 const bool bRet = ::DuplicateHandle(hCurrentProcess, m_h, hTargetProcess,
298 &hNewHandle, dwDesiredAccess, bInheritHandle, dwOptions);
302 #elif defined(__linux__)
303 HANDLE Duplicate()
const
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define HRESULT_WIN32_C(x)
a constant LSTATUS/error_status_t with no check, unlike HRESULT_FROM_WIN32()
Definition: HResult.h:79
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
static HRESULT GetLastDef(HRESULT hResDef=E_FAIL) noexcept
Definition: HResult.h:242
Definition: cNonCopyable.h:17
Definition: cOSHandle.h:59
HRESULT ReadX(void *pData, size_t nDataSize) const
Definition: cOSHandle.h:200
static bool IsValidHandle(HANDLE h) noexcept
Definition: cOSHandle.h:116
void AttachHandle(HANDLE h) noexcept
Definition: cOSHandle.h:163
cOSHandle & operator=(const cOSHandle &Handle)
Definition: cOSHandle.h:89
STREAM_SEEKRET_t Seek(STREAM_OFFSET_t lOffset, SEEK_ORIGIN_TYPE eSeekOrigin) const
Definition: cOSHandle.h:237
void CloseHandle() noexcept
Definition: cOSHandle.h:143
~cOSHandle()
Definition: cOSHandle.h:98
cOSHandle(const cOSHandle &Handle) noexcept
Definition: cOSHandle.h:85
bool isValidHandle() const noexcept
Definition: cOSHandle.h:125
void CloseHandleLast() noexcept
Definition: cOSHandle.h:71
HRESULT FlushX() const
Definition: cOSHandle.h:222
HANDLE & ref_Handle() noexcept
Definition: cOSHandle.h:111
HRESULT WriteX(const void *pData, size_t nDataSize) const
Definition: cOSHandle.h:178
cOSHandle(HANDLE h=INVALID_HANDLE_VALUE) noexcept
Definition: cOSHandle.h:80
HANDLE get_Handle() const noexcept
Definition: cOSHandle.h:107
static bool CloseHandle(HANDLE h) noexcept
Definition: cOSHandle.h:130
HANDLE m_h
Definition: cOSHandle.h:68
HANDLE DetachHandle() noexcept
Definition: cOSHandle.h:171
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
LONG_PTR STREAM_OFFSET_t
Might be 64 or 32 bit. TODO SET USE_FILE_POS64.
Definition: cOSHandle.h:52
LONG_PTR STREAM_SEEKRET_t
return from Seek()
Definition: cOSHandle.h:53
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
ULONG_PTR STREAM_POS_t
NOT same as FILE_SIZE_t in 32 bit. Why not ?
Definition: cOSHandle.h:54
SEEK_ORIGIN_TYPE
Definition: cOSHandle.h:34
@ SEEK_End
SEEK_END = FILE_END = STREAM_SEEK_END = 2 = relative to the end of the file.
Definition: cOSHandle.h:41
@ SEEK_Set
SEEK_SET = FILE_BEGIN = STREAM_SEEK_SET = 0 = relative to the start of the file.
Definition: cOSHandle.h:39
@ SEEK_MASK
| _BITMASK(SEEK_Set) allow extra bits above SEEK_ORIGIN_TYPE ?
Definition: cOSHandle.h:42
@ SEEK_Cur
SEEK_CUR = FILE_CURRENT = STREAM_SEEK_CUR = 1 = relative to the current position.
Definition: cOSHandle.h:40