. NET components and COM components interoperate

Source: Internet
Author: User
Tags command line unique id
. NET components and COM components interoperate
. NET technology is the next generation platform technology that Microsoft vigorously promotes, since. NET technology Architecture BETA2 version of the official release, the technology has gradually become mature and stable. According to Microsoft's platform system share, it is not difficult to imagine that in the next year or two. NET technology is bound to mount the mainstream technology platform, and the most important prerequisite for the rapid development of a new technology platform is: He will not completely discard the previous technology, this is for. NET technology refers to the com/com+ technology.

Generally speaking, in the IT technology industry as well as the hardware industry, the speed of technology upgrading is very amazing, and the Convention is that all new technologies will follow the principle of backward compatibility, but. NET technology has not only done this,. NET even realizes the respective call between each other, this is very commendable. That is to say, not only can we be in. NET component, you can also invoke the. NET component normally in a COM component. The benefits of this point are obvious, on the one hand we can maintain the existing technical resources, on the other hand, in the existing resources can be used. NET brings with it all kinds of new technologies.

Let's take a look at it before we start. NET components and the COM components we use today are different.

. NET components can be grouped into two broad categories: shared. NET components and private. NET components.

Shared. NET components require a standard disclosure of keyword cryptography to ensure their uniqueness, which compares the globally unique ID number GUID similar to COM. However, we should try to avoid the use of shared information where possible. NET component, because it is likely to fall back into the "DLL Hell" that has plagued developers for a long time in the Windows system.

Private. NET components are the. NET components that we will often use, in this way, we release the. NET components need to do just a simple copy operation on it, as if back to the ancient era of DOS, and do not have to care about the complex system registry, and do not have to worry about the DLL version is overwritten and so on.

and COM component is Microsoft has been pushing for many years a code reuse technical framework, in the past few years has also been a great development and application, but its drawbacks are increasingly obvious, we have to face a large number of COM components between version control and scary DLL Hell, as well as the registry, GUID and so on. As we install a software, we also bring a large number of unknown versions of the COM components to our operating system.

However, the technical advantages of COM components are also obvious, to a large extent, to achieve the Windows platform code reuse, so we will put forward such a topic, how to be in the. NET technology is getting more mature, the protection and utilization of the existing large number of software and products using COM technology?

Here we will discuss how to achieve. NET component and COM component, call and manipulate.

First, let's take a look at how to invoke the component in the existing COM component. NET writing:
Here we use C # to write one of the simplest components, just to return a string, the detailed code is as follows:

On the top. NET component we have implemented a class member: Say. His purpose is simply to return a section of 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 the C # compiler that we are now building a library application. In this way, we get a. NET component called ClassLibrary1.DLL.
But to use this component in an existing COM component, we have the following steps:

Switch to the MS-DOS command line and run:
C:\>regasm out Classlibrary1.dll/regfile:classlibrary1.reg

The command line above is registered with us. NET component, and an alternate registry file is generated. Everyone will remember that before our win9x/nt/2000. The following commands for registering COM components are:
regsvr32 c:\test.dll

Under. NET, register. NET component requires the above RegAsm command, and it is worth noting that this method is only intended for COM components to be invoked. NET itself to invoke components between each other is not required any registration!

It's not over yet, and then we need to:

Switch to the MS-DOS command line and run:
C:\>tlbexp classlibrary1.dll/out:classlibrary1.tlb

The command line above indicates that one will be generated. NET component, the purpose of which is to make early binding operations in a COM component.

Well, then we can use it in our own code based on the current COM technology, which we use in C #. NET component. Here, we use VB6.0 to write a small test code, before we begin, we need to use the "reference" option from the menu in the VB Integration environment to select the type library file ClassLibrary1.tlb we just produced.

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 the. NET component, use the existing COM component.

For. NET, use COM components to do something simpler. NET at the beginning of the design to take into account how to easily use the existing technical resources, which is Microsoft's consistent style. NET's strong point also can be seen. NET provides a large number of class libraries to facilitate mutual operation with COM, one of the most important namespaces is: System.Runtime.InteropServices. By the name of this namespace we can also literally see "interoperability services". System.Runtime.InteropServices This namespace provides a series of classes to manipulate COM objects.

In the following example, we call the system's Win32 function MessageBoxA, which is located in the COM component user32.dll of the system, and 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, we can see the dialog box directly!

It is to be noted that before invoking the COM component, we need to be in the. NET Program Reference namespace: System.Runtime.InteropServices. Because we need to use one of the methods provided by this namespace: dllimport.

Well, it's convenient to use COM components in. Net. The above is the DLL component that comes with the system itself, and you can use this method to invoke the COM component that we write ourselves using VB/VC.

Well, we've already explored how to get in. NET and COM operate between each other, we also obviously see. NET technology is strong, now the earlier master. NET technology will in the future easier to occupy the leading position, let us work together to master the strong. NET platform technology.



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.