Getting started near Com Interop - RCW

zhaozj2021-02-08  325

Getting Started with COM Interop - RCW

introduction

Since there is currently a project group responsible for porting the original system to the .NET platform, it takes some basic research to COM ->. Net. Post this article, I hope to share with you. Insufficient, please refer to you. Thank you first.

First, what is COM Interop? COM Interop looks like a link between COM and .NET, a bridge. To keep backward compatibility, COM Interop can make the .NET program to easily access COM components without modifying the original COM component. This is very important. In fact, the global COM component code estimates may have billions, and companies with these COM components cannot rewrite these components, so Com Interop provides a good solution for developers with this needs. . Everyone knows that there is a very big difference between COM and .NET, in order to make both organic combined together, there are two bridges in COM Interop. One is RCW, Runtime Callable Wrapper (limited to the exact translation, there is no detailed explanation.); The other is CCW, CoM Callable Wrapper. The latter will discuss in subsequent related articles, and will not be described again here. This paper mainly pays attention to the former. The RCW is obtained by the instantiation of the related information dynamics through the CLR from the interop assembly in the runtime. Personally think it is to understand it in a proxy between COM and .NET applications, the .NET application is transferred through this RCW for each call request for the COM component. Users will not feel that they are calling COM components, everything is so natural, and calling a .NET component without any difference. Friends using C know, if you want to instantiate a COM object in C , you need to use CocreateInstance. And when we have RCW, everything is simple, we can use New to use New to directly instantize this COM object. It should be noted that a COM component (referring to an instance, a DLL file) is maintained by one RCW. Then there is a problem here. Is there a different RCW corresponding to a different version of a COM component? The answer is yes. Some friends will say, the component in .NET does not have solved the "DLL HELL" problem in COM? According to the above say, it doesn't seem to be solved? What I want to say here is that this problem occurs in the different versions of a COM component in .NET. The method of solving such a problem is to use PIA (Primary Interop Assembly), which is not within the discussion of this article, I will discuss with you in subsequent related articles.

Second, the actual exercise .NET provides three ways to import a COM component: - "Add Reference" feature in Visual Studio .NET - Via the command line mode - TLBIMP.EXE - using System.Runtime.InterOpServices.TypelibConverter The first way to implement the import function is not described in the first way to implement the import function. In the third way I will explain the use of the PIA to illustrate the use of this method. However, my level is not high, you don't ask too much. ^ _ ^ Specifies to go forward, this article focuses on the second way. We can use the simplest call way to provide this tool directly: TLBIMP Testobj.dll. However, you need to remind you that if we use this way to import a COM component, we will "sacrifice" the original COM component, which is very dangerous. Personally, the simplest call mode is TLBIMP TESTOBJ.DLL /OUT: Interop.Testobjlib.dll. In this way, a COM Interop named Interop.Testobjlib.dll will be generated after execution. A simple example will be given below to explain the entire process. 1. First use VB Write a simple ActiveX DLL'Project Name is testobjlist, Class Name is testObjoption explicitpublic function add (byval iValue as integer) AS integer add = ivalue 1END FUNCTION

2. Use the command line tool to import this COM component TLBIMP TESTOBJLIB.dll /out:interop.testobjlib.dll

3, write in a period of a .Net Windows Forms test code call this component 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 ());

A little more, when TLBIMP.EXE is generated when generating an Interop accessory, add a "class" after the name of the class in the original COM. When calling, please pay attention.

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

New Post(0)