Use GlobalPtrLock instead of manual Global{Lock,Unlock}() calls

Also add GlobalPtrLock::GetSize() and use it instead of calling
GetSizeFromBuffer() as it's more direct and doesn't require the use of
::GlobalHandle().
This commit is contained in:
Vadim Zeitlin
2020-08-22 19:30:03 +02:00
parent aadfaada7a
commit 0d7be7c189
2 changed files with 16 additions and 16 deletions

View File

@@ -773,6 +773,15 @@ public:
void *Get() const { return m_ptr; } void *Get() const { return m_ptr; }
operator void *() const { return m_ptr; } operator void *() const { return m_ptr; }
size_t GetSize() const
{
const size_t size = ::GlobalSize(m_hGlobal);
if ( !size )
wxLogLastError(wxT("GlobalSize"));
return size;
}
private: private:
HGLOBAL m_hGlobal; HGLOBAL m_hGlobal;
void *m_ptr; void *m_ptr;

View File

@@ -713,12 +713,9 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
} }
// copy data // copy data
const void *pBuf = GlobalLock(pmedium->hGlobal); GlobalPtrLock ptr(pmedium->hGlobal);
if ( pBuf == NULL ) { if ( !ptr )
wxLogLastError(wxT("GlobalLock"));
return E_OUTOFMEMORY; return E_OUTOFMEMORY;
}
// we've got a problem with SetData() here because the base // we've got a problem with SetData() here because the base
// class version requires the size parameter which we don't // class version requires the size parameter which we don't
@@ -731,14 +728,14 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
case wxDF_HTML: case wxDF_HTML:
case CF_TEXT: case CF_TEXT:
case CF_OEMTEXT: case CF_OEMTEXT:
size = strlen((const char *)pBuf); size = strlen((const char *)ptr.Get());
break; break;
#if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500)) #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
case CF_UNICODETEXT: case CF_UNICODETEXT:
#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) )
size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t); size = std::wcslen((const wchar_t *)ptr.Get()) * sizeof(wchar_t);
#else #else
size = wxWcslen((const wchar_t *)pBuf) * sizeof(wchar_t); size = wxWcslen((const wchar_t *)ptr.Get()) * sizeof(wchar_t);
#endif #endif
break; break;
#endif #endif
@@ -759,19 +756,13 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
size = sizeof(METAFILEPICT); size = sizeof(METAFILEPICT);
break; break;
default: default:
pBuf = m_pDataObject-> size = ptr.GetSize();
GetSizeFromBuffer(pBuf, &size, format);
size -= m_pDataObject->GetBufferOffset(format); size -= m_pDataObject->GetBufferOffset(format);
} }
bool ok = m_pDataObject->SetData(format, size, pBuf); if ( !m_pDataObject->SetData(format, size, ptr.Get()) )
GlobalUnlock(pmedium->hGlobal);
if ( !ok ) {
return E_UNEXPECTED; return E_UNEXPECTED;
} }
}
break; break;
default: default: