日期:2011-03-11  浏览次数:20411 次

Using Late Bound COM Objects
ABSTRACT

In this article, Beth Breidenbach describes how to late bind to COM objects in .NET. Interoperability with COM objects has been well–integrated into the .NET runtime. But judging by the postings in the .NET newsgroups there appears to be some lingering confusion about how to bind to COM objects at runtime. This article will discuss the different methods available for invoking COM objects at runtime. After a brief review of late binding (and why it should sometimes be avoided), we will walk through three different invocation scenarios and explore the appropriate C# syntax to use. We end with a brief look at the relative performance of each method.
ARTICLE
This article will describe how to late bind to COM objects in .NET. Interoperability with COM objects has been well-integrated into the .NET runtime. Indeed, early binding is quite straightforward and has already been covered quite well in Kaushal Sanghavi's articles on COM(+) interoperability (see reference list at the end of this article). Judging by the postings in the .NET newsgroups, however, there appears to be some lingering confusion about how to bind to COM objects at runtime. This article will discuss the different methods available for invoking COM objects at runtime. After a brief review of late binding (and why it should sometimes be avoided) we will walk through three different invocation scenarios and explore the appropriate C# syntax to use. We will end with a brief look at the relative performance of each method.
Prerequisites
Our discussion assumes you understand the basic concepts of COM interoperability in .NET. If you are just getting up to speed on the topic, see the list of references at the end of this article. In particular, you should understand what a Runtime Callable Wrapper is and how one is generated.
Introductory Concepts
Late Binding Defined
Binding associates a method with a pointer to the method's memory location. In early binding, the object's client is bound to the vtable locations of the object's methods at the time of compilation. Late binding, in contrast, identifies a method's location at runtime via human-readable names. To enable late binding, the target object must implement the IDispatch interface. The IDispatch::GetIDsOfNames and IDispach::Invoke methods allow object interrogation and method invocation at runtime. The OleView application (bundled with Visual Studio 6) can be used to determine whether the IDispatch interface is supported by your target component. Notice the IDispatch inherited interface for this component:

Assuming this object has a ProgID of "TestObject.TestClass" and is located on a server named "OurServer," the typical VB6 code for late binding would look like this:
Dim oMyObject As Object
Set oMyObject = CreateObject("TestObject.TestClass", "OurServer")
MsgBox oMyObject.Echo1("echo this back to me")
In the example above, the variable is declared as a generic object. The CreateObject call uses the ProgID to locate the dll on the specified server. Note that the server parameter is not required if the target dll is located on the local box.
Why Use Late Binding
As you probably know, Microsoft recommends early binding whenever possible. It provides the best performance because your application binds directly to the address of the required functions. As Microsoft's Knowledge Base article #Q245115 (http://support.microsoft.com/default.aspx?scid=kb;EN-US;q245115) states when talking about early binding, "in terms of overall execution speed, it is at least twice as fast as late binding." So why worry about how to late bind at all?
There are scenario