. NET technology is the next-generation platform technology that Microsoft vigorously promotes. Since the formal release of. Net Technical Architecture beta2, this technology has gradually become mature and stable. According to Microsoft's platform system share, we can easily imagine that in the next year or two. NET technology is bound to become a mainstream technology platform, and the most important premise for a new technology platform to develop rapidly is: he will not abandon the previous technology completely, this is true. NET technology refers to the COM/COM + technology.
In general, in the IT technology field and hardware industry, the technology upgrading speed is amazing, and the practice is that all new technologies will follow the principle of backward compatibility,. NET technology does not only achieve this ,. net and even implement their respective calls, which is very valuable. That is to say, we can not only call the COM component in the. NET component, but also call the. NET component normally in the COM component. The benefits of this point are obvious. On the one hand, we can maintain the existing technical resources, and on the other hand, we can use various new technologies brought about by. net in the existing resources.
Before starting, let's take a look at the differences between the. NET component and the COM component that we usually use.
1... NET components can be divided into two categories: Shared. NET components and private. NET components.
1. The shared. NET component must use the standard public keyword Cryptography technology to ensure its uniqueness. In this regard, it is similar to the guid of COM's globally unique ID number. However, we should avoid sharing as much as possible. NET component, because it may once again fall into the "DLL hell" that has plagued developers for a long time in the Windows system ".
2. private. NET components are frequently used. NET component method. In this way, we release. NET components only need to do a simple copy operation, as if back to the ancient dos era, do not have to worry about the complicated system registry, you don't have to worry about overwriting the dll version.
3. sentiment: COM component is a type of tool that Microsoft has been pushing for many years.CodeThe Reusable technical framework has also been greatly developed and applied over the years, but its drawbacks have become increasingly obvious, we have to deal with version control among many COM components and the terrible DLL hell, as well as the registry, guid, and so on. While installing a software, we also bring a large number of unknown COM components to our operating system. However, the technical advantages of COM components are also obvious. To a large extent, we have implemented code reuse on the Windows platform. Therefore, we will propose such a topic, how can we solve this problem. when the net technology is becoming increasingly mature, how can we protect and use a large number of existing COM-based software and products?
Ii. Next we will discuss how to call and operate the. NET Component and COM component.
1. First, let's look at how to call the. NET Component in the existing COM component:
Here, we use C # To compile a simple component and only return a string. The detailed code is as follows: in the above. NET component, we implemented a class member: Say. Its purpose is to simply return a string.
Switch to the MS-DOS command line and run:
C: \>T: Library/out classlibrary1.dllclasslibrary1. CS
The above compiler parameter/T: Library tells C # the compiler that we are creating a library application.Program. In this way, we get a. NET component named classlibrary1.dll. However, to use this component in the existing COM component, we also perform the following steps:
Switch to the MS-DOS command line and run:
C: \> regasm out classlibrary1.dll/regfile: classlibrary1.reg
The above command line registers our. NET Component and generates a backup registry file. You will surely remember that the command used to register COM components Under Win9x/NT/2000 is: regsvr32 c: \ test. dll
In. net. the above regasm command is required for the net component. It is worth noting that this method is only for the COM component to call ,. net itself does not need to be registered to call each other's components! It's not over yet. Next we need:
Switch to the MS-DOS command line and run:
C: \> tlbexp classlibrary1.dll/out: classlibrary1.tlb
The command line above indicates that a. NET Component Type Library will be generated to bind the COM component in advance. Now, we can easily use the. NET component written in C # In our current COM-based code. Here, we use VB6.0 to compile a small test code. Before starting, we need to use the "Reference" option in the menu in the VB integration environment, select the type library file classlibrary1.tlb we just generated.
The VB test code is as follows:
Private sub form_load ()
Dim test as new classlibrary1.hello
Dim STR as string
STR = test. Say ("dddd ")
Msgbox Str
End sub
2. Next, let's take a look at how to use the existing COM component in the. NET component.
For. net, it is easier to use COM components .. At the beginning of the design, net considered how to easily use existing technical resources. This is also Microsoft's consistent style. Net's strength is also evident .. NET provides a large number of class libraries to facilitate operations on the same com. An important namespace is system. runtime. interopservices. Through the namespace name, we can also literally see the "Interoperability service ". The system. runtime. interopservices namespace provides a series of classes to operate on COM objects.
In the following example, we call the built-in Win32 function messageboxa, which is located 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 to the MS-DOS command line and run:
C: \> test. CS
After compiling our C # application, you can directly run it to see the dialog box!
Note that before calling the COM component, we need to reference the namespace: system. runtime. interopservices in the. Net program. Because we need to use the method provided by this namespace: dllimport. It is very convenient to use COM components in. net. The above is the built-in DLL component of the system we call. Similarly, we can use this method to call our own COM component written in VB/VC.
Iii. Conclusion: Well, we have discussed how. net and COM are operating on each other, and we can clearly see that. NET technology is powerful, and the sooner you master it. NET technology will be more likely to take the lead in the future, let us work together to master the powerful. NET platform technology.