Today I am testing the IDispatch of a COM object. The method has the following fingerprint:

[id(6)] HRESULT DataMatrixEncodeSet(
        [in] BSTR strDateToEncode,
        [in] LONG sizeID,
        [out,retval] LONG* chunks);

I was quite surprise that I got an “type mistach” error from Invoke function. My code is as below:

args[0].vt = VT_BSTR;
args[0].bstrVal = SysAllocString(L"this is data to encodeencodeencodeencodeencode");
args[1].vt = VT_I4;
args[1].lVal = 0;

dp.cArgs  = 2;   
dp.rgvarg = args;   
dp.cNamedArgs = 0;   
dp.rgdispidNamedArgs = NULL;

VARIANT vaRet;
VariantClear(&vaRet);
EXCEPINFO  exInfo;
unsigned int uErr;
hr = disp->Invoke(dispID[0], IID_NULL, LCID_NEUTRAL,
        DISPATCH_METHOD, &dp, &vaRet, &exInfo, &uErr);
BOOST_CHECK(SUCCEEDED(hr));

After some search, I finally found the cause: the arguments packed in DISPPARAMS must be in reverse order. In other words, the args[] array must be reversed. After I changed that, the error disappeared.