![]() |
How Do I Find The IDispatch Of An OCF Automated Object?There are occasions when it is necessary to access the IDispatch of an Automated Object directly, but there is no mechanism in the OCF library to facilitate this. For example suppose you have two automated classes A and B, and A need to access B as a property and vice versa. There is now no way you can have B as a property of A without first defining B, but since A is a property of B also, A needs to be defined first. One way out of this is for the class that is defined first in the headers to define the property that accesses B as TAutoDispatch rather than TAutoObject<B>, the problem now is reduced to finding the IDispatch of B at run time. The following template code is designed to allow access to the IDispatch of any OCF automated object, and has many uses in addition to that given above. N.B. Must be used within the same module as class T, T::ClassInfo has only file scope linkage. Function definition: template <class T> IDispatch* GetDispatchOfObject(TRegistrar* Reg, T& Obj, TObjectDescriptor::TDestruct type); Obj: reference to the C++ OCF automated object. type: Type of the object, can be.. TObjectDescriptor::Quiet // automation object quietly goes away without notice. TObjectDescriptor::Delete // automation object deletes the C++ object serviced. TObjectDescriptor::PostQuit // automation object posts a quit message to application Function Body. template <class T>
IDispatch* GetDispatchOfObject(TRegistrar* Reg, T& Obj, TObjectDescriptor::TDestruct type)
{
TObjectDescriptor Disc(&Obj, T::ClassInfo, type);
TAppDescriptor& appDesc = Reg->GetAppDescriptor();
IDispatch* pdisp = (IDispatch*)appDesc.FindServed(Disc);//does AddRef if successful
if(pdisp == 0)
{
// The TServedObjectCreator is owned and deleted by OCF, not by us
TServedObjectCreator* creat = new TServedObjectCreator(appDesc);
pdisp = creat->CreateDispatch(Disc);
}
return pdisp;
}
Copyright Dr J. Maddock. Last Updated: 23 May 1997 |