C#
  Home arrow C# arrow Page 2 - Using Late Bound COM Objects
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Using Late Bound COM Objects
By: Wrox Team
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2002-11-18

    Table of Contents:
  • Using Late Bound COM Objects
  • Introductory Concepts
  • Late Binding in .NET: The Basic Invocation
  • Late Binding in .NET: With a Runtime Callable Wrapper Available
  • Late Binding in .NET: Monitoring Performance of the Various Invocation Methods
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C# Articles
    More By Wrox Team


     

    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#






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT