Home arrow C# arrow Page 2 - Using Late Bound COM Objects
C#

Using Late Bound COM Objects


There are different methods available for invoking COM objects at runtime in .NET, including late binding. In this article Beth from the Wrox team will walk us through them, providing examples for each.

Author Info:
By: Wrox Team
Rating: 5 stars5 stars5 stars5 stars5 stars / 35
November 18, 2002
TABLE OF CONTENTS:
  1. · Using Late Bound COM Objects
  2. · Introductory Concepts
  3. · Late Binding in .NET: The Basic Invocation
  4. · Late Binding in .NET: With a Runtime Callable Wrapper Available
  5. · Late Binding in .NET: Monitoring Performance of the Various Invocation Methods
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Using Late Bound COM Objects - Introductory Concepts
(Page 2 of 6 )

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 (click here) 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.

.NET to COM Interop Review
The .NET runtime supports the creation of a managed wrapper for COM objects to handle translations between .NET and COM. This Runtime Callable Wrapper (RCW) acts as a bridge between .NET and COM. If you add a reference to a COM component in your Visual Studio .NET project, a RCW will automatically be generated for you (note that Visual Studio .NET asks you first if a wrapper should be generated, rather than just automatically creating it for you). Alternatively, you can use the tlbimp utility that installs with the .NET Framework Software Development Kit to create the wrapper manually. Manual generation of the RCW allows for better control of the process. Command-line switches allow specification of namespace, output file, strong name information, and references, among others. More details are found in the .NET Framework SDK documentation.

With the ability to create an RCW, early bound COM interoperability is quite straightforward. Having now laid some groundwork, let's look at how to accomplish COM late binding in .NET.
blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials