Home > Microsoft .Net Development Tips > Visual Basic and Visual Basic .NET > Accessing .NET components from COM clients and vice versa
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

VISUAL BASIC AND VISUAL BASIC .NET

Accessing .NET components from COM clients and vice versa


Manish Mehta
01.21.2003
Rating: -4.17- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!

In this article I am going to explain how to access a .NET component from a COM client and, vice versa, how to access COM components from .NET clients.

COM is all about how little pieces of code find other little pieces of code and how they communicate with each other. COM precisely defines how the location and communication takes place.

In the .NET world, little pieces of code still find and talk with little pieces of code, but they don't use COM. Hence, if you want to access a .NET component from a COM client, you cannot do it directly. The solution to this is CCW (COM callable wrapper) which acts as a proxy for the .NET object. Similarly, if you want to access a COM component from a .NET client you will have to create a RCW (runtime callable wrapper).

Accessing .NET components from COM clients

This example demonstrates how a VB6 COM client using CCW accesses a component developed in VB.NET.

VB.NET component (testCCW.vb)

  imports system
  namespace CCWComponent
  public class CCWClass
    public function PassStr as string
      PassStr = "Hi From .NET Component"
    end function
  end class
  end namespace  

Save this code to a text file with the name testCCW.vb. Now compile this file from the command line with the statement:

Vbc /t:library testCCW.vb

The VBC compiler will create a testCCW.dll file for you -- this is the .NET assembly.

Now, the next step is to create a COM Callable Wrapper proxy for the component testCCW.dll file. The regasm utility can register the .NET component and also create a .TLB file, which can be referenced from any COM client.

If you want to just register the .NET component with the registry, use Regasm testCCW.dll. It will create the required registry entries. It is useful when you want to create late binded clients. If you want to register and generate the Type library, i.e .TLB file, from the .NET component, use:

Regasm testCCW.dll /tlb:testCCW.tlb

It will create a file testCCW.tlb, which can be reference by COM clients It is useful when you want to create COM clients that want to use early binding to the .NET component.

COM client (VB6) (Late binded)

Private Sub Command1_Click()
  dim o 
  set o = createobject("CCWComponent.CCWClass")
  msgbox o.PassStr
end sub  

The same client can also early bind to the CCW of the .NET component if the CCW of the .NET component was exported to .tlb file using regasm /tlb switch.

Most important

In order to find the assembly of the .NET component, the COM client executable has to be in the same directory as the .NET component, or the .NET component has to be in the Global Assembly cache.

Using RCW (runtime callable client)

If you want to access the COM component from the .NET client you will need to create a wrapper for the COM component using the TLBImp utility.

COM Server ComSrv.dll (MyCom.ComComponent)

Add the following code in a ActiveX DLL:

Public Function SayHi() As String
   SayHi = "Hi From COM Component"
End Function 

After compiling the component, create a wrapper using tlbimp utility.

Tlbimp ComSvr.dll /out c:<Path>

It is recommended to use the /out switch to prevent accidentally overwriting the COM DLL. The .NET proxy for the COM server will be created in the path specified in the /out switch.

VB.NET client (Ntest.vb)

imports system
imports microsoft.visualbasic
imports MyCom

class NTest
  shared sub main
    dim o as new MyCom.ComComponent
    msgbox (o.sayHi)
  end sub
end class 

Save the file with the name Ntest.VB. Compile the VB.NET client and run it using: vbc /r:ComSvr.dll Ntest.vb.


Source: DotNetExtreme.com

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Visual Basic and Visual Basic .NET
Compose XML more quickly using Visual Basic 9
Virtualization keeps Legacy Apps alive
VB 9 Anonymous Types help create flexible objects
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Book Excerpt: Sams Teach Yourself Visual Basic 2008 in 24 Hours -- Complete Starter Kit
Check out CodePlex for a ton of interesting .NET projects
Book excerpt: Murach's VB 2008
Book excerpt: Printing in Visual Basic 2005
Visual Basic 2008 and closures
WinForms development using SQL Server 2005 and Visual Basic 2005

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts