DLL preliminary (1)

Source: Internet
Author: User

This is the note after reading a video.

Implicit Link

Create a dynamic link library project, start vc6.0, select Win32 dynamic-Link Library, enter the project name and project path, and select an empty DLL project on the second page, click Finish. After the project is created, add the C ++ source file.

Compile a function in the file to complete addition and a function to complete subtraction.

View code

1 int add(int a,int b)
2 {
3 return a+b;
4 }
5 int sub(int a,int b)
6 {
7 return a-b;
8 }

Compile the program. dlll1.dll is displayed in the debug folder under the project folder.

At this time, the file is not exported. You can use the dumpbin command to view the export function of a DLL file. The specific method is as follows:

1. Open the command prompt and copy the directory path of the debug folder under the corresponding file. Transfer to the corresponding directory.

2. Enter dumpbin (dumpbin is a tool under the vc6.0bin directory)-Exports dlll1.dll. The output file information.

No output function is displayed.

Modify the function code:

View code

1 _declspec(dllexport) int add(int a,int b)
2 {
3 return a+b;
4 }
5 _declspec(dllexport) int sub(int a,int b)
6 {
7 return a-b;
8 }

At this point, re-compile and use dumpbin-Exports dlll1.dll to view the output function name. (Note that at this time the function name is written, which is changed by vs according to its own rules. Different compilers may be different)

Test the DlLL. dll file.

Create an MFC wizard project and select the simple dialog box.

Compile the message processing functions for two buttons:

View code

 1 extern int add(int a,int b);
2 extern int sub(int a,int b);
3 void CDLLLtestDlg::OnBtnAdd()
4 {
5 // TODO: Add your control notification handler code here
6 CString str;
7 str.Format("a+b=%d",add(5,3));
8 MessageBox(str);
9 }
10
11 void CDLLLtestDlg::OnBtnSub()
12 {
13 // TODO: Add your control notification handler code here
14 CString str;
15 str.Format("a-b=%d",sub(5,3));
16 MessageBox(str);
17 }

Run the program and find that the program does not run correctly at this time.

This is because the project cannot find the location of the dlll1.dll file. Copy the dlll1.dll and dlll1.lib files in the dlll1 folder to the dllltest folder. Enter the project/settings as follows:

Run the program again to run it correctly.

You can view the input information of the executable program, and run the command prompt in the dllltest/DEBUG directory of the project using dumpbin.

Run the command: dumpbin-imports dllltest.exe to compile all the input functions required by the project. You can see the functions provided by DlLL. dll.

In addition, you can use the graphic tools provided by Vs to view the Dynamic Link Libraries required by the project. In vc6.0. In addition, it should be noted that only when the EXE and DLL files are put together can depends recognize them.

The function called by the program is listed above the window on the right, and the function exported by the DLL is listed below.

Generally, extern is not used in the project that calls the DLL, but is written as follows:

View code

 1 _declspec(dllimport) int add(int a,int b);
2 _declspec(dllimport) int sub(int a,int b);
3 void CDLLLtestDlg::OnBtnAdd()
4 {
5 // TODO: Add your control notification handler code here
6 CString str;
7 str.Format("a+b=%d",add(5,3));
8 MessageBox(str);
9 }
10
11 void CDLLLtestDlg::OnBtnSub()
12 {
13 // TODO: Add your control notification handler code here
14 CString str;
15 str.Format("a-b=%d",sub(5,3));
16 MessageBox(str);
17 }

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.