0

I've created a COM wrapper in C# with a method that returns an array of strings:

public string[] GetArrayOfStrings()
{
    string[] array = new string[3];
    array[0] = "first";
    array[1] = "second";
    array[2] = "third";

    return array;
}

In VB6 I'm calling that method and presenting strings in a list like this:

Dim s() As String
s = obj.GetArrayOfStrings()
For i = LBound(s) To UBound(s)
    List1.AddItem s(i)
Next i

Does anyone know how to call that method from Borland C++ and get all elements in the returning array?

4

1 回答 1

2

COM 中的数组由SAFEARRAY结构处理。

根据 COM 对象如何公开数组,它可能会SAFEARRAY直接返回 a,例如:

SAFEARRAY *psa = obj->GetArrayOfStrings();
VARTYPE vtype;
SafeArrayGetVartype(psa, &vtype);
if (vtype == VT_BSTR)
{
    LONG lBound, uBound;
    SafeArrayGetLBound(psa, 0, &lBound);
    SafeArrayGetUBound(psa, 0, &uBound);
    for(LONG i = lBound; i <= uBound; ++i)
    {
        BSTR str;
        SafeArrayGetElement(psa, &i, &str);
        ...
        SysFreeString(str);
    }
}
SafeArrayDestroy(psa);

或者它可能被包裹在一个VARIANT结构中,例如:

VARIANT v = obj->GetArrayOfStrings();
if (V_VT(&v) & VT_ARRAY)
{
    SAFEARRAY *psa = V_ARRAY(&v);
    ...
}
VariantClear(&v);

无论哪种方式,数组内部的元素可能会或可能不会包裹在VARIANT结构内,例如:

SafeArrayGetVartype(psa, &vtype);
if (vtype == VT_VARIANT)
{
    ...
    VARIANT elem;
    VariantInit(&elem);
    SafeArrayGetElement(psa, &i, &elem);
    ...
    VariantClear(&elem);
    ...
}
于 2013-10-03T07:43:37.847 回答