Click here to Skip to main content
15,867,965 members
Articles / Desktop Programming / ATL
Article

Resolving Variants

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Jul 2001 65.4K   2   22   1
Dereferincing and resolving Variants and IDispatch

Introduction

I have created a recursive function to dereference a passed VARIANT ([in]) and to resolve IDispatch if the contained variable in the passed VARIANT is of type VT_DISPATCH.

I could use VariantCopy and/or VariantCopyInd, but I wanted to avoid copying arrays or other types that are not expected.

Please take a look at the my function ResolveVariant() (sorry if it is long) and let me know if you find any problems. I have done some testing and I dont see any problems (including ref. counting).

HRESULT SomeClass::get_Item(VARIANT Index, VARIANT* pVar)
{
    VARIANT varOut;
    VariantInit(&varOut);
    HRESULT hr = ResolveVariant(&Index, &varOut);

    if (!FAILED( hr )
    {
        switch (varOut.vt)
        {
        case VT_BSTR:
            SomeCodeHere to prepare pVar;
            break;

        case VT_I4:
            SomeCodeHere to prepare pVar;
            break;

        default:
            break;
        }



        VariantClear(&varOut);


    }
    else
        return hr;

}


HRESULT ResolveVariant(VARIANT* pVarIn, VARIANT* pVarOut)
{
    VARIANT* tmpVarIn = pVarIn;
    ULONG ByRef = 0;

    while(tmpVarIn->vt == (VT_BYREF | VT_VARIANT) )
        tmpVarIn = tmpVarIn->pvarVal;

    if(tmpVarIn->vt & VT_BYREF)
        ByRef = VT_BYREF;


    switch(tmpVarIn->vt)
    {
    case (VT_I2): case (VT_BYREF|VT_I2):
        if(ByRef)
        {
            if(!tmpVarIn->piVal) return E_POINTER;
            pVarOut->iVal = *(tmpVarIn->piVal);
        }
    break;

    case (VT_I4): case (VT_BYREF|VT_I4):
        if(ByRef)
        {
            if(!tmpVarIn->plVal) return E_POINTER;
            pVarOut->lVal = *(tmpVarIn->plVal);
        }
        break;

    case (VT_BSTR): case (VT_BYREF|VT_BSTR):
        if(ByRef)
        {
            if(!tmpVarIn->pbstrVal) return E_POINTER;
            if(!*(tmpVarIn->pbstrVal) ) return E_POINTER;
            pVarOut->bstrVal = *(tmpVarIn->pbstrVal);
        }
        break;

    case (VT_DISPATCH): case (VT_BYREF|VT_DISPATCH):
        if(ByRef)
        {
            if(!tmpVarIn->ppdispVal) return E_POINTER;
            if(!*(tmpVarIn->ppdispVal) ) return E_POINTER;
            pVarOut->pdispVal = *(tmpVarIn->ppdispVal);
        }
        break;


    default:
        return DISP_E_TYPEMISMATCH;
    }

    if(ByRef)
        pVarOut->vt = (tmpVarIn->vt - ByRef);
    else
        *pVarOut = *tmpVarIn;


    if (pVarOut->vt == VT_DISPATCH)
    {
        VARIANT varResolved;
        DISPPARAMS  dispParamsNoArgs = {NULL, NULL, 0, 0};

        VariantInit(&varResolved);

        if ( SUCCEEDED

            pVarOut->pdispVal->Invoke(DISPID_VALUE, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYGET | DISPATCH_METHOD,
            &dispParamsNoArgs, &varResolved, NULL, NULL) ) )
        {
            ResolveVariant(&varResolved, pVarOut);
        }
        else
            return E_FAIL;

        VariantClear(&varResolved);
            return S_OK;
    }
    else
    {
        HRESULT hr;
        VARIANT retVar;
        VariantInit(&retVar);
        hr = VariantCopy(&retVar, pVarOut);
        *pVarOut = retVar;
        return hr;
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Ecuador Ecuador
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCannot resolve IDispatch value Pin
Ning Ni1-Dec-01 7:11
Ning Ni1-Dec-01 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.