应Mr.Cool要求:Using Late Bound COM Objects

来源:互联网 发布:淘宝店铺怎么装修2017 编辑:程序博客网 时间:2024/05/18 18:14
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
COM" target=_blank>Using COM" target=_blank>Late COM" target=_blank>Bound COM Objects
ABSTRACT
In this article, Beth Breidenbach describes how to COM" target=_blank>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 COM" target=_blank>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 COM" target=_blank>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 COM" target=_blank>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
COM" target=_blank>Late Binding Defined
Binding associates a method with a pointer to the method's memory location. In early binding, the object's client is COM" target=_blank>Bound to the vtable locations of the object's methods at the time of COMpilation. COM" target=_blank>Late binding, in contrast, identifies a method's location at runtime via human-readable names. To enable COM" target=_blank>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 COM" target=_blank>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 COM" target=_blank>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 COM" target=_blank>Late binding." So why worry about how to COM" target=_blank>Late bind at all?
There are scenarios for which COM" target=_blank>Late binding is justified. You may not know the exact binary interface until runtime. The object may exist in multiple versions and have improperly adapted interfaces between the versions. You may be developing web applications on a shared host and be unable to get a RCW created for the COM object you want to invoke. Or, the object may have been COMpiled in VB with improper version COMpatibility settings, resulting in constantly changing GUIDs even when the version hasn't changed. In these scenarios, a defensive programmer needs to remove his or her code a bit from the object's binary signature. The tradeoff is one of flexibility versus performance and should be weighed carefully.

COM" target=_blank>Mr.COM" target=_blank>CoolCOM" target=_blank>要求:COM" target=_blank>Using COM" target=_blank>Late COM" target=_blank>Bound COM Objects';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击