Use resources in DLL (1)

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

1. The Win32 DLL dialog box in Win32 DLL is very simple. You only need to add 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) In usedlg. the app contains resource. h, 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) {C ASE dll_process_attach: hinst = (hinstance) hmodule; Case dll_process_detach: break;} return true;} extern "C" _ declspec (dllexport) void showdlg () {hwnddlg = createdialog (hinst, makeintresource (outputs), null, (dlgproc); showwindow (hwnddlg, sw_show);} bool callback dlgproc (hwnd hdlg, uint message, wparam, lparam) {Switch (Message) {Case wm_initdialog: Return true; Case wm_c Ommand: 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 us Edlg. Lib. Next, create an application that calls this dll. The procedure is as follows: 1) Create an MFC Appwizard (exe) project named use in File> new in the VC menu, 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 after the # include statement: extern "C" _ declspec (dllexport) void showdlg (); # pragma comment (Lib, "Debug/usedlg ") 4) run the usedlg generated in the preceding usedlg project. DLL and usedlg. copy the Lib files 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. the dialog box used by mfc dll 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 a string 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. If the DLL that is statically linked to MFC is statically linked to the Rule DLL of MFC and the static link of the MFC library, the MFC Library cannot be shared at this time, so MFC always uses the module status of the DLL that 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, you can follow the steps below: 1) create a project named dllstatic MFC Appwizard in File> new in the VC menu, and then 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) In the dllstatic. the following functions are defined in CPP: void showdlg () {cdialog DLG (idd_aboutbox); DLG. domodal ();} 4) in dllstatic. add a line: showdlg to the exports statement in the 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. DLL and dllstatic. copy the Lib files to the DEBUG directory of the project, and copy extern "C" _ declspec (dllexport) void showdlg (); # pragma comment (Lib, "Debug/usedlg ") change the two lines to void showdlg (); # pragma comment (Lib, "Debug/dllstatic") to 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.