Mega Code Archive

 
Categories / Delphi / Examples
 

Returnclassfromcomobject

Hi there- the simple way to do this is: 1) define more interfaces in the type library of your server, one for each class that you want to return (make sure these are dual) 2) add methods & properties etc to make those interfaces reflect the objects you want to pass back 3) modify your "GetClass" method to return an IDispatch 4) in the implementation of GetClass, simply set Result to the object you want to return. There are other ways, but they are a bit roundabout, and not as easy as this in Delphi. (e.g., you could design a custom object descendant which implemented a custom IDispatch by querying the RTTI for properties etc). At the risk of telling you things you know already, it's worth pointing out that there are two ways to call a method or property in COM, early and late binding. With early binding the call is made by jumping into a specified offset in the vtable of the COM object, and the only ways the COM runtime can know what the stack should look like before and after (i.e., what parameters should be provided & returned) are either if you provide a custom proxy which makes the calls (not very easy!) or if you provide a type-library with this info (this is the standard approach). In both these cases the type-library or proxy needs registering on the client machine as well as the server. The other way of calling a method or property in COM is via the IDispatch interface, also known as late binding. This works via run-time type discovery, i.e. the client queries the server at run-time to discover what methods and properties there are on the object, and what their parameters are. It can then call whichever it is interested in with the correct parameters etc. The point of this is that although you can't alter a type-library on the fly, it is possible either to build a smart proxy (which can then provide more run-time flexibility, but isn't very VB friendly) or to build a custom implementation of the IDispatch interface, which is potentially very VB friendly. Delphi provides a standard implementation of the IDispatch interface which works by looking at the type-library of the server to see what methods should be supported, and what their parameters are, but you could override that to be a bit more flexible- have a look at the code to see what it's up to. Probably this is more information than you wanted- I hope it's useful. Also if there are any COM experts out there who want to correct me, go ahead. cheers, Andrew Barber AIM