用户登陆:
设为首页 | 加入收藏 | 免费邮箱
建站学院: 建站心得 | 搜索优化 | 建站点子 | 网站推广 | 网站推荐 | 企业新闻 | 人物新闻 | 网媒内幕 | 域名相关 | 热门专题 | 本站作品
设计学院: 网页设计 | 平面设计 | 网络编程 | 多媒体类 | 数据库类 | 服务器类 | 操作系统 | 联盟新闻 | 联盟介绍 | 联盟点评 | 网赚技巧
模板下载: 旅游度假 | 饮食食品 | 公司展示 | 学校教育 | 文化艺术 | 金融财经 | 儿童卡通 | 体育运动 | 服饰品牌 | 爱情交友 | 游戏娱乐
您现在的位置: 星星雨设计站 >> 设计学院 >> 网络编程 >> NET编程 >> 教程正文
应Mr.Cool要求:Using Late Bound COM Objects
作者:佚名    教程来源:不详    点击数:    更新时间:2007-11-19

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 scenarios for which 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.


发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口教程录入:冰河    责任编辑:冰河 
  网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
最新热门
最新推荐
| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明