From 82c372a7e244ab1bc1da358c5ce365d8b0c0dd68 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 15 May 2012 10:04:03 +0000 Subject: [PATCH] Add support for wxArrayString to wxVariant-to-OLE conversion. This allows to call COM methods taking arrays of strings easily. Also remove support for the old and non-existent any more "stringlist" variant type. And add more error checking. Closes #14296. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71437 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/ole/oleutils.cpp | 67 ++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/msw/ole/oleutils.cpp b/src/msw/ole/oleutils.cpp index 0ba95fc800..421e6a9e69 100644 --- a/src/msw/ole/oleutils.cpp +++ b/src/msw/ole/oleutils.cpp @@ -199,49 +199,62 @@ WXDLLEXPORT bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& ole oleVariant.vt = VT_DISPATCH; oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr(); } - else if (type == wxT("list") || type == wxT("stringlist")) + else if (type == wxT("list") || type == wxT("arrstring")) { - oleVariant.vt = VT_VARIANT | VT_ARRAY; - SAFEARRAY *psa; SAFEARRAYBOUND saBound; - VARIANTARG *pvargBase; - VARIANTARG *pvarg; - int i, j; + bool isArrString = type == wxT("arrstring"); + wxArrayString strings; - int iCount = variant.GetCount(); + if (isArrString) + strings = variant.GetArrayString(); + oleVariant.vt = (isArrString ? VT_BSTR : VT_VARIANT) | VT_ARRAY; + long lCount = isArrString ? strings.GetCount() : variant.GetCount(); saBound.lLbound = 0; - saBound.cElements = iCount; - - psa = SafeArrayCreate(VT_VARIANT, 1, &saBound); + saBound.cElements = lCount; + psa = SafeArrayCreate(isArrString ? VT_BSTR : VT_VARIANT, 1, &saBound); if (psa == NULL) return false; - SafeArrayAccessData(psa, (void**)&pvargBase); - - pvarg = pvargBase; - for (i = 0; i < iCount; i++) + long i; + for (i = 0; i < lCount; i++) { - // copy each string in the list of strings - wxVariant eachVariant(variant[i]); - if (!wxConvertVariantToOle(eachVariant, * pvarg)) + if (isArrString) { - // memory failure: back out and free strings alloc'ed up to - // now, and then the array itself. - pvarg = pvargBase; - for (j = 0; j < i; j++) + wxBasicString bstr(strings[i]); + if ( !bstr && !strings[i].empty() ) { - SysFreeString(pvarg->bstrVal); - pvarg++; + // BSTR can be NULL for empty strings but if the string was + // not empty, it means we failed to allocate memory for it. + break; } - SafeArrayDestroy(psa); - return false; + + if ( FAILED(SafeArrayPutElement(psa, &i, (BSTR)bstr)) ) + break; + } + else // list of wxVariants + { + VARIANT v; + if ( !wxConvertVariantToOle(variant[i], v) ) + break; + + HRESULT hr = SafeArrayPutElement(psa, &i, &v); + + // SafeArrayPutElement makes a copy of an added element, so + // free this one. + VariantClear(&v); + + if (FAILED(hr)) + break; } - pvarg++; } - SafeArrayUnaccessData(psa); + if (i < lCount) // the iteration was exited prematurely because of an error + { + SafeArrayDestroy(psa); + return false; + } oleVariant.parray = psa; } -- 2.45.2