Technical Points of DLL Export Dialog Box

Source: Internet
Author: User

I have written it before, but I forgot it. This time I wasted a lot of time. Now I want to write it down to make my impression better.

1. Create a DLL Project, Select the type "regular DLL using shared mfc dll ";

2. Import existing resources of the MFC project
Method 1: copy the corresponding control definition in resource. h, and then copy the dialog box resource in the. RC file to the. RC file of the current DLL project;
Method 2: Use Project-> Add to project-> files to select the. RC file of the corresponding project, and then use Ctrl + C/V to copy the required control.
3. Copy the implementation code and required code of the existing project to the current DLL.
Warning do not copy header files and implementation files with app classes. Modify the file referenced in other files to the current xxxdll. h
Otherwise, the DLL is successfully generated, but the call fails because initinstance in the app class is the program entry, and the DLL project itself has the program entry, resulting in a conflict.
Note: When multiple dialog box-based MFC programs export through a DLL, an error may occur due to the Control ID conflict. To avoid this problem, when writing a dialog box program, you must assign a meaningful ID to each control to avoid conflicts and unavailability.
4. Compile the export function in xxxdll. cpp.
Shape:
Extern "C" _ declspec (dllexport) char * corgdlg_rtnstring ()
{
Afx_manage_state (afxgetstaticmodulestate ());
Ccorgdlgdlg DLG;
DLG. domodal ();
Char P [1024];
Strcpy (p, DLG. infostr. getbuffer (0 ));
Return P;
}
5. Use of DLL files
After the file is generated, you can use a tool to check whether a correct export function is generated.
Example:
Hinstance hmod;
Hmod =: loadlibrary ("orgpidplsrdlgdll. dll"); // cplsydlgdll. dll cpserialdll cpwmdlgdll. dll cplsr2dlgdll. dll
If (hmod = NULL)
{
Afxmessagebox ("fail ");
Return;
}
Cstring strtest;
Char STR [1024];
Memset (STR, 0, 1024 );
/***************** Corgdlg *********************/
# If 1
// Do not upload the Initialization Configuration. Open the dialog box and set it and return
Typedef const char * (* corgdlg) (); // defines the function pointer type
Corgdlg rtnstring = (corgdlg) getprocaddress (hmod, "corgdlg_rtnstring"); // obtain the void address of the function.
Strcpy (STR, rtnstring ());
Strtest. Format ("% s", STR );
Afxmessagebox (STR );


// Pass the Initialization Configuration. In the dialog box that appears based on the configuration, the user configures the configuration before returning it.
Cstring inistr = "Initial string ";
Typedef const char * (* inicorgdlg) (cstring inistr); // define the function pointer type
Inicorgdlg iniadnrtn = (inicorgdlg) getprocaddress (hmod, "corgdlg_initialandrtn"); // obtain the void address of the function.
Strcpy (STR, iniadnrtn (inistr ));
Strtest. Format ("% s", STR );
Afxmessagebox (STR );
# Endif
Freelibrary (hmod );

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.