April 17, 2003 tools

Exposing Multiple Interfaces to Scripting Clients

This 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:

“How do I expose multiple interfaces to scriping clients?”

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 GetCoords([out] long* pleft, [out] long* ptop,
                      [out] long* pright, [out] long* pbottom);

    HRESULT SetCoords([in] long left, [in] long top,
                      [in] long right, [in] long bottom);
}

interface I2DObject : IUnknown
{
    HRESULT Inflate([in] long cx, [in] long cy);
    HRESULT Translate([in] long cy, [in] long cy);
}

// For scripting clients only
[ hidden, dual ]
interface _IRectangle : IDispatch
{
    [propput] HRESULT Left([in] long left);
    [propget] HRESULT Left([out, retval] long* pleft);
    …
    HRESULT Inflate([in] long cx, [in] long cy);
    HRESULT Translate([in] long cy, [in] long cy);
}

An implementation would use the dual strictly for IDispatch purposes, i.e. vtbl-binding clients wouldn’t use _IRectangle:

class CRectangle : …,
    public IRect,
    public I2DObject,
    public IDispatchImpl<_IRectangle, &IID__IRectangle,
                         &LIBID_SHAPESLib>
{
public:
BEGIN_COM_MAP(CRectangle)
    COM_INTERFACE_ENTRY(IRect)
    COM_INTERFACE_ENTRY(I2DObject)
    COM_INTERFACE_ENTRY(IDispatch)
    // No entry for _IRectangle
END_COM_MAP()

    // 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 ]
interface IRect : IUnknown
{
    HRESULT GetCoords([out] long* pleft, [out] long* ptop,
                      [out] long* pright, [out] long* pbottom);

    HRESULT SetCoords([in] long left, [in] long top,
                      [in] long right, [in] long bottom);
}

[ oleautomation ]
interface I2DObject : IUnknown
{
    HRESULT Inflate([in] long cx, [in] long cy);
    HRESULT Translate([in] long cy, [in] long cy);
}

[ hidden, dual ]
interface _IRectangle
{

    // Copied from IRect
    HRESULT GetCoords([out] long* pleft, [out] long* ptop,
                      [out] long* pright, [out] long* pbottom);

    HRESULT SetCoords([in] long left, [in] long top,
                      [in] long right, [in] long bottom);

    // Copied from I2DObject
    HRESULT Inflate([in] long cx, [in] long cy);
    HRESULT Translate([in] long cy, [in] long cy);

}

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 :
    …,
    public IRect,
    public I2DObject,
    public IDispatchImpl<_IRectangle, &IID__IRectangle,
                         &LIBID_SHAPESLib>

{
public:

BEGIN_COM_MAP(CRectangle)
    COM_INTERFACE_ENTRY(IRect)
    COM_INTERFACE_ENTRY(I2DObject)
    COM_INTERFACE_ENTRY(IDispatch)
    // No entry for _IRectangle
END_COM_MAP()

    // 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.
Herbert Carroll provides a similar implementation, but not based on typeinfo.
Kjell Tangen provides another solution for by implementing a extended version of CComTypeInfoHolder.

Imagine the following IDL:

[ oleautomation ]
interface ICowboy : IUnknown
{
    HRESULT Draw([in] long nSpeed); // Conflicts with IArtist::Draw
    HRESULT Shoot();                 // Conflicts with ICurse::Shoot
}

[ oleautomation ]
interface IArtist : IUnknown
{
    HRESULT Draw([in] long nStrokes); // Conflicts with ICowboy::Draw
    HRESULT Paint();
}

[ oleautomation ]
interface ICurse : IUnknown
{
    HRESULT Shoot(); // Conflicts with ICowboy::Shoot
    HRESULT Darn();
}

library TURNOFTHECENTURYLib
{
    coclass AcePowell
    {
        interface ICowboy;
        [default] interface IArtist;
        interface ICurse;
    }
}

The client would like to write code like this:

<script language=vbscript>

    ace.paint    ′ unambiguously IArtist::Paint
    ace.draw 100 ′ resolved to IArtist::Draw because
                 ’IArtist is [default]

    ace.darn    ′ unambiguously ICurse::Darn
    ace.shoot   ′ resolved to ICowboy::Shoot because ICowboy
                ′ comes before ICurse in the coclass statement

    ace.icurse_shoot    ′ resolved to ICurse::Shoot because of prefix
    ace.icurse_darn     ′ prefix unnecessary but still works

</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 :
    …,
    public IProvideClassInfo
{

BEGIN_COM_MAP(CAcePowell)
    // Appropriate entries for nested composition of
    // ICowboy, IArtist and ICurse

    COM_INTERFACE_ENTRY_AUTOAGGREGATE(IID_IDispatch, m_spunkDisp.p,
                                      CLSID_StdAmalgamDispatch)

    COM_INTERACE_ENTRY(IProvideClassInfo)
END_COM_MAP()

    …

private:
    CComPtr<IUnknown> m_spunkDisp;
};

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. You can download our implementation. 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
                 ′ [default] interface

    dim artist
    set artist = ace.iartist ′ Can “QI” for specific interface
    artist.draw 100

    dim curse
    set curse = artist.icurse ′ Simulated QI
    curse.darn

    dim cowboy
    set cowboy = curse.icowboy ′ Simulated QI
    cowboy.shoot

    curse.shoot
    curse.darn

</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
                 ′ [default] interface

    dim artist
    set artist = ace.interface(“iartist”)

    if not artist is nothing then artist.draw 100

    dim curse
    set curse = artist.interface(“icurse”)
    if not curse is nothing then curse.darn

    dim cowboy
    set cowboy = curse.interface(“icowboy”)
    if not cowboy is nothing then cowboy.shoot

   if not curse is nothing then
        curse.shoot
        curse.darn

    end if

</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
    set dispenum.ObjectClass = ace

    ′ Doesn’t require IProvideClassInfo or IPersist
    ′ Note: Could use CLSID instead of ProgID
    dispenum.SetCLSIDObjectClass(ace, “Ace.AcePowell.1”)

    Dim artist
    Set artist = dispenum(“iartist”) ′ Use disenum to perform QI
    If not artist is nothing then artist.draw 100

    Dim curse
    Set curse = dispenum(“icurse”)
    If not curse is nothing then curse.darn

    Dim cowboy
    Set cowboy = dispenum(“icowboy”)
    If not cowboy is nothing then cowboy.shoot

   If not curse is nothing then
        Curse.shoot
        Curse.darn
    End if

</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 a vptr. Doh!

This article is translated to Serbo-Croatian by Anja Skrba from Webhostinggeeks.com.

April 17, 2003 spout

XML For Humans

When I sat down to write this piece, I didn’t get why the work that Don, Gudge, KeithBa, Yasser, Tim and ChrisAn is doing is so amazing. I mean, I know that they’re smart and I know that they’re building the hell out of their stuff, but why? It seems like everyone that gets into blogging starts by writing their own blogging engine. It’s like C programmers writing editors, C++ programmers writing string classes and XML programmers writing XSD<->Class codegen. Sure, it’s a useful learning tool, but earth-shattering? Worth that much energy from that many guys? When they’re done, what great problems will be solved?

Don’t get me wrong. I love the that the web makes it easy for anyone to not only publish but also get an audience. I also love that there’s a protocol (RSS) that lets me subscribe to my favorite freelance authors in any one of dozens of tools (I’m into SharpReader today). I even like the Dear Diary style of writing because it leads itself to thoughts, feelings and insights that give me greater understanding of not only the topic but the author. As an added bonus, blog entries have turned into everything that’s good about mailing lists w/o the endless angle brackets that remove the need to write in coherent sentences. Blogs feel like the kind of democracy we had that lead to the American Revolution when the world was re-shaped with big ideas, written by great authors.

But does the world need yet another blog engine? Personally, while it’s primitive, I find that FrontPage have served me well as a blogging tool. The mental overhead is small, the development is nil and I get red squiggles. It falls down sometimes, but I’ve spent far less time maintaining the infrastructure for my content than I would if I were to build my own blogging engine.

On the other hand, while I don’t feel the need for another blogging engine in the world, I definitely see value in what those guys are doing. Specifically, they’re building apps, which is not something that XML guys are traditionally into. In fact, I’m scratching my head trying to think of another kind of XML-based technology that isn’t infrastructure-based and I’m not coming up with one (even InfoPath is a dev-tool). Is RSS really the first mainstream use of XML that hasn’t nothing to do with technology for it’s own sake? Are blogs the killer app for XML? I know web services were supposed to fill that role, but while they do solve a real problem, they’re not for consumers. My aunt in FLA couldn’t make use of a web service. However, she could definitely subscribe to an RSS feed and read up on the local news, her job and her hobbies.

Of course, the beauty of having infrastructure guys building apps is that they get to put into practice what they preach. When stuff is a pain (like every single XML API ever invented and most XML vocabularies), they feel the pain. The difference between us and them is that instead of just wrapping a layer of abstraction around the ugly stuff like we have to, they can actually fix it. I started this piece wanting to shake those guys until they could tell me why they cared so much. How ironic that thinking through the issues in the very medium they’re digging into helped me figure out what they were up to. The blog engine work is but the vehicle. They’re riding the app train to enlightenment. They’re trying to understand how XML can be used to solve problems for real humans. How can I argue with that?

April 16, 2003 .net

Essential .NET Nominated for Reader’s Choice Award

If you liked Don's Essential .NET (I know I did), then you should vote for it on the .NET Developer's Journal Reader's Choice Awards. You also get the opportunity to vote in all kinds of other categories besides books.
April 15, 2003

Make XML More Readable

I didn't know that XmlTextWriter could format your code for you. I've written the code to do it myself. Doh! Is there nothing that the .NET Framework Class Library authors haven't already thunk up?!?
April 15, 2003 .net

C# Programmers Out-Earn VB.NET Programmers by 26%

Visual Studio Magazine posts the results of their latest salary survey. Average Visual Studio programmer salary is $76K, up 12% from last year. Unfortunately, layoffs and such like are still the order of the day.
April 14, 2003

Microsoft CRM Software Development Kit (SDK)

From Ramon: This SDK provides the information necessary for developers to integrate their applications into Microsoft CRM or to build a vertical solution on the Microsoft CRM platform. An architectural overview includes descriptions of the object and security models, as well as the integration points. The API references are documented and the database schema is provided. There is a “How To” section with sample code.
April 14, 2003 spout

Priceless

  • Running sellsbrothers.com for one month: $150
  • Round-trip ticket from OR to FL to give a 90-minute talk: $1200
  • My son picking me for his “famous person” school report: priceless

I don’t know how my son picked up on the tiny bit of fame I’ve acquired amongst a small, strange (but loveable!) group of people, but he did and now he thinks I’m cool. I’m sure it won’t last, but I plan to enjoy it while it does. : )

April 12, 2003 fun

The SellsBrothers Little League Team

The SellsBrothers Little League Team

Here’s the SellsBrothers Beaverton Area Little League team looking very serious. Notice the SellsBrothers logo on the hats. Having your own company definitely has it’s perks. : )

Here’s the SellsBrothers team looking more natural.

Beaverton, OR
Saturday, April 12, 2003


← Newer Entries Older Entries →