In this article I am going to explain how to access a .NET component from a COM client and accessing 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 this 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 ClientsThis example demonstrates how 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 StringPassStr = "Hi From .NET Component" end function end class end namespaceSave this code to a text file with the name testCCW.vb.Now compile this file from the command line with the statemantVbc /t:library testCCW.vbThe VBC compiler will create a testCCW.dll file for you, this is the .NET assembly.Now, the next step is to create 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 resister the .NET component with the registry, use Regasm testCCW.dll
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
COM Client (VB6)(Late binded)Private Sub Command1_Click()dim o set o = createobject("CCWComponent.CCWClass")msgbox o.PassStr end subThe same client can also early bind to the CCW of the .NET component if the CCW of .NET component was exported to .tlb file using regasm /tlb switch.Most ImportantIn order to find the assembly of the .NET component, the COM client Executable has to be in the same directory of 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 ActiceX dllPublic Function SayHi() As String SayHi = "Hi From COM Component" End FunctionAfter compiling the component create a wrapper using tlbimp utility Tlbimp ComSvr.dll /out c:\<Path> It is recommended to use the /out switch from preventing accidentally over writing 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.ComComponentmsgbox (o.sayHi)end subend classSave the file with the name Ntest.VB, compile the VB.NET client and run it using vbc /r:ComSvr.dll Ntest.vb.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: