Revert "Introduces wxBSTR, an RAII wrapper for MSW BSTR type. wxBSTR also supersedes wxBasicString."

This reverts commit db22c91d44.
This commit is contained in:
PB
2017-06-23 17:05:03 +02:00
parent 3dd7d57bd4
commit 684f0146f1
8 changed files with 111 additions and 149 deletions

View File

@@ -21,7 +21,7 @@
// wx includes // wx includes
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "wx/msw/ole/oleutils.h" #include "wx/msw/ole/oleutils.h" // wxBasicString &c
#include "wx/msw/ole/uuid.h" #include "wx/msw/ole/uuid.h"
#include "wx/window.h" #include "wx/window.h"
#include "wx/variant.h" #include "wx/variant.h"

View File

@@ -61,6 +61,29 @@ inline void wxOleUninitialize()
::OleUninitialize(); ::OleUninitialize();
} }
// wrapper around BSTR type (by Vadim Zeitlin)
class WXDLLIMPEXP_CORE wxBasicString
{
public:
// ctors & dtor
wxBasicString(const wxString& str);
wxBasicString(const wxBasicString& bstr);
~wxBasicString();
wxBasicString& operator=(const wxBasicString& bstr);
// accessors
// just get the string
operator BSTR() const { return m_bstrBuf; }
// retrieve a copy of our string - caller must SysFreeString() it later!
BSTR Get() const { return SysAllocString(m_bstrBuf); }
private:
// actual string
BSTR m_bstrBuf;
};
#if wxUSE_VARIANT #if wxUSE_VARIANT
// Convert variants // Convert variants
class WXDLLIMPEXP_FWD_BASE wxVariant; class WXDLLIMPEXP_FWD_BASE wxVariant;
@@ -172,56 +195,6 @@ WXDLLIMPEXP_CORE BSTR wxConvertStringToOle(const wxString& str);
// Convert string from BSTR to wxString // Convert string from BSTR to wxString
WXDLLIMPEXP_CORE wxString wxConvertStringFromOle(BSTR bStr); WXDLLIMPEXP_CORE wxString wxConvertStringFromOle(BSTR bStr);
// A thin RAII wrapper for BSTR, which can also create BSTR
// from wxString as well as return the owned BSTR as wxString.
// Unlike _b_str_t, wxBSTR is NOT reference counted.
class WXDLLIMPEXP_CORE wxBSTR
{
public:
// Creates with the owned BSTR set to NULL
wxBSTR() : m_bstr(NULL) {}
// If copy is true then a copy of bstr is created,
// if not then ownership of bstr is taken.
wxBSTR(BSTR bstr, bool copy);
// Creates the owned BSTR from wxString str
wxBSTR(const wxString& str) : m_bstr(::SysAllocString(str.wc_str(*wxConvCurrent))) {}
// Creates a copy of BSTR owned by wxbstr
wxBSTR(const wxBSTR& wxbstr) : m_bstr(wxbstr.GetCopy()) {}
// Frees the owned BSTR
~wxBSTR() { Free(); }
// Creates a copy of the BSTR owned by wxbstr
wxBSTR& operator=(const wxBSTR& wxbstr);
// Takes ownership of bstr
void Attach(BSTR bstr);
// Returns the owned BSTR and gives up its ownership
BSTR Detach();
// Frees the owned BSTR
void Free();
// Returns the owned BSTR while keeping its ownership
BSTR GetBSTR() const { return m_bstr; }
operator BSTR() const { return GetBSTR(); }
// Returns the address of owned BSTR
BSTR* GetAddress() { return &m_bstr; }
// Returns a copy of owned BSTR
BSTR GetCopy() const { return ::SysAllocString(m_bstr); }
// Returns the owned BSTR converted to wxString
wxString GetwxString() const { return wxConvertStringFromOle(m_bstr); }
private:
BSTR m_bstr;
};
#else // !wxUSE_OLE #else // !wxUSE_OLE
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -1127,7 +1127,7 @@ bool wxAMMediaBackend::Load(const wxURI& location, const wxURI& proxy)
if(pPlay) if(pPlay)
{ {
pPlay->put_UseHTTPProxy(VARIANT_TRUE); pPlay->put_UseHTTPProxy(VARIANT_TRUE);
pPlay->put_HTTPProxyHost(wxBSTR(proxy.GetServer()).Detach()); pPlay->put_HTTPProxyHost(wxBasicString(proxy.GetServer()).Get());
pPlay->put_HTTPProxyPort(wxAtoi(proxy.GetPort())); pPlay->put_HTTPProxyPort(wxAtoi(proxy.GetPort()));
pPlay->Release(); pPlay->Release();
} }
@@ -1150,9 +1150,9 @@ bool wxAMMediaBackend::DoLoad(const wxString& location)
// the docs say its async and put_FileName is not - // the docs say its async and put_FileName is not -
// but in practice they both seem to be async anyway // but in practice they both seem to be async anyway
if(GetMP()) if(GetMP())
hr = GetMP()->Open( wxBSTR(location).Detach() ); hr = GetMP()->Open( wxBasicString(location).Get() );
else else
hr = GetAM()->put_FileName( wxBSTR(location).Detach() ); hr = GetAM()->put_FileName( wxBasicString(location).Get() );
if(FAILED(hr)) if(FAILED(hr))
{ {

View File

@@ -905,21 +905,21 @@ bool wxWMP10MediaBackend::Load(const wxURI& location,
{ {
long lOldSetting; long lOldSetting;
if( pWMPNetwork->getProxySettings( if( pWMPNetwork->getProxySettings(
wxBSTR(location.GetScheme()).Detach(), &lOldSetting wxBasicString(location.GetScheme()).Get(), &lOldSetting
) == 0 && ) == 0 &&
pWMPNetwork->setProxySettings( pWMPNetwork->setProxySettings(
wxBSTR(location.GetScheme()).Detach(), // protocol wxBasicString(location.GetScheme()).Get(), // protocol
2) == 0) // 2 == manually specify 2) == 0) // 2 == manually specify
{ {
BSTR bsOldName = NULL; BSTR bsOldName = NULL;
long lOldPort = 0; long lOldPort = 0;
pWMPNetwork->getProxyName( pWMPNetwork->getProxyName(
wxBSTR(location.GetScheme()).Detach(), wxBasicString(location.GetScheme()).Get(),
&bsOldName); &bsOldName);
pWMPNetwork->getProxyPort( pWMPNetwork->getProxyPort(
wxBSTR(location.GetScheme()).Detach(), wxBasicString(location.GetScheme()).Get(),
&lOldPort); &lOldPort);
long lPort; long lPort;
@@ -936,11 +936,11 @@ bool wxWMP10MediaBackend::Load(const wxURI& location,
} }
if( pWMPNetwork->setProxyName( if( pWMPNetwork->setProxyName(
wxBSTR(location.GetScheme()).Detach(), // proto wxBasicString(location.GetScheme()).Get(), // proto
wxBSTR(server).Detach() ) == 0 && wxBasicString(server).Get() ) == 0 &&
pWMPNetwork->setProxyPort( pWMPNetwork->setProxyPort(
wxBSTR(location.GetScheme()).Detach(), // proto wxBasicString(location.GetScheme()).Get(), // proto
lPort lPort
) == 0 ) == 0
) )
@@ -948,16 +948,16 @@ bool wxWMP10MediaBackend::Load(const wxURI& location,
bOK = DoLoad(location.BuildURI()); bOK = DoLoad(location.BuildURI());
pWMPNetwork->setProxySettings( pWMPNetwork->setProxySettings(
wxBSTR(location.GetScheme()).Detach(), // protocol wxBasicString(location.GetScheme()).Get(), // protocol
lOldSetting); lOldSetting);
if(bsOldName) if(bsOldName)
pWMPNetwork->setProxyName( pWMPNetwork->setProxyName(
wxBSTR(location.GetScheme()).Detach(), // protocol wxBasicString(location.GetScheme()).Get(), // protocol
bsOldName); bsOldName);
if(lOldPort) if(lOldPort)
pWMPNetwork->setProxyPort( pWMPNetwork->setProxyPort(
wxBSTR(location.GetScheme()).Detach(), // protocol wxBasicString(location.GetScheme()).Get(), // protocol
lOldPort); lOldPort);
pWMPNetwork->Release(); pWMPNetwork->Release();
@@ -997,7 +997,7 @@ bool wxWMP10MediaBackend::DoLoad(const wxString& location)
{ {
IWMPMedia* pWMPMedia; IWMPMedia* pWMPMedia;
if( (hr = pWMPCore3->newMedia(wxBSTR(location).Detach(), if( (hr = pWMPCore3->newMedia(wxBasicString(location).Get(),
&pWMPMedia)) == 0) &pWMPMedia)) == 0)
{ {
// this (get_duration) will actually FAIL, but it will work. // this (get_duration) will actually FAIL, but it will work.
@@ -1012,7 +1012,7 @@ bool wxWMP10MediaBackend::DoLoad(const wxString& location)
#endif #endif
{ {
// just load it the "normal" way // just load it the "normal" way
hr = m_pWMPPlayer->put_URL( wxBSTR(location).Detach() ); hr = m_pWMPPlayer->put_URL( wxBasicString(location).Get() );
} }
if(FAILED(hr)) if(FAILED(hr))
@@ -1061,12 +1061,12 @@ bool wxWMP10MediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags)
if(!flags) if(!flags)
{ {
m_pWMPPlayer->put_enabled(VARIANT_FALSE); m_pWMPPlayer->put_enabled(VARIANT_FALSE);
m_pWMPPlayer->put_uiMode(wxBSTR(wxT("none")).Detach()); m_pWMPPlayer->put_uiMode(wxBasicString(wxT("none")).Get());
} }
else else
{ {
// TODO: use "custom"? (note that CE only supports none/full) // TODO: use "custom"? (note that CE only supports none/full)
m_pWMPPlayer->put_uiMode(wxBSTR(wxT("full")).Detach()); m_pWMPPlayer->put_uiMode(wxBasicString(wxT("full")).Get());
m_pWMPPlayer->put_enabled(VARIANT_TRUE); m_pWMPPlayer->put_enabled(VARIANT_TRUE);
} }
@@ -1358,7 +1358,7 @@ wxLongLong wxWMP10MediaBackend::GetDownloadTotal()
if(m_pWMPPlayer->get_currentMedia(&pWMPMedia) == 0) if(m_pWMPPlayer->get_currentMedia(&pWMPMedia) == 0)
{ {
BSTR bsOut; BSTR bsOut;
pWMPMedia->getItemInfo(wxBSTR(wxT("FileSize")).Detach(), pWMPMedia->getItemInfo(wxBasicString(wxT("FileSize")).Get(),
&bsOut); &bsOut);
wxString sFileSize = wxConvertStringFromOle(bsOut); wxString sFileSize = wxConvertStringFromOle(bsOut);

View File

@@ -937,8 +937,9 @@ STDMETHODIMP wxIAccessible::get_accDefaultAction ( VARIANT varID, BSTR* pszDefau
return S_FALSE; return S_FALSE;
} }
else else
{ {
* pszDefaultAction = wxBSTR(defaultAction).Detach(); wxBasicString basicString(defaultAction);
* pszDefaultAction = basicString.Get();
return S_OK; return S_OK;
} }
} }
@@ -997,8 +998,9 @@ STDMETHODIMP wxIAccessible::get_accDescription ( VARIANT varID, BSTR* pszDescrip
return S_FALSE; return S_FALSE;
} }
else else
{ {
* pszDescription = wxBSTR(description).Detach(); wxBasicString basicString(description);
* pszDescription = basicString.Get();
return S_OK; return S_OK;
} }
} }
@@ -1057,8 +1059,9 @@ STDMETHODIMP wxIAccessible::get_accHelp ( VARIANT varID, BSTR* pszHelp)
return S_FALSE; return S_FALSE;
} }
else else
{ {
* pszHelp = wxBSTR(helpString).Detach(); wxBasicString basicString(helpString);
* pszHelp = basicString.Get();
return S_OK; return S_OK;
} }
} }
@@ -1168,7 +1171,8 @@ STDMETHODIMP wxIAccessible::get_accKeyboardShortcut ( VARIANT varID, BSTR* pszKe
} }
else else
{ {
* pszKeyboardShortcut = wxBSTR(keyboardShortcut).Detach(); wxBasicString basicString(keyboardShortcut);
* pszKeyboardShortcut = basicString.Get();
return S_OK; return S_OK;
} }
} }
@@ -1230,8 +1234,9 @@ STDMETHODIMP wxIAccessible::get_accName ( VARIANT varID, BSTR* pszName)
return S_FALSE; return S_FALSE;
} }
else else
{ {
*pszName = wxBSTR(name).Detach(); wxBasicString basicString(name);
*pszName = basicString.Get();
} }
return S_OK; return S_OK;
} }
@@ -1411,7 +1416,8 @@ STDMETHODIMP wxIAccessible::get_accValue ( VARIANT varID, BSTR* pszValue)
} }
else else
{ {
* pszValue = wxBSTR(strValue).Detach(); wxBasicString basicString(strValue);
* pszValue = basicString.Get();
return S_OK; return S_OK;
} }
} }

