Mutual operations between. NET components and COM components

Source: Internet
Author: User

. 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 occupancy rate, 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 not only achieves this point ,. 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 COM group pieces of unknown versions 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 strong advantages are 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)
----------------------
Related Knowledge
The following are the concepts of com:
What is com
Components Object Model (COM) is a way for software Components to communicate with each other. It is a binary and network standard that allows any two components to communicate with each other, no matter on which computer they run as long as the computer is connected ), no matter what the computing machine runs * as long as the * system supports COM), or the language in which the component is written. COM also provides location transparency: When you write components, other components are in-process DLL, local EXE, or components on other computers, it doesn't matter to you. Of course there will be performance differences, but even if you change the location of other components, you do not have to rewrite anything, this is the key .)
COM is based on objects-but this object concept is not the same as the objects in C ++ or Visual Basic that you are familiar. By the way, "object" and "component" are almost the same thing. GUI doctors are willing to say "components" when talking about the structure of the application, but are willing to say "objects" when talking about implementation ")
First, the COM object is well encapsulated. You cannot access the internal implementation details of the object. You cannot know what data structure the object uses. In fact, object encapsulation is so strict that COM objects are often depicted as boxes. Figure 1 depicts a fully encapsulated object. Note how the implementation details are hidden from you.
Component Services in Windows 2000 is a very important part of enterprise-level applications. It provides almost all required functions for web-based, distributed applications. Although the COM + service was originally designed to provide services for COM components, it would be good if the. NET component can also use the COM + service. Because COM + can provide many services for components, such as object buffering, resource sharing, role security mechanisms, and distributed transaction processing.
In summary, COM has the following advantages:
The programming technology difficulty and workload are reduced, the development cycle is shortened, and the development cost is reduced. Generally, programmers only need to select the appropriate components according to the functional requirements of the application, and do not have to do anything in detail. The component module allocates the technical difficulty and workload of programming on individual personnel and time.
Hierarchical programming can be implemented to promote the professional production of software. Professionals can develop software components with strong professionalism, which ensures that common programming applications can complete the necessary application development without reducing the performance of use. The component modules that the application staff cannot implement can be customized by professionals.
The rich component market is promoted to meet various application requirements to the maximum extent, and it is quite easy to customize specific software modules.
The software reuse rate is improved, which improves the software efficiency and extends the service life. The Component Programming System localized a large number of programming problems, making software updates and maintenance fast and easy, and greatly reducing software costs.

What is COM?

What is COM?
The so-called COMComponet Object Model, Component Object Model) is a specification that describes how to establish dynamically changing components. This SPECIFICATION provides a way to ensure interoperability, some binary and network standards that customers and components should follow. With this standard, you can communicate between any two components without considering whether the operating environment is the same, whether the development languages used are consistent, and whether they run on the same computer.
What are the advantages of COM?
First, users generally want to customize the applications they use, and the component technology is essentially customizable, therefore, users can replace the original one with a component that better meets their needs. Secondly, components are components independent from applications. We can use the same component in different programs without any problems. the reusability of the software will be greatly enhanced. Third, with the increase of network bandwidth and its importance, distributed network applications have undoubtedly become an increasingly important part of the software market. The component price structure simplifies the process of developing such applications.
What is COM +?
COM + is not a simple upgrade of COM. The underlying structure of COM + is still based on COM. It covers almost all COM content, COM + integrates the technical elements of COM, DCOM, and MTS. It promotes the COM component software to the application layer instead of the underlying software structure. It supports various operating systems, the component object model is built on the application layer, leaving the underlying details of all components to the operating system. Therefore, the combination of COM + and the operating system is closer.
COM + is no longer limited to the COM component technology. It focuses more on the design and implementation of distributed network applications. COM + inherits almost all the advantages of COM and avoids some shortcomings in COM implementation. It combines the programming models of COM, DCOM, and MTS and inherits the vast majority of their features, new features are added to the original features.
What are the new advantages of COM +?
The following lists the main features of COM +:
COM + not only inherits all the advantages of COM, but also adds some services, such as queue service, load balancing, memory database, and event service.
The queue service is very meaningful for distributed applications, especially when the network speed is very slow, this mechanism can ensure that the application system can run reliably. When the application system contains a large number of nodes but the server is busy, the customer application can put their requests in the queue and process these requests when the server load ratio is lighter;
Another example is that COM + provides the load balancing service, which can achieve dynamic load balancing. In addition, the load balancing feature of COM + applications does not need to be supported by coding, both client programs and component programs can be implemented in the usual way. The load balancing feature is not implemented by programming, but by configuring Load Balancing for distributed applications. The Queue Service mentioned above, in fact, it also reflects a load balancing.
(1) Real asynchronous communication. The underlying layer of COM + provides Queue component services, which makes it possible for customers and components to work collaboratively at different time points. COM + applications can achieve this feature without adding code.
(2) Event Service. The new event mechanism makes the event source and event receiver more flexible in implementing the event function. The system service simplifies the event model and avoids the trivial details of the object mechanism that can be connected to COM.

(3) scalability. The scalability of COM + comes from multiple aspects. Dynamic Load Balancing, memory database, Object pool, and other system services provide the technical basis for the scalability of COM +, the scalability principle of COM + is consistent with that of the multi-layer structure.
(4) manageability and configurability. Management and configuration are actions after the application system is developed. As the software maintenance costs keep increasing, COM + applications will help software vendors and users Reduce investment in this area.
(5) easy to develop. The complexity and difficulty of COM + application development determine the success or failure of COM +. Although the COM + development model is simpler than the previous COM component development model, however, some excellent development tools are still needed to improve development efficiency.
COM + marks that Microsoft's component technology has reached a new height. It is no longer limited to a desktop system on a machine. It directs its goal to a broader enterprise intranet, or even the Internet. COM + and multilayer structure model and Windows operating system provide a complete set of solutions for enterprise applications or Web applications.

Related Article

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.