added GlobalHandle

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19741 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2003-03-23 20:36:51 +00:00
parent f7292ebeba
commit 51a2a728e5

View File

@ -395,7 +395,46 @@ private:
HDC m_hdc;
HGDIOBJ m_hgdiobj;
DECLARE_NO_COPY_CLASS(SelectInHDC)
DECLARE_NO_COPY_CLASS(SelectInHDC)
};
// when working with global pointers (which is unfortunately still necessary
// sometimes, e.g. for clipboard) it is important to unlock them exactly as
// many times as we lock them which just asks for using a "smart lock" class
class GlobalHandle
{
public:
GlobalHandle(HGLOBAL hGlobal) : m_hGlobal(hGlobal)
{
m_ptr = ::GlobalLock(hGlobal);
if ( !m_ptr )
{
wxLogLastError(_T("GlobalLock"));
}
}
~GlobalHandle()
{
if ( !::GlobalUnlock(m_hGlobal) )
{
#ifdef __WXDEBUG__
// this might happen simply because the block became unlocked
DWORD dwLastError = ::GetLastError();
if ( dwLastError != NO_ERROR )
{
wxLogApiError(_T("GlobalUnlock"), dwLastError);
}
#endif // __WXDEBUG__
}
}
operator void *() const { return m_ptr; }
private:
HGLOBAL m_hGlobal;
void *m_ptr;
DECLARE_NO_COPY_CLASS(GlobalHandle)
};
// ---------------------------------------------------------------------------