Knowledge about MFC DLL

Source: Internet
Author: User
Tags export class

Although everything that can be implemented using DLL can be implemented using COM, dll has many advantages and is easier to create. This article will discuss how to use MFC to create different types of DLL and how to use them.
 1. Different types of DLL
You can use MFC to generate two types of DLL: MFC extension DLL and conventional DLL. Conventional dll can be divided into dynamic and static connections. Visual c ++ can also generate Win32 DLL, but it is not the main object discussed here.
1. MFC extension DLL
Each dll has a certain type of Interface: variables, pointers, functions, class accessed by the client program. Their role is to allow the client program to use the DLL. The MFC extension DLL can have a C ++ interface. That is, it can export the C ++ class to the client. The exported function can use the C ++/mfc data type as a parameter or return value. When exporting a class, the client can create a class object or derive this class. You can also use DLL and MFC In the DLL.
The MFC class library used by Visual C ++ is also saved in a DLL. The MFC extension DLL dynamically connects to the DLL of the MFC code base, and the client program must also dynamically connect to the DLL of the MFC code base. (The two DLL mentioned here, one is our own DLL and the other is the DLL for installing the MFC class library.) Currently, there are multiple versions of the DLL in the MFC code library, both the client program and the extended dll must use the same version of the MFC code DLL. So in order for the MFC extension DLL to work well, both the extension DLL and the client program must be dynamically connected to the MFC code library DLL. The dll must be run on the computer where the client is running.
2. Conventional DLL
One problem with using the MFC extension DLL is that the DLL can only work with the MFC client program. If you need a more widely used DLL, it is best to use the conventional DLL because it is not limited by the MFC. Conventional DLL also has a disadvantage: it cannot send pointer or reference to the MFC derived class and object with the client program. In a word, the interfaces of conventional DLL and client programs cannot use MFC, but they can still be used inside the DLL and client programs.
When the DLL of the MFC code library is used inside a conventional DLL, it can be a dynamic or static connection. If it is a dynamic connection, that is, the MFC Code required by the conventional DLL is not built into the DLL. This is a bit similar to the extended DLL, And the DLL of the MFC code library must be required on the computer where the DLL runs. If it is a static connection, the conventional DLL already contains the required MFC code, so that the DLL size will be relatively large, but it can run normally on a computer without the MFC code library DLL.

 

2. Create a DLL
By using the Wizard feature provided by Visual C ++, you can easily create a DLL that does not complete any substantive tasks. Here we will not talk much about it. The main task is how to add functions to the DLL, and use the DLL in the client program.
1. Export class
After the framework is created using the wizard, you can add the. cpp. h file of the class to be exported to the DLL, or use the Wizard to create the C ++ herder file/C ++ source file. To export this class, add "_ declspec (dllexport)" to the class declaration, for example:
Class _ declspec (dllexport) cmyclass
{
... // Declaration
}
If you create an MFC extension DLL, you can use the macro: afx_ext_class:
Class afx_ext_class cmyclass
{
... // Declaration
}
In this way, the export class method is the simplest. You can also use the. Def file for export.
2. Export variables, constants, and objects
In many cases, you do not need to export a class, so that the DLL can export a variable, constant, and object. to export them, you only need to make a simple declaration: _ declspec (dllexport) int Myint;
_ Declspec (dllexport) extern const colorref mycolor = RGB (0, 0 );
_ Declspec (dllexport) crect rect (10, 10, 20, 20 );
To export a constant, the keyword extern must be used; otherwise, a connection error occurs.
NOTE: If your program recognizes this class and has its own header file, only one class object can be exported. If a class is created in the DLL, the class cannot be recognized without using the header file.
When exporting an object or variable, each client program that loads the DLL has its own copy. That is, if two programs use the same DLL, the modifications made by one application will not affect the other.
When exporting, we can only export global variables or objects in the DLL, but not local variables and objects. Because they do not exist after the scope, the DLL will not work normally. For example:
Myfunction ()
{
_ Declspec (dllexport) int Myint;
_ Declspec (dllexport) cmyclass object;
}
3. Export Functions
The export function is similar to the export variable/object. You only need to add _ declspec (dllexport) to the start position of the function prototype:
_ Declspec (dllexport) int myfunction (INT );
If it is a conventional DLL, it will be used with the program written in C. The declaration method is as follows:
Extern "C" _ declspec (dllexport) int myfunction (INT );
Implementation:
Extern "C" _ declspec (dllexport) int myfunction (int x)
{
... // Operation
}
If you create a conventional DLL that dynamically connects to the MFC code library DLL, you must insert afx_manage_state as the first line of the export function. Therefore, the definition is as follows:
Extern "C" _ declspec (dllexport) int myfunction (int x)
{
Afx_manage_state (afxgetstaticmodulestate ());
... // Operation
}
Sometimes, for the sake of security, it is added to every common DLL, and there will be no problem, but this macro is invalid during static connection. This is the method for exporting functions. Remember that only the MFC extension DLL can use the data type of MFC for parameters and return values.
4. Export pointer
Export pointers as follows:
_ Declspec (dllexport) int * pint;
_ Declspec (dllexport) cmyclass object = new cmyclass;
If the pointer is initialized at the same time, you need to find a suitable local class to release the pointer. There is a function dllmain () in the extended DLL (). (Note that the two l in the function name should be lowercase letters), you can process the pointer in this function:
# Include "myclass. H"
_ Declspec (dllexport) cmyclass * pobject = new cmyclass;
Dllmain (hinstance, DWORD dwreason, lpvoid lpreserved)
{
If (dwreason = dll_process_attach)
{
.....//
}
Else if (dwreason = dll_process_detach)
{
Delete pobject;
}
}
A common dll has a class object processing DLL derived from cwinapp. You can use the Class Wizard to add the initinstance/exitinstance function.
Int cmydllapp: exitinstance ()
{
Delete pobject;
Return cwinapp: exitinstance ();
}

3. Use DLL in the client program
When compiling a DLL, two. DLL files and. Lib files will be created. First, copy the two files to the client program project folder. Pay attention to the DLL and client program version issues. Try to use the same version, both of which use release or both are DEBUG Versions.
Then you need to set the Lib file in the client program and open the Project Settings ---> link ---> Object/library modules and enter the Lib file name and path. For example, debug/sampledll. Lib. In addition to the DLL and Lib files, the client program needs to export header files for classes, functions, objects, and variables. The keyword for importing and adding is _ declspec (dllimport), for example:
_ Declspec (dllimport) int myfunction (INT );
_ Declspec (dllimport) int Myint;
_ Declspec (dllimport) cmyclass object;
Extern "C" _ declspec (dllimport) int myfunction (INT );
In some cases, to import the class, add the header file of the corresponding class to the client program. The difference is that the flag of the class declaration should be modified:
Class _ declspec (dllimport) cmyclass. If you create an extended DLL, both of them are:
Class afx_ext_class cmyclass.

 

 

In addition, you only need to add afx_ext_class to the class to get the extension DLL. By default, all the member functions of this class have been exported.

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.