]> git.saurik.com Git - wxWidgets.git/commitdiff
added GlobalHandle
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 23 Mar 2003 20:36:51 +0000 (20:36 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 23 Mar 2003 20:36:51 +0000 (20:36 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19741 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/private.h

index 9d639f1fb81942b0515da3e778ba690f82d04f80..3bd6f49620225f84f06cdfbde55f9d547dc85df9 100644 (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)
 };
 
 // ---------------------------------------------------------------------------