-- RCW
Xuyan
Since we have joined a project team responsible for porting the original system to the. NET platform, it took some time to do some basic research on com -->. net. I hope to share this article with you. Please correct us. Thank you.
I. Basic Concepts
What is com InterOP? Com InterOP seems to be a bridge between COM and. net. To maintain backward compatibility, com InterOP can enable. net Program Do not modify the original COM Component To access the COM component. This is very important. In fact Code It is estimated that there may be billions of lines. Companies with these COM components cannot rewrite these components. Therefore, the existence of COM InterOP provides a good solution for developers with this demand.
As we all know, there is a big difference between COM and. net, so that they can be organically combined Together For collaborative work, there are actually two bridging methods in COM InterOP. One is RCW and runtime callable wrapper (the level is limited. I don't know what the exact translation is, so I won't explain it here .); The other is CCW and COM callable wrapper. The latter will be relevant in the future Article This article focuses on the former. RCW is obtained by dynamically instantiating the relevant information from the metadata of the InterOP Assembly through CLR at runtime. I personally think that it can be understood as between COM and. net Applications A proxy between. NET applications, each call request of COM components is forwarded through this RCW. The user will not feel that he is calling the COM component. Everything is so natural, there is no difference from calling a. NET component. Anyone who uses C ++ knows that cocreateinstance is required to instantiate a COM Object in C ++. With RCW, everything becomes simple. We can use new in C # To directly instantiate this COM object.
It should be noted that a COM component (refers to an instance, that is, DLL file Only one RCW is responsible for maintenance. So there is a problem here. For different versions of a COM component, is there a different RCW that corresponds to it? The answer is yes. Some friends may say that the components in. Net have not solved the "DLL hell" Problem in COM? As mentioned above, it does not seem to be solved? Here I want to say that this problem occurs when importing different versions of a COM component in. net. The solution to this problem is to use PIA (primary InterOP assembly), which is not covered in this article and will be discussed in subsequent articles.
Ii. Practical drills
. NET provides three ways for us to import a COM component:
-The "add reference" feature provided by Visual Studio. NET
-Use the command line -- tlbimp. exe
-Use the system. runtime. interopservices. typelibconverter class Programming to Implement the import function.
The first method is very simple. In the third way, I will describe the usage of Pia and describe the use of this method. However, My The level is not high. ^_^ Let's get down to the point. This article focuses on the second method. We can directly use this tool to provide the simplest call method: tlbimp testobj. dll. However, we need to remind you that if we import a COM component in this way, we will sacrifice the original COM component, which is very dangerous. In my opinion, the simplest call method is tlbimp testobj. dll/out: InterOP. testobjlib. dll. In this way, a com InterOP named InterOP. testobjlib. dll will be generated after execution. The following is a simple example to illustrate the entire process.
1. A simple ActiveX dll written in VB
'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. Use the command line tool to import this COM Component
Tlbimp testobjlib. dll/out: InterOP. testobjlib. dll
3. Write a test code to call this component in. Net Windows Forms.
Using InterOP. testobjlib;
Private void button#click (Object sender, system. eventargs E)
{
Testobjclass OBJ = new testobjclass ();
Int num = 1;
MessageBox. Show (obj. Add (Num). tostring ());
}
Note that when tlbimp. EXE generates an InterOP assembly, it adds a "class" to the name of the class in the original COM ". When calling, please note.
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