Log in

IDispatch Invoke Dispparams - Toronto Dude

> Recent Entries
> Archive
> Friends
> Profile

Toronto Apartments for Rent

October 13th, 2010

Previous Entry Share Next Entry
01:58 am - IDispatch Invoke Dispparams
In one of my project I needed to do some old school stuff that I haven't been doing in over 10 years, that is to call a COM object using IDispatch Invoke with parameters to pass.

I had to go all over the place to figure things out, but here is an example for those looking. Just keep in mind that the parameters in the variant array are in reverse (last parameter is with index 0)

        LPOLESTR szMember = L"MethodName";
        DISPID dispid;
        hr = pdisp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid) ;
        //setup the variant
        DISPPARAMS dispparams;
        memset(&dispparams, 0, sizeof dispparams);
        dispparams.cNamedArgs = 0;
        dispparams.cArgs = 3;
        dispparams.rgvarg = new VARIANTARG[dispparams.cArgs];
        dispparams.rgvarg[2].vt =  VT_BSTR;
        dispparams.rgvarg[2].bstrVal =  SysAllocString(L"value1");
        dispparams.rgvarg[1].vt =  VT_BSTR;
        dispparams.rgvarg[1].bstrVal =  SysAllocString(L"value2");
        dispparams.rgvarg[0].vt =  VT_I2;
        dispparams.rgvarg[0].iVal =  value3;

//Make the call
        _variant_t varResult;
        EXCEPINFO *pExcepinfo = NULL;
        unsigned int *puArgErr = NULL;
        hr = pdisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparams, &varResult, pExcepinfo, puArgErr);

Happy coding!

( | Leave a comment)

> Go to Top