Assume that a scripting client calls an Automation method named GetData, and that GetData returns a callee-determined amount of data in a SAFEARRAY. Here's the caller's code: // Caller Dim arrVals As Variant arrVals = MyObject.GetData For i = LBound(arrVals) To UBound(arrVals) . . . Next iHere's the method definition in IDL: // IDL HRESULT GetData ([out, retval] VARIANT* pVariant) And here's a method implementation that returns an array of 10 integers: // Callee VariantInit (pVariant); pVariant->vt = VT_ARRAY | VT_I4; // Array of integers
SAFEARRAY* psa; // 10 elements numbered 0-9 SAFEARRAYBOUND bound = { 10, 0 }; psa = SafeArrayCreate (VT_I4, 1, &bound); if (psa == NULL) return E_OUTOFMEMORY; for (long i=0; i<10; i++) SafeArrayPutElement (psa, &i, &i); pVariant->parray = psa; return S_OK; SafeArrayCreate is a Windows
SafeArrayCreate is a Windows® API function that creates a SAFEARRAY. You pass a SAFEARRAY in a VARIANT by setting pVariant->parray equal to the SAFEARRAY's address and including in pVariant->vt a VT_ARRAY flag and a flag specifying the type of data stored in the array. The scripting client does the dirty work on the other end.
Dim arrVals As Variant
arrVals = MyObject.GetData
For i = LBound(arrVals) To UBound(arrVals)
.
.
.
Next iHere's the method definition in IDL:
// IDL
HRESULT GetData ([out, retval] VARIANT* pVariant)
And here's a method implementation that returns an array of 10 integers:
// Callee
VariantInit (pVariant);
pVariant->vt = VT_ARRAY | VT_I4; // Array of integers
SAFEARRAY* psa;
// 10 elements numbered 0-9
SAFEARRAYBOUND bound = { 10, 0 };
psa = SafeArrayCreate (VT_I4, 1, &bound);
if (psa == NULL)
return E_OUTOFMEMORY;
for (long i=0; i<10; i++)
SafeArrayPutElement (psa, &i, &i);
pVariant->parray = psa;
return S_OK;
SafeArrayCreate is a Windows