Interoperability between .NET components and COM components
.NET technology is the next-generation platform technology that Microsoft vigorously promotes, since the official release of the .NET technology architecture beta2 version, this technology is gradually maturing and stabilizing. According to Microsoft's platform system, we are not difficult to imagine, in the next two years. NET technology must be trending the mainstream technical platform, and a new technical platform is the most important premise of rapid development is : He will not completely abandon the previous technology, this point is COM / COM technology for .NET technology. In general, in the IT technology circle and hardware industry, the upgrade speed of technology is very amazing, and the practice is all new technologies to follow the principle of downward compatibility, but .NET technology not only did this, .NET or even Implementation of each call between each other, this is very difficult to be valuable. That is, not only we can call COM components in the .NET component, but also call the .NET component in the COM component. The benefits of this point are obvious, on the one hand, we can maintain existing technical resources, on the other hand, in the existing resources, can take advantage of the various new technologies brought by .NET. Let's take a look at the .NET component and what we currently use. Net components. The .NET component can be divided into two categories: shared .NET components and private .NET components. Shared .NET components need to ensure their own uniqueness through standard public keyword technology, which is more similar to the world's unique ID number GUID similar to COM. However, we should try to avoid the use of shared .NET components in the case of possible, because it will be able to fall into the "DLL Hell" that is currently in the long-term developer under the window system. Private .NET components are the way we will use frequently. Net component, in this way, we will release .NET components need to do simple copy operation, as if returned to ancient Dos era. It is not necessary to care about the complex system registry, and don't have to worry about the problem of the DLL version covered. The COM component is Microsoft once has also pushed a lot of code multiplexed technology frameworks, which also has achieved great development and application in these years, but its drawbacks are also increasingly obvious, we have to face many The version control between COM components and the horrible DLL hell, as well as registry, guid, and more. While we install a software, we also bring a lot of weighing COM components we are unknown to our operating system. However, the technical advantage of the COM component is also obvious. In a large extent, the code multiplexed under the Windows platform is realized, so we will propose such a topic, how to protect the .NET technology to be mature, And use a large number of software and products that have existing COM technology? Let's discuss how to achieve the meeting calls and operations between .NET components and COM components. First, let's take a look at how to call .NET written in an existing COM component: Here we use C # to write a simplest component, just return a string, the detailed code is as follows: on it. We implemented a class member in the NET component: SAY. His use is simple to return a string. Switch below the MS-DOS command line, run:
C: /> CSC / T: library / out classlibrary1.dllclasslibrary1.cs The above compiler parameter / T: library tells the C # compiler We are now building a library application. In this way, we get a .NET component named ClassLibrary1.dll. But we want to use this component in an existing COM component, we have the following steps: Switch to the MS-DOS command line, run:
C: /> regaSM out classlibrary1.dll /Regfile: Classlibrary1.Reg The above command line is registered with our .NET components and generates an alternate registry file. Everyone will remember that the command used by the COM component under our Win9X / NT / 2000 is: regsvr32 c: /test.dll under. Net below, registration .NET component requires the above regaSM command, it is worth noting This method is only to be called for COM components, and between .NET itself calls the component without any registration! Not yet ended, then we need: Switch to the MS-DOS command line, run:
C: /> TLBEXP CLASSLIBRARY1.DLL /OUT: Classlibrary1.tlb The above command line representation will generate a type library of a .NET component, with the purpose of our advance binding operation in the COM component. Ok, let's follow our business-based COM technology-based code. We can use the .NET component we use to use C # in your current COM technology. Here, we use VB6.0 to write a small test code. Before we start using the "Reference" option in the menu in the VB's integrated environment, choose the type library file ClassLibrary1.tlb we just generated. The code for the VB test is as follows:
Private Sub Form_Load () Dim Test As New Classlibrary1.Hellodim Stringstr= Test.SAY ("DDDD") MSGBox Strend Sub Next, let's take a look, how to use the current COM component in the .NET component. For .NET, use COM components to think about simple. .NET takes into account how conveniently use existing technical resources, which is also a consistent style of Microsoft, and the power of .NET can also be seen. .NET provides a large number of libraries to facilitate the implementation of COM's interoperability, which is very important to a namespace: system.Runtime.InteropServices. Through this name space, we can also see from the literal, "interoperability service". System.Runtime.InteropServices This namespace provides a range of classes to operate in COM objects. In the following example, we call the Win32 function MessageBoxa that comes with the system. This function is in the system's COM component user32.dll, the code we call is as follows:
using System; using System.Runtime.InteropServices; class Test {[DllImport ( "user32.dll")] public static extern int MessageBoxA (inthWnd, string strMsg, string strCaption, intnType); public static void Main () {int myMsg; Mymsg = MessageBoxa (0, "Hello!", "TEST", 0);}} Switch below the MS-DOS command line, run: