What is Dll?
Stands for "Dynamic Link Library. "a dll (. dll) file contains a library of functions and other information that can be accessed by a Windows program. when a program is launched, links to the necessary. dll files are created. if a static link is created,. dll files will be in use as long as the program is active. if a dynamic link is created,. dll files will only be used when needed. dynamic links help programs use resources, such as memory and hard drive space, more efficiently.
Dialog Box: the dialog box can be divided into modal and modeless. Create modal dialog box in Win32 Dll. Creating a modal dialog box in a Win32Dll is very simple. We all know that the Win32 function DialogBox can create a modal dialog box, and the messages in the modal dialog box are independent of the message loop of the main program, therefore, you can create your own window in the Dll. The following is an example of the create modal dialog box in Win32 Dll: // name: Modal_Dialog.cpp # include <windows. h> # include "resource. h "bool callback DlgProc (HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {switch (Msg) {case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch (wParam) {case IDOK: MessageBox (NULL, "This is an example of a modal dialog box", "prompt", MB_ OK); EndDialog (hwnd, 0); return TRUE;} break ;} return FALSE;} extern "C" _ declspec (dllexport) void ShowDialog () {HINSTANCE hinst; hinst = LoadLibrary ("Modal_Dialog.dll"); // obtain the instance handle DialogBox (hinst, MAKEINTRESOURCE (IDD_DIALOG1), NULL, DlgProc ); freeLibrary (hinst);} // name: Test. cpp ...... Typedef void (* fp) (); HINSTANCE hDll; fp Show; hDll = LoadLibrary ("Modal_Dialog.dll"); Show = (fp) GetProcAddress (hDll, "ShowDialog "); (* Show) (); FreeLibrary (hDll );...... Create a modeless dialog box in Win32 Dll. Different from the modal dialog box, messages in the modeless dialog box must pass through the message loop of the main program. In this way, the message loop of the dialog box must be processed in the main program that calls the Dll, in this way, Dll cannot be used to complete componentized development. In fact, we can create a window class for the Modeless dialog box. We know that the window process belongs to the window class, so we have a message loop in the main window. // Name: Modeless_Dialog.cpp # include <windows. h> # include "resource. h "lresult callback WndProc (HWND, UINT, WPARAM, LPARAM); bool callback DlgProc (HWND, UINT, WPARAM, LPARAM); extern" C "int _ declspec (dllexport) showDialog () {static TCHAR szAppName [] = TEXT ("Modeless_Dialog"); HWND hwnd; MSG msg; WNDCLASS wndclass; HINSTANCE hInstance; HWND hDialog; hInstance = LoadLibrary ("modeless_dig.alodll" ); Wndclass. style = CS_HREDRAW | CS_VREDRAW; wndclass. lpfnWndProc = WndProc; wndclass. cbClsExtra = 0; wndclass. cbWndExtra = dlgw.wextra; // Note! Wndclass. hInstance = hInstance; wndclass. hIcon = LoadIcon (hInstance, szAppName); wndclass. hCursor = LoadCursor (NULL, IDC_ARROW); wndclass. hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); wndclass. lpszMenuName = NULL; wndclass. lpszClassName = szAppName; hwnd = CreateWindow (szAppName, TEXT ("Modeless_Dialog"), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // ShowWindow (hwnd, iCmdShow); // The Main Window // UpdateWindow (hwnd); hDialog = CreateDialog (hInstance, MAKEINTRESOURCE (IDD_DIALOG1), hwnd, DlgProc); while (GetMessage (& msg, NULL, 0, 0) {if (hDialog = 0 |! IsDialogMessage (hDialog, & msg) {TranslateMessage (& msg); DispatchMessage (& msg) ;}} return msg. wParam;} bool callback DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_INITDIALOG: return true; case WM_COMMAND: switch (wParam) {case (IDOK): MessageBox (NULL, "This is an example of the modeless dialog box", "prompt", MB_ OK); DestroyWindow (hDlg); PostQuitMessage (0 ); return TRUE ;}} retur N FALSE;} lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_DESTROY: PostQuitMessage (0); return 0 ;} return DefWindowProc (hwnd, message, wParam, lParam);} CreateDialog is used to create a window, and the visible attribute must be set to True in the dialog box template. Only in this way can the dialog box be displayed. if (hDialog = 0 |! IsDialogMessage (hDialog, & msg) is used to distribute messages. All messages belonging to the dialog box are delivered to DlgProc for processing. To exit the processing dialog box, you must use the DetroyWindow function and send an Exit message so that the undisplayed main window can exit the message loop without causing memory leakage.
Create modal dialog box in MFC regular DLLCreating a modal dialog box in MFC Regular Dll is very simple. The Cdialog base class provides a method DoModal () to create a modal dialog box. Therefore, the modal dialog box is created in MFC Regular Dll, you only need to inherit the basic class of the dialog box and call DoModal in the export function.
Create a modeless dialog box in MFC regular DLL.Because the MFC application framework only encapsulates Win32 functions, the create modeless dialog box in the MFC Regular Dll actually corresponds
Method 1 for generating a dialog box in DLL (Win32 DLL)The Internal principles of the modeless dialog box are the same. The difference is that it requires a little skill in the MFC framework. We know that in Win32 SDK programming, there is a main window handle hwnd. Similarly, in MFC, there is also such a variable, but it is now encapsulated into CwindThread. We can go to AFXWIN. h, we can see the following definition :...... CWnd * m_pMainWnd; // main window (usually same AfxGetApp ()-> m_pMainWnd )...... We know that CWinApp inherits from CWinThread. Let's take a look at the encapsulation of the window process. In MFC, The CFrameWnd category can be used to create a window and define the window process internally. Specifically, it is CreateWindow, messageLoop is implemented in this category. With the above basics, let's take a look.
Method 1 for generating a dialog box in DLL (Win32 DLL)Steps for generating the modeless dialog box in this article: 1: we first created a main window, but it does not display it. What we really need is the window handle hwnd.2: Create a dialog box, set the owner handle of the dialog box to hwnd. 3: Message Delivery. The above step actually hides a technique in it: We can discard hwnd, that is, we can set the handle of the dialog box to hwnd, so that we can omit step 3, we don't need to distribute messages in the message loop, so our code looks like this ...... Hwnd = CreateWindow (szAppName, TEXT ("Modeless_Dialog"), transform | WS_CLIPCHILDREN, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // ShowWindow (hwnd, NULL, iCmdShow); // The main window is not displayed here // UpdateWindow (hwnd); hwnd = CreateDialog (hInstance, MAKEINTRESOURCE (IDD_DIALOG1), NULL, DlgProc); while (GetMessage (& msg, NULL, 0, 0) {TranslateMessage (& msg); DispatchMessage (& msg );}
......
OK. Now we can follow the above steps to generate a dialog box in the MFC Regular DLL. 1: Create an MFC Regular DLL.2 using AppWizard: derive a new CmyFrameWnd class from CFrameWnd, because we only need to generate a non-display window, we can use the default operation for all actions. Class CMyFrame: public CFrameWnd {public: CMyFrame (); DECLARE_MESSAGE_MAP ()}; 3: A New CMyDialog class is derived from CDialog. 4: CWinApp InitInstance () is used to initialize the application. Therefore, we add the following code to the InitInstance method of CMyWinApp: m_pMainWnd = new CMyFrame (); m_pMainWnd = pDlg; // pDlg is a pointer to the CmyDialg object. 5: Write export function: extern "C" void _ declspec (dllexport) Show () {pDlg-> DoModal (); // display dialog box} from the above steps, we can see that the method for displaying the modeless dialog box in the MFC Regular DLL still follows the Win32 DLL method, of course, because of the object-oriented feature of the MFC, as long as we follow this method, we can implement more methods, but here we will introduce the simplest and most direct method. The method for generating the dialog box in the MFC Regular Dll is discussed earlier, and there is also an MFC Extention Dll, but because of its many restrictions, there is a tendency to be eliminated, so it is best to use Win32 Dll or MFC Regular Dll. Of course, the simplest method is the next C # Dll, because everything has been encapsulated. net framework, what window process, what handle, message loop, and so on do not have to worry about, the only drawback is that the target machine needs to be installed. net FrameWork:
1: create a new class library in C. 2: Create a New form class (in C #, everything is an object, so simply add a form) 3: Add a method3 in the new class: call the form class related methods: // code that generates the modal Dialog Box Form1 myModalDialog = new Form1 (); myModalDialog. showDialog (); // code used to generate the modeless dialog box Form1 myModelessDialog = new Form1 (); myModelessDialog. show (); // It is also convenient to use Modal_Dialog.CMyDialog test = new Modal_Dialog.CMyDialog (); test. showModalDialog (); test. showModelessDialog (); of course, in addition to the direct reference above, you can also load it through the Assembly LoadFrom.