![]() |
off topic
|
Exposing Multiple Interfaces to Scripting ClientsThis page represents an attempt to capture the collective consciousness of the COM community. Of course, thoughtful feedback is welcome and encouraged (who the hell am I to voice the collective opinions of the entire COM community?). Once this is all sorted out, I anticipate never implementing IDispatch again ("Get COM+ now, I'll show you how!"). This page attempts to answer the question:
Why is this an issue? Because IDispatch is limited? No. It's a problem because current scripting clients don't support QI. Based on my observations of this list and my extensive conversations with members of the COM community, I see several solutions to this problem: 1. Don't try to expose the functionality of multiple interfaces to a scripting client.This is my favorite technique and was originally suggested to me by Keith Brown. He recommends using IDispatch to model the entire functionality of the object at a higher level than the individual interfaces, e.g.
interface IRect : IUnknown
HRESULT SetCoords([in] long left,
[in] long top,
interface I2DObject : IUnknown
// For scripting clients only
An implementation would use the dual strictly for IDispatch purposes, i.e. vtbl-binding clients wouldn't use _IRectangle:
class CRectangle : ...,
// IRect methods
// I2DObject methods
// _IRectangle methods
}; Since we're no longer using interface-based programming, this technique allows full control when implementing the single interface the scripting client will see w/o having to worry about mapping every method of all of the interfaces. By using the dual strictly as a means of implementing IDispatch and not exposing it, your IDispatch implementation can evolve as your object functionality evolves, i.e. using another dual w/ another IID. While it's still possible for a savvy developer to get a hold of the dual and try to implement it or derive from it, a slap on the forehead is generally enough to discourage this behavior. Unfortunately, nobody ever likes this technique because it forces them to provide client-specific implementation (which, btw, you'll likely to have to do anyway...). 2. Use the C++ MI trick.If you have n interfaces that are already [oleautomation] compatible, you can define a dual interface in IDL that is a union of all of the methods of all of the interfaces you'd like to expose methods from and let the C++ compiler match up the vtbls for you.
[ oleautomation ]
HRESULT SetCoords([in] long left,
[in] long top,
[ oleautomation ]
[ hidden, dual ]
// Copied from IRect
HRESULT SetCoords([in] long left,
[in] long top,
// Copied from I2DObject
} The implementation derives from all of the interfaces and implements the union of the methods. The C++ compiler will fill in the vtbl entries for the dual interface using the methods you are already implementing for your non-dual interfaces, e.g.
class CRectangle :
{
BEGIN_COM_MAP(CRectangle)
// IRect methods
// I2DObject methods
// _IRectangle methods already implemented! }; This method allows your clients to have access to the union of the methods of all of the interfaces that you have copied and pasted into your dual interface. However, now you're left with the copy 'n' paste dance in IDL and there's no way to resolve name conflicts, e.g. IRect and I2DObject both have a method Foo() that is each meant to do different things. This method also requires you to define your non-dual interfaces with scripting clients in mind. Finally, this method also exposes the dual in the TypeLib to savvy developers (see the aforementioned forehead-slap solution). 3. Use a typeinfo-driven implementation of IDispatch that provides a union of the methods of the scriptable interfaces.
Daniel Sinclair provides
another implementation of this technique called
MultiDual, described in this
online ReadMe.
Imagine the following IDL:
[ oleautomation ]
[ oleautomation ]
[ oleautomation ]
library TURNOFTHECENTURYLib
The client would like to write code like this: <script language=vbscript>
ace.paint '
unambiguously IArtist::Paint
ace.darn '
unambiguously ICurse::Darn
ace.icurse_shoot ' resolved to ICurse::Shoot because of
prefix
</script> The typelib-based table-driven implementation would use the coclass statement to map calls to GetIDsOfNames and Invoke to the appropriate interface definition, doing a little pre-processing along the way to provide non-colliding DISPIDs for non-default interface methods and handling the prefixes to perform fully-scoped name resolution. Because the implementation is completely table-deriven based on the coclass statement, an aggregatable implementation of IDispatch that provided this amalgam behaviour could be implemented that only depended on the object to expose IProvideClassInfo. In fact, some members of this list have provided implementations of similiar techniques w/o, unless I'm mistaken, the name resolution scheme I've proposed above. Assuming such an implementation of IDispatch, the object implementation could look like this:
class CAcePowell :
BEGIN_COM_MAP(CAcePowell)
COM_INTERFACE_ENTRY_AUTOAGGREGATE(IID_IDispatch, m_spunkDisp.p,
COM_INTERACE_ENTRY(IProvideClassInfo)
...
private:
Note: Assuming the implementation exposed the same DISPIDs for the methods and properties of the default interface as the default interface, early bound clients would be just as happy as late bound ones. And, because no dual interface is defined to implement IDispatch, there's no worry of a developer getting a hold of the interface definition for the dual interface directly (although a slap is still warranted for those that try). 4. Use a typeinfo-driven implementation of IDispatch that provides a collection of nested objects that implement the scriptable interfaces.Don and I implemented this technique at Tim's house. It's available here. Also, this has been updated by Serge Sandler for VC6 and for Win9x compatibility. Assuming the AcePowell IDL shown above, the client would like to write the following code (which performs the same as the previous client code): <script language=vbscript>
ace.paint ' top
level object has all methods of
dim artist
dim curse
dim cowboy
curse.shoot
</script> As you can see, we're exposing a collection of objects where the top level object has all of the methods and properties of the [default] interface as well a property to obtain an interface pointer on a sub-object for each of the oleautomation interfaces. The sub-objects are separate COM identities whose implementation of IDispatch simply forwards to the main identity via the specific interface in question. Each of the sub-objects also have one property per interface to get to the other interfaces, thus simulated the rules of COM identity as exposed via QI. This allows us to build an invocation model very similar to VB's where QI is an assignment and references of type class mean references to the [default] interface. Parts of the implementation for this scheme have also been posted w/o, unless I'm mistaken, the full simulated QI as I propose above. The nested implementation of IDispatch could also be fully typelib-drived based on an object's implementation of IProvideClassInfo and aggregated, e.g. using a clsid like CLSID_StdNestedDispatch. One alternative syntax that might be a bit more flexible is the following: <script language=vbscript>
ace.paint ' top
level object has all methods of
dim artist
dim curse
dim cowboy
if not curse is nothing then
</script> This syntax makes it even more clear that we're doing COM, it only requires one additional property (interface) and it's easier to handle the QI failure case. 5. Use a separate object to perform QI.This method is made real in an implementation by Dave Rogers, available at http://www.combatcom.com/adminstore/component-warehouse/thedispadapter.htm and Valery Pryamikov, available at http://home.sol.no/~valery Instead of building the pseudo-QI into the object itself, Valery uses another another object to perform the QI. This allows the client code to look like this: <script language=vbscript>
' Requires object to implement
IProvideClassInfo or IPersist
' Doesn't require
IProvideClassInfo or IPersist
Dim artist
Dim curse
Dim cowboy
If not curse is nothing then
</script> This technique requires another object to perform the QI, so it seems a little weird, but the good news is that it works with existing objects. No recompile necessary! Now What?Given those techniques of implementing IDispatch to expose the functionality of multiple interfaces, the first two require no additional tool support. You can use them today. The last two, amalgam dispatch and nested dispatch, represent two models of exposing the functionality of multiple interfaces to a scripting client. Either model may be appropriate, but each is conceptually separate, i.e. the object implementor would pick one or the other. After writing the client code, I find I really like technique 4 best. It looks like COM to me. However, I can see that some folks would prefer technique 3 or 5. So, what do we do now? Do we wait for MS to ship COM+? We won't see meta-information driven scripting until the turn of the millennia. Do we wait for MS to implement CLSID_StdAmalgamDispatch and CLSID_StdNestedDispatch? Those guys are pretty busy with COM+, so I won't hold my breath. Are we sure of the ramifications and usage of amalgam dispatch and nested dispatch? I'm pretty sure, but not completely so. If you think I'm all wrong (or even just a litte), let me know. On the other hand, if there are enough folks that are excited about a typelib-driven implementation of IDispatch, we can get it done. I've dedicated this web page to collecting and distributing the pieces. If folks have some source to contribute, I'll use it to build the two implementations. If folks have full implementations, I'm happy to post those as well. Please, let's stop implementing IDispatch so I can stop thinking about the worthlessness of duals. Thanks. BTW, want to know the real reason we have duals? It saves an vptr. Doh! |
||||||||||
|
home & news
DSL DevCon
interviews
tools
the spout
writing
fun
colophon
news
contact
off topic
Ads: text links build a website best web hosting White Noise Web Optimization VMOptions Web Directory free software downloads termite control recommendation software Web Hosting Reviews payday loans Internet Marketing Software Authority Web Directory buy backlinks Bathrooms Online Tutoring Jobs thermal paper lead management This page is copyright (c) 1995-2009, Chris Sells. All rights reserved. No warranties extended. Some assembly required. Void where prohibited. You may link to this site freely from your own site. You may quote small excerpts from this site, but please include a link to the original source on this site. |
|||||||||||