Use resources in DLL (1)
The most commonly seen on the 9CBS Forum About DLL is how to use dialogs in DLL, which is a very common problem with how to use resources in DLL. Here we analyze and solve this problem from both aspects of Win32 DLL and MFC DLL.
1. Win32 DLL
Using dialog box in Win32 DLL is simple, you only need to add dialog resources in your DLL, and you can set the control you need above on the dialog. Then use Dialogbox or CreateDialog to create a dialog box and define your own dialog box callback function to process the message received. By following a specific example, learn how to use the dialog box in Win32 DLL, follow these steps:
1) In the VC menu, File-> New New Win32 Dynamic-Link Library Project is named USEDLG, the next step Select A Simple DLL Project.
2) Insert-> Resource in the VC menu Add a IDD_DLG_SHOW Dialog resource, remove the CANCEL button on this Dialog, and only the OK button is left. Add another ID to IDD_ABOUTBOX dialog, which capens is About. Save this resource and name the resource file Usedlg.rc. And add resource.h and usedlg.rc to the project.
3) Contains Resource.h in usedlg.app, and add the following code:
Hinstance hinst = null;
HWND HWNDDLG = NULL;
Bool Callback DlgProc (HWND HDLG, UINT MESSAGE,
WPARAM WPARAM, LPARAM LPARAM;
Bool Callback AboutProc (HWND HDLG, UINT MESSAGE,
WPARAM WPARAM, LPARAM 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) DLGPROC;
ShowWindow (hwnddlg, sw_show);
}
Bool Callback DlgProc (HWND HDLG, UINT MESSAGE,
WPARAM WPARAM, LPARAM 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 WPARAM, LPARAM LPARAM)
{
Switch (Message)
{
Case WM_Close:
EndDialog (HDLG, NULL);
HWnddlg = NULL;
Return True;
}
Return False;
}
4) Compile to generate usedlg.dll and usedlg.lib.
Next we establish an application that calls this DLL, the steps are as follows:
1) In the VC menu, File-> New has created a new MFC AppWizard (EXE) project named Use, and then click the finish button after you select Dialog Based.
2) Add a button on the main dialog, then double-click this button, pop up the Add Member Function dialog box, directly click OK to enter the void cusedlg :: onbutton1 () function. And add a function call to this function: showdlg () ;.
3) Follow the following code followed by the #include statement:
EXTERN "C" __Declspec (dllexport) void showdlg ();
#pragma comment (Lib, "Debug / UseDLG")
4) Copy the usedlg.dll and usedlg.lib generated above the Usedlg project to the debug directory of the USE project.
5) Compile to generate use.exe.
Run Use.exe, click the Button1 button, you can see a non-modular dialog box named Dialog. Click the button above to pop up the modal dialog. Run success.
Let's review the process of using the dialog box in Win32 DLL.
In DLL, we define two dialog resources: IDD_DLG_SHOW and IDD_ABOUTBOX, and export the function showdlg. The non-modular dialog IDD_DLG_SHOW is created using the CREATEDIALOG function in the function showdlg, and specifies the callback function DLGProc of the dialog. WM_INITDIALOG, WM_COMMAND, and WM_CLOSE messages are processed in DLGProc to respond to the action made by the user on the dialog. When processing the button action, create an IDD_ABOUTBOX using the Dialogbox function, specify its callback function to AboutProc, and process its corresponding message in the ABOUTPROC.
In EXE, we use implicit link to call DLL and use the showdlg function exported in the DLL to call dialogs in the DLL.
Using dialog box in Win32 DLL is as simple, let's take a look at how to use the dialog in the MFC DLL.
2. MFC DLL
Using dialogs in MFC DLL is not as simple as Win32 DLL, mainly because there is a problem with module status in the MFC program, that is, resource duplicate issues. (The term module here refers to a DLL (or a set of DLLs) of a DLL (or a set of DLL) that does not depend on the rest of the application but use the MFC run library. The MFC DLL we created is A typical example of this module.)
In each module (EXE or DLL), there is a global state data, and the MFC relies on this global state data to distinguish between different modules to perform the correct operation. This data includes: Windows universal handle (for loading resources), pointing to the pointer of the application current CWINAPP and CWINTHREAD object, OLE module reference count, and various mappings that maintain the Windows object handle and the corresponding MFC object instance Wait. However, when the application uses multiple modules, the status data of each module is not the scope of the application. Instead, each module has a private copy of the MFC status data. This global state data is called the MFC module state. The state data of the module is included in the structure and can always be used by a pointer to the structure. When the code enters a module when executed, only the status of this module is "current" or "valid" state, the MFC can correct this module and perform the correct operation.
For example, the MFC application can load strings from the resource file using the following code:
CString Str;
Str.LoadString (IDS_MYSTRING);
It is very convenient to use this code, but it masks the fact that Ids_MyString in this program may not be unique identifiers. A program can load multiple DLLs, some DLLs may also define a resource with the IDS_MYSTRING identifier. How do MFC know which resource should I load? The 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 generate incorrect calls or errors.
According to the Link method of the MFC library, a MFC DLL has two ways to use the MFC library: static links to the MFC DLL and dynamic links to the MFC DLL. Below we will introduce how to switch the current module status in the correct way to use resources in the MFC DLL in accordance with these two types of MFC DLL.
1, static link to the MFC DLL
Static links to the rule DLL of the MFC and the MFC library static link, then the MFC library cannot be shared, so the MFC always uses the module status of the DLL it linked. This does not have problems with the status of the management module. However, the disadvantage of using this method is that the DLL program will become large, and repeated code will be left in the program. The example given below verifies this. This example can be done in accordance with the following steps:
1) In the VC menu.
2) Add a dialog resource in the project, which ID is: IDD_ABOUTBOX. And change the value of IDD_aboutbox to 100 among resource.h.
3) Define the following functions in dllstatic.cpp:
void showdlg ()
{
CDIALOG DLG (IDD_ABOUTBOX);
Dlg.domodal ();
}
4) Add a line in the Exports statement in the dllstatic.def file to export the ShowDLG function.
5) Compile DllStatic.dll and DllStatic.lib.
Continue to use the USE project in the previous section to copy the previously generated dllstatic.dll and dllstatic.lib to the project's debug directory, and will
EXTERN "C" __Declspec (dllexport) void showdlg ();
#pragma comment (Lib, "Debug / UseDLG")
These two rows are changed to:
Void showdlg ();
#pragma comment (lib, "debug / dllstatic") compiles and runs Use.exe. Click the button to see the modal dialog box in DllStatic pops up.
In this case, you can notice that the About dialog resource defined in the DLL is identical to the About dialog resource ID defined in the exe, but when we click on the button above Use.exe, the modality of the DLL is Dialog. Note When using a static link to the rule DLL of the MFC, there is no problem with the status of the management module.
Serial: Use resources in DLL (2)
---------- Rivershan Original in 2004.3.8, please indicate the source