From: Vadim Zeitlin Date: Thu, 28 Feb 2008 00:41:38 +0000 (+0000) Subject: use GlobalPtrLock (modified to allow not initializing it if the ptr is NULL) in wxGet... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/2d2b68baa3414a496bae9be99095e02f3b82a2df use GlobalPtrLock (modified to allow not initializing it if the ptr is NULL) in wxGetPrinterDC() to avoid /Wp64 warnings and also make code safer git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@52164 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index f31355f2cb..892d81c606 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -630,16 +630,33 @@ private: class GlobalPtrLock { public: - GlobalPtrLock(HGLOBAL hGlobal) : m_hGlobal(hGlobal) + // default ctor, use Init() later -- should only be used if the HGLOBAL can + // be NULL (in which case Init() shouldn't be called) + GlobalPtrLock() { - m_ptr = GlobalLock(hGlobal); + m_hGlobal = NULL; + m_ptr = NULL; + } + + // initialize the object, may be only called if we were created using the + // default ctor; HGLOBAL must not be NULL + void Init(HGLOBAL hGlobal) + { + m_hGlobal = hGlobal; + m_ptr = ::GlobalLock(hGlobal); if ( !m_ptr ) wxLogLastError(_T("GlobalLock")); } + // initialize the object, HGLOBAL must not be NULL + GlobalPtrLock(HGLOBAL hGlobal) + { + Init(hGlobal); + } + ~GlobalPtrLock() { - if ( !GlobalUnlock(m_hGlobal) ) + if ( m_hGlobal && !::GlobalUnlock(m_hGlobal) ) { #ifdef __WXDEBUG__ // this might happen simply because the block became unlocked @@ -652,6 +669,7 @@ public: } } + void *Get() const { return m_ptr; } operator void *() const { return m_ptr; } private: diff --git a/src/msw/dcprint.cpp b/src/msw/dcprint.cpp index 628e6e81b2..7eac01116f 100644 --- a/src/msw/dcprint.cpp +++ b/src/msw/dcprint.cpp @@ -327,17 +327,21 @@ WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst) } - HGLOBAL hDevMode = (HGLOBAL)(DWORD) data->GetDevMode(); - - DEVMODE *lpDevMode = hDevMode ? (DEVMODE *)::GlobalLock(hDevMode) : NULL; - - HDC hDC = ::CreateDC(NULL, deviceName.wx_str(), NULL, lpDevMode); + GlobalPtrLock lockDevMode; + const HGLOBAL devMode = data->GetDevMode(); + if ( devMode ) + lockDevMode.Init(devMode); + + HDC hDC = ::CreateDC + ( + NULL, // no driver name as we use device name + deviceName.wx_str(), + NULL, // unused + wx_static_cast(DEVMODE *, lockDevMode.Get()) + ); if ( !hDC ) wxLogLastError(_T("CreateDC(printer)")); - if ( lpDevMode ) - ::GlobalUnlock(hDevMode); - return (WXHDC) hDC; #endif // PostScript/Windows printing }