Ray's playground

 

The Anatomy of a COM Server(Chapter 2 of COM and .NET Interoperability) part2

  The final topic of this chapter is to refresh the role of IDispatch. As you have just seen, C++ and Visual Basic 6.0 are able to exercise early binding when making use of COM types. Early binding describes the process of understanding the calling conventions for a given interface method at compile time rather than at runtime. Using this (preferred) technique, a COM client is able to make sure that the method name and parameter format is abiding by the contract laid out in the server's type library. This is quite helpful given that any syntactic errors are found at compile time. Other COM clients are not so fortunate in having a detailed understanding of an interface's methods at compile time for the simple reason that they are not compiled! Consider for example Web-scripting languages such as VBScript. When a Web developer wishes to make use of a COM object from an HTML or classic ASP page, the only way to do so is by using an intrinsic method named CreateObject(), which returns a loosely typed Object variable (stored in a COM VARIANT). As the late-bound client interprets the scripting code, it is at runtime that the caller is able to determine if a given method exists, and if so, whether the parameters are of the correct type and order. Obviously, this is far less robust a programming model than early binding. However, for scripting clients, this is the only option given that the external GUIDs and type information have not been compiled into the code base of the scripting engine! When a coclass wishes to allow late-bound clients to access its functionality, it must support a standard COM interface named IDispatch. This standard COM interface allows a late-bound client to call any method or property on the object's IDispatch implementation (termed a dispinterface) using two well-known methods:
GetIDsOfNames() and Invoke(). In addition to these core members, IDispatch defines two members of lesser importance (as far as the client is concerned) named
GetTypeInfo() and GetTypeInfoCount(). The latter members are of great importance when you wish to read type information at runtime.

posted on 2010-03-23 21:45  Ray Z  阅读(192)  评论(0编辑  收藏  举报

导航