3

I have written a C++ COM which is running as COM+ application. I am trying to access COM functionality from VBScript (ASP application). I am able to call a function of COM from VBScript which takes a string. But when I try to call a COM function which takes an array of string, I could get length of array but I could not retrieve elements from that array at COM side.

VBScript (ASP application)

    dim myComObj
    Set myComObj = Server.CreateObject("ProgId_PerlCOMSimple.1")
    Dim myArray(3)
    myArray(0) = "Clean Underwear"
    myArray(1) = "Vacuum Cleaner"
    myArray(2) = "New Computer"
    myArray(3) = "Talking Bass"
    strDfStatus = myComObj.TestArray1 (myArray) 

C++ COM which runs as COM+ application (through dllHost.exe)

    STDMETHODIMP CPerlCOMSimple::TestArray1(VARIANT* testArray, LONG* lResult)
    {
        // TODO: Add your implementation code here
        *lResult = testArray->parray->rgsabound->cElements;
        BSTR** StrPtr = 0;
        //LONG* pVals;
        long LowerBound = 0;
        long UpperBound = 0;
        int i;

        SafeArrayGetLBound(testArray->parray, 1, &LowerBound);
        SafeArrayGetUBound(testArray->parray, 1, &UpperBound);

        SafeArrayAccessData(testArray->parray, (void**)&pVals);

        for (i = LowerBound; i <= UpperBound; ++i)
        {
            BSTR* lVal = StrPtr[i];
            lVal++;
        }
        SafeArrayUnaccessData(testArray->parray);
        return S_OK;
    }
2

1 Answer 1

1

VBScript will not generate a SAFEARRAY with vartype VT_BSTR, which is what you are expecting. It will have VARTYPE VT_VARIANT.

// check all your parameters
if(testarray == null) return E_INVALIDARG;
if(testarray->vartype != VT_ARRAY|VT_BSTR
  && testarray->vartype != VT_ARRAY|VT_VARIANT)
    return E_INVALIDARG;
if(testarray->parray == null) return E_INVALIDARG;


// Now we have established we have an array, and that it
// is either a string array or a variant array.

VARTYPE vt = VT_EMPTY;
SafeArrayGetVarType(testarray->pArray, &vt);
// Now we need to take different actions based on the vartype.
if(vt == VT_BSTR){
    // we have an array of strings
    // Proceed as above.
}else if(vt == VT_VARIANT){
    // we have an array of variants, probably supplied by VBScript
    // Read them one by one and use VariantChangeType to get a string

}else{
    // We have some other array type we don't support
    return E_INVALIDARG;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.