MFC project packaged into DLL

1. Preparation: ①The MFC project solution (WinForm1) that has been tested;

             ②Create a new MFC DLL solution (WinDLL);

             ③Create a new MFC dialog project (DlgTest);

2. Copy the MFC project directory (.Cpp, .H, .rc, res, etc.) to the DLL project directory, where the rc name needs to be changed to the DLL project name, (.rc is the file that stores MFC resources, and the res folder is generally Store pictures that need to be added, etc.)

Sometimes the compilation fails to prompt error RC2135, which may be a problem with rc compilation:

1. You need to close the rc file, open the rc file in the resource manager, and save it as a file in unicode format.

2. Specifically, it should be processed according to the prompt information behind the error.


3. Since there is a class that inherits CWinApp under WinForm1, it can be deleted directly. Change the included header files of XXXXXDlg.h and XXXXXXDlg.cpp to inherit CWinApp under the current project.

why?

Because CWinApp implies WinMain() entry

4. Add code under WinDLL.Cpp

extern "C" __declspec(dllexport) void ShowDlg(void)  
{  
    AFX_MANAGE_STATE(AfxGetStaticModuleState()); //In order to ensure the code is accurate to prevent the call from failing
    CXXXXXDlg dlg;  
    dlg.DoModal();  
}

//Don't understand extern "C" __declspec(dllexport) You can download it from Baidu, here is a brief explanation

--declspec(dllexport) is to export the data

--declspec(dllimport) is the import data

There is also the export of the added function under the .def file. You can see the introduction of MSDN and the explanations of the great gods.

LIBRARY "XXX"; dll generation name
EXPROTS; export function name
myFunction1 @1; label needs to see the data by yourself

myFunction2 @2

PS: If you encounter CDialogEx is an undefined base class, you can change it to CDialog, because CDialogEx.h could not be found.

5. Add a button event in the test project (DlgTest), add some code, remember to copy the dll to the project directory

HINSTANCE hDll; //DLL handle  
hDll = LoadLibrary(_T("XXXXDLL.dll")); //Load dll
if (NULL == hDll)  
{  
    MessageBox(_T("DLL load failed"));  
}  
else  
{  
    FARPROC     proc;    
    proc = GetProcAddress(hDll,"ShowDlg");//Call the dll function
    proc();  
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325870175&siteId=291194637