Code reuse-DLL and COM

Source: Internet
Author: User
Tags export class

Common Methods of code reuse include source code reuse, DLL, and COM.

I. source code reuse
Directly add others' source files to your project for compilation and connection, and finally generate the EXE file. This code-Level Reuse method saves the following problems:

1. source code written in one language can only be used by the same language. For example, the code you write with VC must not be directly used in VB and can only be used in other VC projects.
2. Programmers usually need to modify the code provided by others, but the premise of modification is that they must understand the ideas and coding style of the Code provider, which is very troublesome.

3. source code reuse leads to a waste of hard disk and memory space
If n software on the machine uses class ca, a compilation of the code generated by class CA is generated in the storage space of each software, and when the n software is executed simultaneously, memory usage of n × sizeof (CA)
4. Once the software is released, it cannot be modified unless the entire software is re-compiled.
If a bug exists in the class Ca and the CA implementation needs to be modified to fix the bug, the whole software will only be re-compiled. The software does not have a 2-digit modular feature.

Ii. dll
DLL is mainly used to implement the "cross-application and language binary sharing" of functions ". Dll can also share classes, but it cannot share classes across languages. It can only be used when static links are used, that is, it can only be used by the same language and compiler.

DLL successfully solves some problems of source code reuse. It brings the following benefits:

1. You can write data in multiple languages.

On Windows, VC ++, C ++ builder, Delphi, VB, C #, and VB. net, C ++.. Net can be used to compile a DLL, and a DLL written in one language can be called by other languages (only for export functions ). (VB can only write active DLL)

2. Facilitate Resource Sharing

Dll can contain dialog box templates, strings, icons, bitmaps, and other resources. Multiple applications can use DLL to share resources.

3. Facilitate Product localization

You can use DLL to provide the multi-language version of the program!

4. Saving disk space and memory

No matter how many software on your machine uses the DLL, there will be only one copy of the DLL. In addition, you only need to call it once in the memory.

 

Iii. dll reuse Problems

1. Incompatibility with compilers and connectors
To support overloading, the C ++ compiler arbitrarily modifies the function name, and each c ++ compiler usually uses different methods to modify the function name.

This causes problems when linking the DLL generated by different compilers.

One solution to this problem is to use the standard C Writing Method to declare the "extern C" Link indicator of the function, but one drawback of this is that the member function of the export class cannot be used. In addition, when the call conventions change, even if "extern C" is used, "Name adaptation" still occurs ".

Another solution is to modify the def file. This ensures that all compilers export the same function name (no problem occurs when linking the DLL), but cannot implement the overload of the export function. In addition, the compatibility of the binary structure cannot be guaranteed. Since the C ++ standard does not specify the state of the program running, each compiler may have its own set of processing methods, such as exception handling, can an exception thrown by one compiler be caught in another compiler.

 

2. dll cannot implement cross-language reuse of Classes

DLL is mainly used to implement the "cross-application and language binary sharing" of functions ". Dll can also share classes, but it cannot share classes across languages. It can only be used when static links are used, that is, it can only be used by the same language and compiler.

 

3. dll version upgrade

To realize class reuse in C ++, we generally use the implicit link DLL method. When the link DLL is displayed, the DLL can only export functions, but cannot export classes.
One of the biggest disadvantages of implicit link is the dll version problem: every time a new DLL is updated, the executable program (.exe) must be re-linked once. Otherwise, the program cannot run normally because the information of functions and classes has changed. If the DLL file is forcibly replaced without re-linking the executable program, errors such as failure to find the export function, failure to find the export class, and export class exceptions may occur.

 
4. com -- implements "cross-application and language binary sharing" for classes and functions through "interfaces"

Com itself is also a DLL, even ActiveX control. OCX is actually a DLL. Therefore, dll has a great advantage in reuse, but we have changed the reuse method through the mechanism of COM by formulating complicated com protocols, use DLL in a new way to overcome the inherent defects of the DLL itself, so as to realize a higher level of reuse method. The implementation of the interface is encapsulated in the DLL, and the customer accesses the implementation of the interface through the interface.
1. The "interface" is used to implement "binary sharing across applications and Languages" of the class ",
2. Solved the dll version upgrade problem when the static link is used. You do not need to consider the dependencies between the DLL and the exe. They are loosely combined, you can easily change to a new version of the component, while the application is confused.
3. Solved the incompatibility between compilers. COM does not have the function name duplication problem, because it does not call a function through the function name, but through a virtual function table. Naturally, there is no function name modification problem, this solves the incompatibility between compilers;

4. The path problem no longer exists, because components can be found by checking the registry and stored anywhere, even on other machines;

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.