Re: IDispatch question ...
Stuart Redmann wrote:
>>2b|!2b==? wrote:
My question then, is: how may I obtain the IDispatch* for the VB COM
object, so that I can pass it to the above function ?.
Jochen Kalmbach [MVP] wrote:
FOr example:
CLSID clsid;
LPUNKNOWN punk;
LPDISPATCH pdisp;
OleInitialize(NULL), if OLE isn't already loaded.
CLSIDFromProgID(progID, &clsid);
CoCreateInstance(clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (LPVOID FAR*)&punk);
punk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&pdisp);
punk->Release();
2b|!2b==? wrote:
Few more questions if you don't mind - I'm quite new at OLE/COM:
1). Can I use CLSIDFromString instead of CLSIDFromProgID ?
Of course.
2). I take it that pdisp holds the adress of the IDispatch interface
that I can now pass to the method that expects an IDispatch*?
You're right.
3). Is it safe to use pdisp AFTER punk->Release() ?
Yes. As the QueryInterface mechanism returns (on success) a pointer that
holds one reference to the object, you can use it until you release this
reference (You'll certainly be able to use it even further if there are
other clients that hold a reference to this object, but this is regarded
as very bad practise. COM mandates not to use any pointers that have
been Released).
4). If I call punk->Release() - presumably i am freeing memory
previously allocated
Nope. As there is still one 'client' to object B, the pdisp pointer,
object B has still a reference count of one after punk is released.
Lets have a closer look at the code: I have added a number at the
beginning of each line which tells the reference count of object B
_before_ the line in question is executed. A minus sign means that the
object isn't created yet.
- LPUNKNOWN punk;
- LPDISPATCH pdisp;
-
- OleInitialize(NULL), if OLE isn't already loaded.
- CLSIDFromProgID(progID, &clsid);
- CoCreateInstance(clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (LPVOID FAR*)&punk);
1 punk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&pdisp);
// Note that two references to B are help now: one through
// pointer punk and one through pdisp.
2 punk->Release();
1
Regards,
Stuart
Many thanks for the clarification Stuart, Jochen. One last question If I
may (this is more to do with validating the code I have written so far,
than the original question of this post - which you have thankfully,
both made very clear):
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)
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. 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 ?