The MFC dynamic library encapsulates the dialog interface (Dialog)

To encapsulate the pop-up window in the MFC dynamic library, you first need to select "MFC Extension DLL" when MFC creates the dynamic library:

After creation, create a new dialog box, and create a class for the dialog box to bind to it, and then the various coding methods of the dialog box are the same as the MFC application. After completing the pop-up window logic, remember to give the class and class in the header file. Add a __declspec(dllexport) between the names to export the class and compile it. However, this usage is because another application must include the header file of the dialog box, which causes the application to include both the resource.h used by the application itself and the resource.h used by the dynamic library, so when defining the resource name, try to Don't repeat it, but I have tried it. Even if I repeat it, there will be a warning when compiling, but it can still be compiled and run successfully. For example, in the example code I posted, IDD_LOGIN is the conflicting macro definition that I deliberately modified. .
First post the various files of the dynamic library:
resource.h of the dynamic library:

// Microsoft Developer Studio generated include file.
// Used by dlgdll.rc
#define IDD_LOGIN                       3000
#define IDC_UNAME                       3000
#define IDC_UPASS                       3001

// Next default values for new objects
#define _APS_NEXT_RESOURCE_VALUE        3001
#define _APS_NEXT_COMMAND_VALUE         32771
#define _APS_NEXT_CONTROL_VALUE         3002
#define _APS_NEXT_SYMED_VALUE           3000

Login.h of the dynamic library

#if !defined(AFX_LOGIN_H__E89E0B64_27D5_4647_9BFF_DBEFF5152794__INCLUDED_)
#define AFX_LOGIN_H__E89E0B64_27D5_4647_9BFF_DBEFF5152794__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "resource.h"

// CLogin dialog

class __declspec(dllexport) CLogin : public CDialog
// Construction
	CLogin(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
	enum { IDD = IDD_LOGIN };
		// NOTE: the ClassWizard will add data members here

// Overrides
	// ClassWizard generated virtual function overrides
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation

	// Generated message map functions
	virtual void OnOK();

	CString GetUserName() {return m_username;}
	CString GetPassword() {return m_password;}

	CString m_username;
	CString m_password;

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_LOGIN_H__E89E0B64_27D5_4647_9BFF_DBEFF5152794__INCLUDED_)

Login.cpp of the dynamic library, enter the user name and password, click the OK button, before the pop-up window disappears, save the user name and password into the private member variable of the object.

// Login.cpp : implementation file

#include "stdafx.h"
#include "Login.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

// CLogin dialog

CLogin::CLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CLogin::IDD, pParent)
		// NOTE: the ClassWizard will add member initialization here

void CLogin::DoDataExchange(CDataExchange* pDX)
		// NOTE: the ClassWizard will add DDX and DDV calls here


// CLogin message handlers

void CLogin::OnOK() 
	CString username, password;
	GetDlgItemText(IDC_UNAME, username);
	GetDlgItemText(IDC_UPASS, password);
	if (username.IsEmpty()) {
		MessageBox("please enter user name");
	if (password.IsEmpty()) {
		MessageBox("Please enter password");
	m_username = username;
	m_password = password;


Dynamic library dialog:

Here's the MFC application calling the DLL method:
First, add a login button to the dialog box of the pop-up application:

Add the import header file and dynamic library to the StdAfx.h header file:

#include "../dlgdll/Login.h"
#ifdef _DEBUG
#pragma comment(lib, "../dlgdll/Debug/dlgdll.lib")
#pragma comment(lib, "../dlgdll/Release/dlgdll.lib")

Double-click the login button, write the response function: call the dialog box, if the user presses the OK button and the pop-up window disappears, then the user name and password can be retrieved from the object.

void CTestdllDlg::OnLogin() 
	CLogin dlg;
	int result = dlg.DoModal();
	if (result == IDOK) {
		MessageBox("username:" + dlg.GetUserName() + " password:" + dlg.GetPassword());


Compilation log:

--------------------Configuration: testdll - Win32 Debug--------------------
Compiling resources...
d:\vscode\testdll\resource.h(9) : warning C4005: 'IDD_LOGIN' : macro redefinition
d:\vscode\dlgdll\resource.h(5) : see previous definition of 'IDD_LOGIN'
d:\vscode\testdll\resource.h(9) : warning C4005: 'IDD_LOGIN' : macro redefinition
d:\vscode\dlgdll\resource.h(5) : see previous definition of 'IDD_LOGIN'
Generating Code...
Creating library Debug/testdll.lib and object Debug/testdll.exp
testdll.exe - 0 error(s), 2 warning(s)

Tags: MFC dll

Posted by suntra on Sun, 01 May 2022 14:44:56 +0300