Interview between .NET and COM

zhaozj2021-02-17  117

Original: http://dotnetextreme.com/articles/ccwrcw.html Translation: Slash 51DotNet Club   This article will introduce, how to access the COM component in the .NET client, and visit .NET in the COM client Component. The essence of com is the mutual communication between the code. The mutual communication between .NET's code is not in the form of COM, so you can't communicate directly to the component of a COM client. Here, CCW (COM CALLABLE WRAPPER) is required to complete such an operation, and the CCW is here as a .NET object agent, the same, if you want to communicate with the COM component in the .NET client, you need to use RCW (Runtime Callable) Wrapper "to act as such a proxy. COM's client with .NET components Make communication The following example Save a VB6 client using CCW to access a VB.NET component (TestCW.vb)

Imports systemnamespace ccwcomponentpublic class ccwclics puble function passstr as stringpassstr = "hi from .net component"                                  

Store the above code as a TestCW.vb file, then compile with the following statement:

VBR / T: Library testccw.vb

The VBC compiler will generate a TestCW.dll file, this is a .NET's assembly, the next step to create a CCW agent for TestCW.DLL. The REGASM tool can register a .NET component and generate a .tlb file that is referenced by the COM client. Use the following statement:

Regasm testccw.dll

This will only register .NET control in the registry, this method is applied to the Late Binded client. Or use the following statement:

Regasm testccw.dll /tlb:testcw.tlb

This will create a TestCW.TLB file, which is applied to the Early Bingde client.

COM Client (VB6) (Late Binded) Private Sub Command1_Click ()            "ccwcomponent.ccwclass") Gbox o.passstrend Sub

The same client can also be applied to the Early Binded client, of course, this requires an TBL file to be output for the COM client to reference. It is emphasized that the client must be in a directory with the .NET component it wants to call, or, the component exists in the global assembly cache. Using RCW (Runtime Callable Wrapper) The following example will introduce the use of TLBIMP Tools to create a COM component's proxy for .NET clients to access. COM server comsrv.dll (mycom.comcomponent) Add the following code to an ActiveX DLL:

Public Function Sayhi () AS STRING SAYHI = "Hi from Component" End Function

After the component is compiled, use the TLBIMP tool to create a proxy:

TLBIMP COMSVR.DLL / OUT C: /

The agent of the COM component will be generated in the specified directory. VB.NET client (NTEST.VB) Imports SystemImports Microsoft.VisualBasicImports MyComclass NTEST  SHARED SUB MAINOMDIM O As New Mycom.comPonent  Msgbox (O. Sayhi) nd Subend Class

Save as NTEST.VB, then compile:

VBC /R: Comsvr.dll nteest.vb

转载请注明原文地址:https://www.9cbs.com/read-31525.html

New Post(0)