Neven MacEwan asked a funkly COM question:
> object COMa and COMb, COMb exposes a property a of 'type' COMa
>
> it works if I import the TL and use the CoClass create to
> explicitly create an instance of COMa of the type interface
> that COMb expects
> but if I 'Late bind' ie let COM work it all out it fails
OK, I'll have a stab at this one. COM doesn't have objects, it has
interfaces, and this makes a large difference to behaviour earily vs late
bound.
When you earily bind all of the typing is working exactly as expected
because Delphi is managing all the interfaces as their appropiate 'native'
types.
When you late bind Delphi must play some games with the typing. A Variant
type can only store two different types of interface, IUnknown and
IDispatch. So any time that you assign an interface into a variant delphi
must type coerse the interfaces somehow. Its turns out that the rules are
really simple for this coersion. If you assign an IDispatch then Delphi
stores that, and if you store anything else that Delphi will query the
interface IUnknown interface and store that.
The upshot of this is that unless you are working with IDispatch that all
interfaces stored in variants are IUnknowns. So when you try to assign
COMb.a = COMa
Delphi barfs because you can't to IDispatch property manipulation on the
IUnknown in COMb.
You can work arounf this if COMb inplements dual interfaces, that is both
the native interface and also IDispatch, but you'll need to do code like:
var
COMa, COMb: variant;
COMa := CreateOLEObkect('COMa.Function') as IDispatch;
COMb := CreateOLEObkect('COMb.Function');
COMb.a := COMa; // fails
and you'll also have to hope that the COMb.a property does appropiate type
coersion to query the appropiate interface that it wants when it gets an
IUnknown passed into it. So unless the COM objects are written to be
dispatched than you can't do late binding with them.
> also when you create an interface in the first method how can
> you free it.
> ie you set a varient to NULL but do you assign an interface nil?
Yes. Assiging nil to an interface is managed by Delphi so that interface
you had there has Release called on it. If you have no other references to
that interface then its going to destroy itself. This is one of the huge
advantages that Delphi has over C, and that is the automatic management of
interfaces and the AddRef and Release calls.
Cheers, Max.
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"