Get started with COM INTEROP--RCW

Source: Internet
Author: User
Tags command line integer versions wrapper visual studio
Go near COM Interop
Getting Started with--RCW

Introduction

As a result of the current addition of a responsibility to the original system porting to. NET platform, spent some time on COM--> NET has done some basic research. Post this article and hope to share with you. Deficiencies, please correct me. Thanks first.

First, the basic concept
What is COM Interop? COM interop looks like a link between COM and. NET, a bridge. For backward compatibility, COM interop enables. NET programs to easily access COM components without modifying existing COM components. This is very important. In fact, the number of code for a global COM component is estimated to be billions of lines, and it is not possible for companies with these COM components to rewrite these components, so the presence of COM interop provides a good solution for developers with this requirement.
As you all know, there are very big differences between COM and. NET, and there are 2 kinds of bridging methods in COM Interop in order for the two to be organically combined to work together. One is Rcw,runtime callable wrapper (the level is limited, do not know exactly what the translation is, here is not detailed. ) The other is ccw,com callable wrapper. The latter, will be discussed in the following related articles, here is no longer to repeat, this article is mainly concerned about the former. The RCW is obtained at run time through a dynamic instantiation of the relevant information from the CLR's metadata in the interop assembly (interop Assembly). Personally, it can be understood to be a proxy between COM and. NET applications, and each invocation request for a COM component by a. NET application is relayed through this RCW. Users will not feel like they are calling COM components, everything is so natural, and calling one. NET components are not any different. Friends with C + + know that if you want to instantiate a COM object in C + +, you need to use CoCreateInstance. And when we have the RCW, everything becomes simple, and we can use new in C # to instantiate this COM object directly.
It should be noted that a COM component (referring to an instance, a DLL file) is maintained by and only one RCW. So here's the question, is there a different RCW for the different versions of a COM component? The answer is yes. Some of the friends would say,. NET does not already solve the problem of "DLL HELL" in COM? According to the above, it does not seem to be resolved? What I'm going to say here is that this kind of problem occurs when you import different versions of a COM component in. Net. The solution to this type of problem is to use the PIA (Primary Interop Assembly), which is not covered in this article, and I will discuss it with you later in the relevant articles.

Second, the actual combat exercise
. NET provides three ways for us to import a COM component:
-Through visual Studio. NET extract from the "Add Reference" feature
---tlbimp by command line method. Exe
-Use System.Runtime.InteropServices.TypeLibConverter class programming to implement import functionality
The first way is not to repeat, it is very simple. The third way I'll explain the use of the PIA is to illustrate the usage of this method. However, my level is not high, you do not ask too high. ^_^, this article is concerned with the second way. We can directly use this tool to provide the simplest way to invoke: TLBIMP TestObj.dll. But we need to remind you that if we import a COM component in this way, we will "sacrifice" the original COM component, which is dangerous. Personally, the simplest way to call is TlbImp testobj.dll/out:interop. TestObjLib.dll. This will result in the execution of a name interop. COM Interop for TestObjLib.dll. A simple example is given below to illustrate the whole process.
1, first use VB to write a simple ActiveX Dll
' Project name is Testobjlist, class name is Testobj
Option Explicit

Public Function Add (ByVal ivalue As Integer) As Integer
ADD = ivalue + 1
End Function

2. Import this COM component using command line tools
TLBIMP Testobjlib.dll/out:interop. TestObjLib.dll

3. Write a section of test code that calls this component in a. Net Windows forms
Using Interop. Testobjlib;
private void Button1_Click (object sender, System.EventArgs e)
{
Testobjclass obj = new Testobjclass ();
int num = 1;
MessageBox.Show (obj. ADD (num). ToString ());
}

One point to note here is that when TLBIMP.EXE generates an interop fitting, it adds a "class" to the name of the class in the original COM. Please be aware of this when you call.



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.