MFC DLL wizard (2)

xiaoxiao2021-03-06  28

A more serious problem with DLL is the compatibility problem between the compiler. Different compilers have different implementations of C functions in binary levels. So it is very troublesome to C DLL, if the compiler is different. If you create an MFC extended DLL, there is no problem because it can only be dynamically connected to the MFC's client application. It is not the focus of this article discussed here. 1. Re-compiling the problem, let's take a look at the problem that may be encountered in the actual situation: If we need to modify the CMYClass class in the DLL, let it have the same function and member variable, but give a private member variable int type, now the size of the CMYClass object is 34 bytes. When this new DLL is directly replaced with the customer, the original 30-byte size DLL is replaced, the client application expects the 30-byte size object, and now it has become a 34-byte size object, bad, customer The program is wrong. Similar questions, if not exporting the CMYClass class, CMYCLASS in the exported function, changing the size of the object still has problems. The only way to modify this issue is to replace the header file of the CMYClass in the client, all recompilated the entire application, allowing the customer to use the size of 34 bytes. This is a serious problem, sometimes there is no source code for the client program, then we can't use this new DLL. Second, the solution In order to avoid recompiling the client, two methods are introduced here: (1) Use the interface class. (2) Use the static function of creating and destroying classes. 1. Use the interface class interface, which is to create a second class. It is used as an interface to be exported, so when the export class changes, it is not necessary to recompile the client because the interface class does not change. Assuming the exported CMYClass class has two functions Functiona functionb. Now create an interface class cmyinterface, below is the code of the header file of the CMYINTERFACE class in the DLL: # include "myclass.h" CLASS _DECLSPEC (DLLEXPORT) CMYINTERFACE {CMYCLASS * PMYCLASS; CMYINTERFACE (); ~ cmyinterface (); public: INT FunctionA (int); INT functionb (int);}; and the header file in the client program is slightly different, no include statement, because the client does not have its copy.

On the contrary, use a CMYClass forward statement, even if there is no header, Class_Declspec (DLLEXPORT) CMYINTERFACE {class cmyclass; // Declaration CMYCLASS * PMYCLASS; cmyinterface (); ~ cmyinterface (); public: int functiona (int); int functionb (int);}; CMYINTERFACE in the DLL is as follows: CMYINTERFACE :: Cmyinterface () {PMYCLASS = New CMYClass ();} cmyinterface :: ~ cmyinterface () {delete PMYClass;} int Cmyinterface :: functiona () {Return PMYCLASS-> Functiona ();} int cmyinterface :: functionb () {return PMYCLASS-> FunctionB ();} ..... The CMYINTERFACE class is exported Provide your own corresponding functions. The client has no contact with CMYCLASS, so that CMYCLASS will not have problems because the size of the CMYINTERFACE class has not changed. Even in order to access the new variables in CMYCLASS, there will be no problem with the CMYINTERFACE class. However, this method also has a significant problem, and each function and member variables of the derived class should be achieved, sometimes this interface class is very large. At the same time, the time required for the customer program call is added. Added the overhead of the program. 2. Use static functions to create and destroy class objects using a static function. When you create an export class, add two static public function creteme () / destroyme (), header files as follows: class _declspec () CMYCLASS {cmyclass (); ~ cmyclass (); public: static cmyclass * createme () Static void destroyme (CMYCLASS * PTR);}; implementation function is: CMYCLASS * CMYCLASS :: CMYCLASS () {Return New CMYClass :: Destroyme (CMYCLASS * PTR) {Delete PTR;} then like other classes Export the CMYCLASS class, this time is slightly different in the client program. If you want to create a CMYCLASS object, it should be: CMYCLASS X; CMYCLASS * PTR = CMYCLASS :: CreateMe (); Remove after use: CMYCLASS :: Destroyme (PTR);

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

New Post(0)