Accessing text elements in a safearray
问题:
ActiveX interfaces are accessible in C++ through the COM 'wrappers' as the doc
sample, AsdkMfcComSamps. When I used IAcadLayout::GetPlotDeviceNames(), it
returns a VARIANT type 8200. In this case, a string Array.
How do I access the elements in it? Attaching or creating SafeArrays of
COleSafeArray is successful and the upper and lower bounds are clear but I
cannot reach the data (strings).
解答:
This can be done by using some of the safe array global functions. The following
code sample demonstrates how to achieve this.
bool GetStringListFromSafeArray(SAFEARRAY *psaItemTexts, CStringList &strNodes)
{
ASSERT (psaItemTexts);
ULONG ulDims = SafeArrayGetDim(psaItemTexts);
ASSERT (1 == ulDims);
if (!psaItemTexts || 1 != ulDims)
return false;
// see if we are given more than one items
long lLBound, lUBound; // note that dimension indices are one-based
HRESULT hRes = SafeArrayGetUBound (psaItemTexts, 1, &lUBound);
ASSERT (SUCCEEDED(hRes));
if (FAILED (hRes))
return false;
hRes = SafeArrayGetLBound (psaItemTexts, 1, &lLBound);
ASSERT (SUCCEEDED(hRes));
if (FAILED (hRes))
return false;
// AcDcss the array
BSTR *pabstrItemTexts = 0;
hRes = ::SafeArrayAccessData (psaItemTexts, (void**)&pabstrItemTexts);
ASSERT (SUCCEEDED (hRes));
if (FAILED (hRes))
return false;
strNodes.RemoveAll ();
USES_CONVERSION;
for (long lName = lLBound; lName <= lUBound; lName ++)
strNodes.AddTail (OLE2T (pabstrItemTexts ));
// release pointer returned by SafeArrayAcDcssData
SafeArrayUnaccessData (psaItemTexts);
return true;
}
页:
[1]