Calling COM functions using IDispatch->Invoke(...
Hi, everybody
I've got to call some COM server functions. It appears, that the only
way I can do it =96 using IDispatch. After I've imported type library I
can see the prototypes for some functions. For example:
HRESULT Start( _bstr_t Path, const _variant_t & Param );
Here is my code:
HRESULT hr=1;
IDispatch *pIDispatch = NULL;
CLSID clsid;
CLSIDFromProgID(L" SomeCOM.SomeInterface ", &clsid);
hr = CoInitialize(NULL);
if( SUCCEEDED(hr) )
(
hr = CoCreateInstance( clsid,
NULL, CLSCTX_LOCAL_SERVER,
IID_IDispatch,
(void**)&pIDispatch
);
if( SUCCEEDED(hr) )
{
DISPID dispid;
OLECHAR FAR* szMemberName = L"Start";
hr = pIDispatch->GetIDsOfNames(IID_NULL, &szMemberName, 1,
LOCALE_SYSTEM_DEFAULT, &dispid);
if( SUCCEEDED(hr) )
{
VARIANT varResult;
DISPID dispidNamed = DISPATCH_METHOD;
EXCEPINFO excep;
UINT uArgErr;
DISPPARAMS dispparams;
dispparams.cNamedArgs = 0;
dispparams.cArgs = 2;
dispparams.rgdispidNamedArgs = &dispidNamed;
dispparams.rgvarg = new VARIANTARG[2];
dispparams.rgvarg[0].vt = VT_ERROR;
dispparams.rgvarg[0].lVal = DISP_E_PARAMNOTFOUND;
dispparams.rgvarg[1].vt = VT_BSTR;
dispparams.rgvarg[1].bstrVal = SysAllocString(L=94SomePath=94);
hr = pIDispatch->Invoke( dispid, IID_NULL,
LOCALE_SYSTEM_DEFAULT,
DISPATCH_METHOD,
&dispparams,
&varResult,
&excep,
&uArgErr
);
if( SUCCEEDED(hr) )
{
//invoke succeeded
..........
}
else
{
//invoke failed
// I'm getting in here ..
}
}
}
And I'm always getting invoke failed.
GetLatestError() returns 0.
If I'm using Alf's P. Steinbach code, which he very kindly supplied in
my previous post, for some wrapper class for IDispatch, calling of
this method also fails, but I'm able to get some strange error,
something like =93=85String is too long=85=94
So is possible to tell me what I'm doing wrong? Am I setting
"dispparams" incorrectly?
By the way in VB it works just fine as
Dim oI As Object
oI = CreateObject("SomeCOM.SomeInterface")
oI. Start(=93SomePath=94)
But I'm using STL library for handling of the data, so I really would
like to be able to call this COM functions directly from my C++
application.
Thanks in advance
Alex