Re: IDispatch question ...
2b|!2b==? wrote:
I am currently using a different sequence of calls to invoke my C++ COM
class. My existing code that initializes the C++ COM class (called A)
looks somewhat like this:
try
{
HRESULT hr;
CoInitialize(NULL);
//Note: m_classA was declared as type IClassA (because I had seen an
example somewhere do that)
I guess that you meant that m_classA is of type IClassAPtr. Else the
call m_classA.CreateInstance would not make sense. In this case we would
be talking about smart pointers instead of (plain) old interface pointers.
hr = m_classA.CreateInstance( __uuidof(IClassA) );
}
catch (_com_error& comError){ throw
(std::string((char*)comError.Description()) ) ; }
I would much prefer to use the approach you are using - because I assume
that it is better than my approach.
Wrong assumption. Dealing directly with (plain) COM pointers is quite
messy and error-prone. I strongly advise you to use smart pointers
instead of raw COM pointers, as they take care of a lot of things.
The code you have posted should do the trick:
IClassAPtr m_classA;
m_classA.CreateInstance( __uuidof(IClassA) );
If you like inside the CreateInstance method of the template _com_ptr_t
(which stands behind all COM smart pointers), you'll see that this
method is performing exactly the steps that are necessary to instantiate
an object with the given class ID and retrieve the requested interface
pointer. It also takes care of in-proc versus out-proc issues, and even
makes a call to the OleRun function. All this logic is hard to implement
by hand, and also messes up the code you really want to write (after all
you want to write code to deal some problem, not with COM).
> So this is how I propose to modify
my code. Please point out any glaring ommisions I might make, or any
gotchas that I need to be aware of:
try
{
CLSID clsid;
LPUNKNOWN punk;
LPDISPATCH pdisp;
OleInitialize(NULL); // if OLE isn't already loaded.
CLSIDFromProgID(L"ObjectFarm.ClassA", &clsid);
CoCreateInstance(clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (LPVOID FAR*)&punk);
// From this point on, its not clear how I get a reference to ClassA ,
// so that I can call its methods directly like m_classA->foo();
// There are two interfaces I want to use (copied from .idl file) :
// (i) interface IClassA : IDispatch
// (ii) dispinterface _IClassAEvents
// Could you please flesh out the details as to how I may be able to
// obtain a reference to the class via the 2 interfaces and invoke
// the methods on each of the interfaces above ?
}
catch (_com_error& comError){ throw
(std::string((char*)comError.Description()) ) ; }
//^^ Finally am I handling errors above correctly ?? - or is there a
better way ?
Catching _com_errors makes only sense when you work with smart pointers
(the plain old COM API lets you deal with HRESULTS). _com_errors are
only thrown if you want to access a smart pointers whose underlying
plain COM pointer is pointing to NULL (so you avoid a memory access
violation).
Regards,
Stuart