Mutual access between. NET and COM

Source: Internet
Author: User
Tags wrapper
Access this article describes how to access COM components in. NET clients and access. NET components in COM clients.
The essence of com is the communication between the code. Communication between code in. NET is not in the form of COM, so you cannot communicate directly with. NET components on a COM client. This is done using the CCW (COM callable Wrapper), where the CCW acts as a proxy for a. NET object, and if you want to communicate with COM components on a. NET client, you need to take advantage of the RCW (RUNTIME callable W Rapper) to act as such an agent.

COM clients communicate with. NET Components

The following example shows a VB6 client using a CCW to access a vb.net component

A 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 the above code as a Testccw.vb file, and then compile with the following statement:


Vbc/t:library Testccw.vb


The VBC compiler will produce a TestCCW.DLL file, which is a. NET assembly, and the next step is to create a CCW proxy for TestCCW.DLL. The RegAsm tool can register a. NET component and generate a reference for COM clients. TLB file.
Use the following statement:

Regasm TestCCW.dll


This will only register the. NET control in the registry, which applies to the late binded client.
Or use the following statement:

Regasm testccw.dll/tlb:testccw.tlb


This will create a testccw.tlb file, which is applied to the early Bingde client.

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 be applied to the early binded client, which, of course, requires a TBL file to be exported in the previous step for reference by a COM client. It is important to emphasize that the client must be in a directory with the. NET component that it is calling, or that the component exists in the global ASSEMBLY cache.

Using the RCW (RUNTIME callable Wrapper)

The following example describes an agent that uses the TlbImp tool to create a COM component that is accessible to. NET clients. COM Server ComSrv.dll (mycom.comcomponent)

Add the following code to an ActiveX DLL:

Public Function Sayhi () as String
sayhi = "Hi from COM Component"
End Function


After the component is compiled, use the TlbImp tool to create an agent:

Tlbimp Comsvr.dll/out c:\<path>


The proxy for the COM component will be generated in the specified directory.

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 As Ntest.vb, and then compile:

Vbc/r:comsvr.dll Ntest.vb



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.