problem with Invoke, help me please.


2005-07-14 03:53:16 PM
cppbuilder44
Hi,
I'm trying to invoke a dispatch interface method with the function
"Invoke", but I'm falling in a lot of trouble...
It's all right with CoInizialize and CoCreateInstance, but Invoke
seems don't work. Here's the prototype of the function that I want to
call:
long CreateGrid(short ncol, short nrow);
And here's the code :
int main( int argc, char * argv[] )
{
CLSID idOgg={0xbb5db54e, 0xbae2,
0x48bf,{0xb0,0xbd,0x2f,0xd3,0xd9,0xf8,0xeb,0x08}};
CLSID idInter={0xbb5db54c, 0xbae2,
0x48bf,{0xb0,0xbd,0x2f,0xd3,0xd9,0xf8,0xeb,0x08}};
// Initialize COM
_DSpectivaWeb *spect;
IDispatch *ptr ;
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
cout<<"CoInitialize failed"<<endl;
return 0;
}
hr = CoCreateInstance(idOgg, NULL, CLSCTX_SERVER,idInter, (void **)
&ptr);
if (FAILED(hr))
{
cout<<"CoCreateInstance failed"<<endl;
return 0;
}
hr=ptr->QueryInterface(idInter,(void **) &spect);
if (FAILED(hr))
{
cout<<"Query interface failed"<<endl;
return 0;
}
DISPID id2 ;
OLECHAR *str = OLESTR ( "CreateGrid" ) ;
spect->GetIDsOfNames ( IID_NULL, &str, 1, GetUserDefaultLCID( ), &id2 )
;
cout<<"id2 "<<id2<<endl;
EXCEPINFO FAR *pExcepInfo = NULL;
unsigned int FAR puArgErr = NULL;
DISPPARAMS dispparams;
VARIANT dispRes;
VariantInit (&dispRes ) ;
VARIANTARG var[2];
VariantInit(&var[0]);
VariantInit(&var[1]);
DISPID id=4;
var[0].vt=VT_I2;
var[0].iVal=1;
var[1].vt=VT_I2;
var[1].iVal=1;
dispparams.rgvarg=var;
dispparams.cArgs=2;
dispparams.rgdispidNamedArgs=NULL;
dispparams.cNamedArgs=0;
dispRes.lVal=-1;
hr=spect->Invoke(id2,IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_METHOD,&dispparams,&dispRes,pExcepInfo,&puArgErr);
if ( puArgErr!=NULL)cout <<"The first index of rgvarg whiche take
error is :"<<puArgErr<<endl;
if (FAILED(hr))
{
cout<<"Error invoke"<<endl;
//return 0;
}
if (hr==S_OK){
cout<<"Chiamata effettuata"<<endl;
cout<<"valore res= "<<dispRes.lVal<<endl;
}
if (hr==DISP_E_BADPARAMCOUNT)cout<<"primo"<<endl;
else{
if (hr==DISP_E_BADVARTYPE)cout<<"secondo"<<endl;
else{
if (hr==DISP_E_EXCEPTION)cout<<"terzo"<<endl;
else{
if (hr==DISP_E_MEMBERNOTFOUND)cout<<"quarto"<<endl;
else{
if (hr==DISP_E_NONAMEDARGS)cout<<"quinto"<<endl;
else{
if (hr==DISP_E_OVERFLOW)cout<<"sesto"<<endl;
else{
if (hr==DISP_E_PARAMNOTFOUND)cout<<"sett"<<endl;
else{
if (hr==DISP_E_TYPEMISMATCH)cout<<"ott"<<endl;
else{
if
(hr==DISP_E_UNKNOWNINTERFACE)cout<<"nono"<<endl;
else{
if (hr==DISP_E_UNKNOWNLCID)cout<<"dec"<<endl;
else{
if
(hr==DISP_E_PARAMNOTOPTIONAL)cout<<"undic"<<endl;
}
}
}
}
}
}
}
}
}
}
spect->Release();
return 1;
}
Note that GetIDsOfNames return the right id of CreateGrid so I think
that the code acceded correctly to the interface.
I tried also to skip the call to QueryInterface, because I inserted the
clsid of the interface that I want directly into CoCreateInstance, but
still not work.
The strange thing is in HRESULT hr because, when I call Invoke, it
doesn't return any of the value that it should return but I get the
error only if check FAILED(hr).
Another thing, it's strictly necessary to insert the file .c that I
created from MIDL?The .h isn't enough?
Thank's to everyone and sorry for my english.
Regards
Riccardo Corona.