View File

@@ -130,7 +130,7 @@ bool wxAutomationObject::Invoke(const wxString& member, int action,
} }
int namedArgStringCount = namedArgCount + 1; int namedArgStringCount = namedArgCount + 1;
wxVector<wxBSTR> argNames(namedArgStringCount, wxString()); wxVector<wxBasicString> argNames(namedArgStringCount, wxString());
argNames[0] = member; argNames[0] = member;
// Note that arguments are specified in reverse order // Note that arguments are specified in reverse order
@@ -156,7 +156,7 @@ bool wxAutomationObject::Invoke(const wxString& member, int action,
// Get the IDs for the member and its arguments. GetIDsOfNames expects the // Get the IDs for the member and its arguments. GetIDsOfNames expects the
// member name as the first name, followed by argument names (if any). // member name as the first name, followed by argument names (if any).
hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL,
// We rely on the fact that wxBSTR is // We rely on the fact that wxBasicString is
// just BSTR with some methods here. // just BSTR with some methods here.
reinterpret_cast<BSTR *>(&argNames[0]), reinterpret_cast<BSTR *>(&argNames[0]),
1 + namedArgCount, m_lcid, &dispIds[0]); 1 + namedArgCount, m_lcid, &dispIds[0]);
@@ -499,7 +499,7 @@ namespace
HRESULT wxCLSIDFromProgID(const wxString& progId, CLSID& clsId) HRESULT wxCLSIDFromProgID(const wxString& progId, CLSID& clsId)
{ {
HRESULT hr = CLSIDFromProgID(wxBSTR(progId), &clsId); HRESULT hr = CLSIDFromProgID(wxBasicString(progId), &clsId);
if ( FAILED(hr) ) if ( FAILED(hr) )
{ {
wxLogSysError(hr, _("Failed to find CLSID of \"%s\""), progId); wxLogSysError(hr, _("Failed to find CLSID of \"%s\""), progId);

View File

@@ -38,7 +38,7 @@
WXDLLEXPORT BSTR wxConvertStringToOle(const wxString& str) WXDLLEXPORT BSTR wxConvertStringToOle(const wxString& str)
{ {
return wxBSTR(str).Detach(); return wxBasicString(str).Get();
} }
WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr) WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr)
@@ -68,48 +68,28 @@ WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr)
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxBSTR // wxBasicString
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxBSTR::wxBSTR(BSTR bstr, bool copy) wxBasicString::wxBasicString(const wxString& str)
{ {
if ( copy ) m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent));
m_bstr = ::SysAllocString(bstr); }
else
m_bstr = bstr; wxBasicString::wxBasicString(const wxBasicString& src)
} {
m_bstrBuf = src.Get();
wxBSTR& wxBSTR::operator=(const wxBSTR& wxbstr) }
{
if ( this != &wxbstr ) wxBasicString& wxBasicString::operator=(const wxBasicString& src)
{ {
Free(); SysReAllocString(&m_bstrBuf, src);
m_bstr = wxbstr.GetCopy(); return *this;
} }
return *this; wxBasicString::~wxBasicString()
} {
SysFreeString(m_bstrBuf);
void wxBSTR::Attach(BSTR bstr)
{
wxCHECK_RET(m_bstr != bstr, wxS("Attaching already attached BSTR!"));
Free();
m_bstr = bstr;
}
BSTR wxBSTR::Detach()
{
BSTR bstr = m_bstr;
m_bstr = NULL;
return bstr;
}
void wxBSTR::Free()
{
::SysFreeString(m_bstr);
m_bstr = NULL;
} }

