Module multiplexing - C ++ class, DLL and COM

zhaozj2021-02-16  48

This article is just a general discussion of C , DLL and COM. Please refer to "COM Nature", this article is also a reading note or summary of the first chapter of "COM Nature". If there is something wrong, I hope everyone criticizes the correct.

The common way of using C write code under Windows is probably the C class, DLL, and COM 1, C code reused defects before the C developer multiplexed code, is probably a set. H and .cpp File, then join these files to your own project compile, connect, and finally generate an EXE file. This type of code level is equipped with the following issues 1, C programmers usually modify the code provided by others according to their own needs. Developers want to use code, usually understand the intent of the code based on source code. More old, it is necessary to understand the ideas and coding styles of code providers. 2, C code level reuse results in waste of hard disk and memory space, if there is N software on the machine, in each software There will be a compile-generated code of Class CA in the storage space. When this N software is executed, the N × SizeOf (CA) size memory space 3 is occupied, and the software is released, unless recompiled the entire software. Modifying if the class CA has a bug, to modify the CA implementation to fix this bug, then only recompile the entire software. Software does not have 2-based modular features. Second, how to solve the above problems - the introduction of the DLL in Window, a good technology is the dynamic link library (DLL), with DLL technology, customers can share the DLL code, so in the user machine How many software uses this DLL, there is only one copy of the DLL. And the DLL is also only .h file, hidden the implementation details of the code. Third, DLL multiplexing problems 1, compiler, connector incompatibility issues In order to support overload, C compiler modifies function names (name adaptation, interested, "can be referred to" in-depth exploration of C object model Chapter 4 - -Function Language "), and the method of modifying the function name each C compiler is usually different. One solution to this problem is to declare the extern "C" link indicator, and one of the defects do not use the Extern "C" to declare member functions. Another solution is to modify the DEF file, so that all compilers export the same function name to ensure that the DLL is compatible when the link is linked. However, this does not guarantee the compatibility of the 2-encyclopedia. Since the C standard does not specify the state of the program, each compiler may have its own way of handling, for example, when doing an abnormal process, a compiler throws out Exceptions, in another compiler, may not be able to have a 2, DLL fatal weaknesses - version conflict problem DLL version issues are notorious. The classic method for solving the version conflict is to rename the DLL file name (this is also the MFC solution, MFC42D ... .mfc42ud, etc.). Fourth, sacrifices the magic weapon, object-oriented principle-interface and implementation of the separation DLL version problem is the key to the C compilation model and the tight coupling relationship between the object, the C compilation model requires customers to understand the object's memory layout for Object allocates memory. The fundamental solution of this issue is to separate interfaces and implementation. The implementation is encapsulated inside the DLL, and the customer will access the implementation through the interface. In this way, the implementation portion of the multiplexed module is not included in the customer code. 5. The solution of COM is strictly separated interface and implementation, and customers can only access components through interfaces. It is guaranteed that no detail of any implementation is exposed to the customer. When the customer uses components to allocate memory, only the VPTR pointer in the COM interface. COM uses interface inheritance to separate the interface and implementation. Most compilers are consistent with the memory layout generated by VPTR and VTBL.

This solves the incompatibility relationship between the compiler. Of course, there are still many uses of COM. COM technology is also very complicated, I don't want to say more, one is because I am lazy, the second is because I am still delicious, "COM Nature" is a good book. Learn COM must see.

Finally, I want to talk about the current software development environment. Due to the launch of .NET, many people are asking if COM is still worth it. Look at the current software, IE, Office, MSN includes the core of .NET, which is not built with COM, and we learn COM, the key is its design idea, knowing what it is, understanding the design behind it Motivation, it is the same with COM or use DLL, if it is just use, don't learn. The DLL is just a release form of COM. Even in other platforms, this idea is also very useful. It reflects OO ideas - interface and implementation separation, open-closed principles, programming for interface, and even forced you to use some design patterns. I think this is the value of learning COM. . Moreover, the implementation of the COM is studied in the source code of the ATL, and some practical technologies and design patterns can also be learned. And these things will never have time. . Technology is evolving, but new technologies are also generated by old technologies. The father of WTL is also suggested to learn .NET before school. In fact, on the Window platform, COM always has its one. . . .

转载请注明原文地址:https://www.9cbs.com/read-28004.html

New Post(0)