Search code examples
c++ccommfcactivex

com: Invalid number of parameters when pDispatch->invoke()


For some reason I must use a COM control in my code. I want to invoke a method exported by the COM control.

idl file:

methods: 
    [id(1)] boolean Open(BSTR filepath, BSTR rootname);

cpp file:

VARIANT pVarResult;
EXCEPTINFO pExcepInfo;
unsigned int puArgErr;
DISPPARAMS pParams;
memset(&pParams, 0, sizeof(DISPPARAMS));

VARIANTARG param[2];
param[0].vt = VT_BSTR;
param[0].bstrVar = filepath.AllocSysString();
param[1].vt = VT_BSTR;
param[1].bstrVar = rootname.AllocSysString();

pParams.rgvars = param;
pParams.cArgs = 2;
hResult = pDispatch->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,  
             DISPATCH_METHOD, &pParams, &pVarResult, &pExcepInfo, &puArgErr);

I get a error say "Invalid number of parameters". Where is wrong of my code? How to pass two strings params to a interface defined in a com control? It seems impossible.

EDIT 1: add more return info. It seems useless.

pVarResult = NULL;
pExcepInfo.wcode = 52424;
puArgErr = 1;

Solution

  • Thanks everyone for you time.
    It's my stupid mistake to cause this problem. Because I use MFC library to implement the COM control. And in MFC framework, if you want to export a interface, you must use a macro to declare it. like this.

    DISP_FUNCTION(MyCtrl, "Open", Open, VT_BOOL, VTS_BSTR VTS_BSTR)
    

    What I write is:

    DISP_FUNCTION(MyCtrl, "Open", Open, VT_BOOL, VTS_BSTR, VTS_BSTR)
    

    The comma should not be added between two VTS_BSTR.