Mutual operations between. NET components and COM components //)

Source: Internet
Author: User
Mutual operations between the. NET component and the COM component // (convert)
. 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.

. NET components can be divided into two categories: Shared. NET components and private. NET components.

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 the Globally Unique id of COM. 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 ".

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.

The COM component is a technical framework that Microsoft once promoted for many years for code reuse. It has 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?

Next we will discuss how to call and operate the. NET Component and COM component.

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: \> CSC/T: Library/out classlibrary1.dllclasslibrary1. CS

The above compiler parameter/T: Library tells C # That we are creating a library application for the compiler. 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. We will 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

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: \> CSC 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.

Now, 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.

(The above program is tested and passed in Windows2000 Server + vs. Net beta2)

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.