View File

@@ -231,9 +231,9 @@ wxString wxWebViewIE::GetPageSource() const
hr = bodyTag->get_parentElement(&htmlTag); hr = bodyTag->get_parentElement(&htmlTag);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
wxBSTR wxbstr; BSTR bstr;
if ( htmlTag->get_outerHTML(wxbstr.GetAddress()) == S_OK ) if ( htmlTag->get_outerHTML(&bstr) == S_OK )
source = wxbstr.GetwxString(); source = wxString(bstr);
} }
} }
return source; return source;
@@ -576,9 +576,9 @@ wxString wxWebViewIE::GetCurrentTitle() const
wxString s; wxString s;
if(document) if(document)
{ {
wxBSTR title; BSTR title;
if ( document->get_nameProp(title.GetAddress()) == S_OK ) if ( document->get_nameProp(&title) == S_OK )
s = title.GetwxString(); s = title;
} }
return s; return s;
@@ -705,10 +705,10 @@ bool wxWebViewIE::IsEditable() const
if(document) if(document)
{ {
wxBSTR mode; BSTR mode;
if ( document->get_designMode(mode.GetAddress()) == S_OK ) if ( document->get_designMode(&mode) == S_OK )
{ {
if ( mode.GetwxString() == "On" ) if ( wxString(mode) == "On" )
return true; return true;
} }
} }
@@ -731,9 +731,9 @@ bool wxWebViewIE::HasSelection() const
HRESULT hr = document->get_selection(&selection); HRESULT hr = document->get_selection(&selection);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
wxBSTR type; BSTR type;
if ( selection->get_type(type.GetAddress()) == S_OK ) if ( selection->get_type(&type) == S_OK )
sel = type.GetwxString(); sel = wxString(type);
} }
return sel != "None"; return sel != "None";
} }
@@ -767,9 +767,9 @@ wxString wxWebViewIE::GetSelectedText() const
hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range); hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
wxBSTR text; BSTR text;
if ( range->get_text(text.GetAddress()) == S_OK ) if ( range->get_text(&text) == S_OK )
selected = text.GetwxString(); selected = wxString(text);
} }
} }
} }
@@ -800,9 +800,9 @@ wxString wxWebViewIE::GetSelectedSource() const
hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range); hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
wxBSTR text; BSTR text;
if ( range->get_htmlText(text.GetAddress()) == S_OK ) if ( range->get_htmlText(&text) == S_OK )
selected = text.GetwxString(); selected = wxString(text);
} }
} }
} }
@@ -841,9 +841,9 @@ wxString wxWebViewIE::GetPageText() const
HRESULT hr = document->get_body(&body); HRESULT hr = document->get_body(&body);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
wxBSTR out; BSTR out;
if ( body->get_innerText(out.GetAddress()) == S_OK ) if ( body->get_innerText(&out) == S_OK )
text = out.GetwxString(); text = wxString(out);
} }
return text; return text;
} }
@@ -950,7 +950,7 @@ wxCOMPtr<IHTMLDocument2> wxWebViewIE::GetDocument() const
bool wxWebViewIE::IsElementVisible(wxCOMPtr<IHTMLElement> elm) bool wxWebViewIE::IsElementVisible(wxCOMPtr<IHTMLElement> elm)
{ {
wxCOMPtr<IHTMLElement> elm1 = elm; wxCOMPtr<IHTMLElement> elm1 = elm;
wxBSTR tmp_bstr; BSTR tmp_bstr;
bool is_visible = true; bool is_visible = true;
//This method is not perfect but it does discover most of the hidden elements. //This method is not perfect but it does discover most of the hidden elements.
//so if a better solution is found, then please do improve. //so if a better solution is found, then please do improve.
@@ -963,13 +963,13 @@ bool wxWebViewIE::IsElementVisible(wxCOMPtr<IHTMLElement> elm)
if(SUCCEEDED(elm2->get_currentStyle(&style))) if(SUCCEEDED(elm2->get_currentStyle(&style)))
{ {
//Check if the object has the style display:none. //Check if the object has the style display:none.
if((style->get_display(tmp_bstr.GetAddress()) != S_OK) || if((style->get_display(&tmp_bstr) != S_OK) ||
(tmp_bstr != NULL && (wxCRT_StricmpW(tmp_bstr, L"none") == 0))) (tmp_bstr != NULL && (wxCRT_StricmpW(tmp_bstr, L"none") == 0)))
{ {
is_visible = false; is_visible = false;
} }
//Check if the object has the style visibility:hidden. //Check if the object has the style visibility:hidden.
if((is_visible && (style->get_visibility(tmp_bstr.GetAddress()) != S_OK)) || if((is_visible && (style->get_visibility(&tmp_bstr) != S_OK)) ||
(tmp_bstr != NULL && wxCRT_StricmpW(tmp_bstr, L"hidden") == 0)) (tmp_bstr != NULL && wxCRT_StricmpW(tmp_bstr, L"hidden") == 0))
{ {
is_visible = false; is_visible = false;
@@ -1007,8 +1007,8 @@ void wxWebViewIE::FindInternal(const wxString& text, int flags, int internal_fla
if(SUCCEEDED(document->QueryInterface(wxIID_IMarkupContainer, (void **)&pIMC))) if(SUCCEEDED(document->QueryInterface(wxIID_IMarkupContainer, (void **)&pIMC)))
{ {
wxCOMPtr<wxIMarkupPointer> ptrBegin, ptrEnd; wxCOMPtr<wxIMarkupPointer> ptrBegin, ptrEnd;
wxBSTR attr_bstr(L"style=\"background-color:#ffff00\""); BSTR attr_bstr = SysAllocString(L"style=\"background-color:#ffff00\"");
wxBSTR text_bstr(text.wc_str()); BSTR text_bstr = SysAllocString(text.wc_str());
pIMS->CreateMarkupPointer(&ptrBegin); pIMS->CreateMarkupPointer(&ptrBegin);
pIMS->CreateMarkupPointer(&ptrEnd); pIMS->CreateMarkupPointer(&ptrEnd);
@@ -1064,6 +1064,9 @@ void wxWebViewIE::FindInternal(const wxString& text, int flags, int internal_fla
} }
ptrBegin->MoveToPointer(ptrEnd); ptrBegin->MoveToPointer(ptrEnd);
} }
//Clean up.
SysFreeString(text_bstr);
SysFreeString(attr_bstr);
} }
} }
} }