Win32 DLL and mfc dll encapsulation dialog box

Source: Internet
Author: User
The most common problem about DLL is how to use the dialog box in DLL. This is a common question about how to use resources in DLL. Here we analyze and solve this problem from the Win32 DLL and mfc dll aspects.

1. Win32 DLL
Using the dialog box in Win32 DLL is very simple. You only need to add the dialog box resources in your DLL, And you can set the required control on the dialog box. Then, use the dialogbox or createdialog functions (or other functions with the same function) to create a dialog box and define your own dialog box callback function to process the messages received in the dialog box. The following is an example of how to use the dialog box in Win32 DLL. You can follow these steps:



1) in the VC menu, choose File> New to create a Win32 dynamic-Link Library project named usedlg. Next, select a simple DLL project.



2) in the VC menu, insert-> resource to add a dialog resource with ID idd_dlg_show. Remove the cancel button on the dialog and retain only the OK button. Add a dialog box with ID idd_aboutbox and its caption is about. Save the resource and name the resource file usedlg. RC. Add Resource. h and usedlg. RC to the project.



3) include resource. h In usedlg. app and add the following code:



Hinstance hinst = NULL;

Hwnd hwnddlg = NULL;



Bool callback dlgproc (hwnd hdlg, uint message,

Wparam, lparam );

Bool callback aboutproc (hwnd hdlg, uint message,

Wparam, lparam );

Extern "C" _ declspec (dllexport) void showdlg ();



Bool apientry dllmain (handle hmodule,

DWORD ul_reason_for_call,

Lpvoid lpreserved

)

{

Switch (ul_reason_for_call)

{

Case dll_process_attach:

Hinst = (hinstance) hmodule;

Case dll_process_detach:

Break;

}

Return true;

}



Extern "C" _ declspec (dllexport) void showdlg ()

{

Hwnddlg = createdialog (hinst, makeintresource (idd_dlg_show ),

Null, (dlgproc );

Showwindow (hwnddlg, sw_show );

}



Bool callback dlgproc (hwnd hdlg, uint message,

Wparam, lparam)

{

Switch (Message)

{

Case wm_initdialog:

Return true;



Case wm_command:

If (loword (wparam) = idok)

Dialogbox (hinst, makeintresource (idd_aboutbox ),

Hdlg, (dlgproc) aboutproc );

Return true;

Case wm_close:

Destroywindow (hdlg );

Hwnddlg = NULL;

Return true;

}

Return false;

}



Bool callback aboutproc (hwnd hdlg, uint message,

Wparam, lparam)

{

Switch (Message)

{

Case wm_close:

Enddialog (hdlg, null );

Hwnddlg = NULL;

Return true;

}

Return false;

}



4) compile and generate usedlg. dll and usedlg. Lib.



Next, create an application that calls this dll. The procedure is as follows:



1) in the VC menu, choose File> New to create an MFC Appwizard (exe) project named use. Next, select dialog based and click Finish.



2) Add a button on the Main Dialog Box and double-click this button. The add member function dialog box is displayed. Click OK to enter the void cusedlg: onbutton1 () function. Add a function call in the function: showdlg ();.



3) Add the following code next to the # include statement:



Extern "C" _ declspec (dllexport) void showdlg ();

# Pragma comment (Lib, "Debug/usedlg ")



4) copy the usedlg. dll and usedlg. Lib files generated in the preceding usedlg project to the DEBUG directory of the Use Project.



5. compile and generate use.exe.



Run use.exe and click the button1 button. A non-Modal Dialog Box named dialog is displayed. Click the button above to bring up the modal dialog box about. Run successfully.



Let's review the process of using the dialog box in Win32 DLL.



In DLL, we define two dialog box resources: idd_dlg_show and idd_aboutbox, and export the showdlg function. In the showdlg function, use the createdialog function to create the idd_dlg_show non-modal dialog box and specify the callback function dlgproc in the dialog box. In dlgproc, wm_initdialog, wm_command, and wm_close messages are processed to respond to user actions on the dialog box. When processing the button action, use the dialogbox function to create the idd_aboutbox modal dialog box, specify its callback function as aboutproc, and process the corresponding message in aboutproc.



In EXE, we use the implicit link method to call the DLL, and use the showdlg function exported in the DLL to call the dialog box in the DLL.



Using the dialog box in Win32 DLL is so simple. Let's take a look at how to use the dialog box in mfc dll.
2. MFC DLL
The dialog box used in mfc dll is not as simple as that in Win32 DLL, mainly because there is a module state problem in the MFC program, that is, the problem of duplicate resources. (The term module here refers to an executable program, or a DLL (or a group of DLL) whose operations do not depend on the rest of the application but use the shared copy of the MFC Runtime Library ). The mfc dll we created is a typical instance of this module .)



In each module (exe or DLL), there is a global state data. MFC relies on this global state data to distinguish different modules for proper operations. This type of data includes Windows instance handle (used to load resources), pointer to the current cwinapp and cwinthread object of the application, Ole module reference count, maintain mappings between Windows Object handles and corresponding MFC object instances. However, when an application uses multiple modules, the status data of each module is not within the application scope. On the contrary, each module has its own private copy of the MFC status data. The global status data is called the status of the MFC module.



The module status data is contained in the structure and can always be used by a pointer to the structure. When the code is executed into a module, only the module is in the "current" or "valid" status, MFC can correctly distinguish this module and perform the correct operations.



For example, an MFC application can use the following code to load strings from a resource file:



Cstring STR;

Str. loadstring (ids_mystring );



This code is very convenient, but it masks the fact that ids_mystring in this program may not be a unique identifier. A program can load multiple DLL files. Some DLL files may also use the ids_mystring identifier to define a resource. How does MFC know which resource to load? MFC uses the current module status to find the resource handle. If the current module is not the correct module we want to use, it will produce incorrect calls or errors.



According to the Link Method of the MFC library, an mfc dll has two methods to use the MFC Library: static link to the mfc dll and dynamic link to the mfc dll. Next we will follow these two types of mfc dll to introduce how to switch the current module status to use resources correctly in the mfc dll.

1. dll statically linked to MFC

When the rule DLL and the MFC library are statically linked to the MFC Library, the MFC Library cannot be shared at this time, so the MFC always uses the module status of the DLL it is linked. In this way, the status of the management module does not exist. However, the disadvantage of using this method is that the DLL program will become larger and duplicate code will be left in the program. The example below demonstrates this. In this example, follow these steps:



1) in the VC menu, choose File> New to create a project named dllstatic MFC Appwizard. Next, select regular DLL with MFC statically linked.



2) Add a dialog box resource in the project. Its ID is idd_aboutbox. In resource. H, change the idd_aboutbox value to 100.



3) define the following functions in dllstatic. cpp:



Void showdlg ()

{

Cdialog DLG (idd_aboutbox );

DLG. domodal ();

}



4) add a line: showdlg to the exports statement in the dllstatic. Def file to export the showdlg function.



5) compile and generate dllstatic. dll and dllstatic. Lib.



Continue to use the use project in the previous section, copy the previously generated dllstatic. dll and dllstatic. Lib files to the DEBUG directory of the project, and



Extern "C" _ declspec (dllexport) void showdlg ();

# Pragma comment (Lib, "Debug/usedlg ")



The two rows are changed:



Void showdlg ();

# Pragma comment (Lib, "Debug/dllstatic ")



Compile and run use.exe. Click the button to see the modal dialog box in dllstatic.



When the button above the DLL is displayed, the modal dialog box in the DLL is displayed. Note: When you use a rule DLL that is statically linked to the MFC, the status of the management module does not exist.

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.