Compiling and using DLL

Source: Internet
Author: User

Discovering that you are facing two major difficulties in learning: laziness and forgetting. Laziness depends on self-discipline, schedules, rewards mechanisms (ice cream, delicious meals, soda, etc.), and forgetting is based on notes, knowledge structure charts, and mind maps, note how to correct small bugs in programming.

Memo:

1-1: def method creation: stdafx. h cannot be found in vc6. Therefore, an empty project is created. The functions in stdafx are too high-end and do not need to be affected. After the DLL project is created, create a CPP file named dlltest. CPP, directly copy a dllmain file, with a warning in the field # include <windows. h> directly write the function to be defined in dllmain. For example, int add (int A, int B) {return (a + B );}. Go to the project directory and create a new notepad document named dlltest. Def. Open and enter the content. Library "dlltest", exports [email protected] (write all the data to be exported, and the sequence numbers are 1, 2, 3, 4 ......).. Save. This step is very important. After saving, click Add file in the sours file bar of the project and browse to find dlltest. Def. Next, compile and link. Do not write and link one side. It is too troublesome to change the DLL. The DLL is generated accidentally. If you don't want to, you can delete all the files generated during the compilation and link process, and re-compile and link them.
1-2: Use of the def method after creation: Add a new project to workstation, Win32 console app (forget how to write, search for keywords by yourself, all references are available, all operations I write are hieroglyphics ). Set to active mode, right-click Settings. Now, create a call. CPP. input # pragma comment (Lib, "dllset. lib "), _ declspec (dllimport) int add (int A, int B) Put the dllset. lib and dlltest. the DLL copy depends on the project directory and can be used like a normal function call. Int sum = add (10, 10); printf (10 + 10 = 20), yeah !!!!!!! This is called dynamic usage. The program is used automatically when it runs, and the space is released by the program itself when it runs out.

2-1: Create in export mode: Old rule: stdafx. h cannot be found in vc6, so create an empty project. The functions in stdafx are too high-end and do not affect it. After the DLL project is created, create a CPP file named dlltest. CPP, directly copy a dllmain file and copy it in. If there is a warning, the field # include windows. h. Write the function to be defined directly under dllmain. For example, int add (int A, int B) {return (a + B );}. Add a codll. h: # pragma once (Lib, "path name + file name (displayed with double backslash)") extern "C" _ declspec (dllexport) int add (int, int B); add is sold, that is, the statement can be called.

2-2: Use workstation to add a new project, Win32 console app (same as above ). Set to active mode, right-click Settings. Now, create a call. CPP. input # progma comment (Lib, "dllset. lib "), extern" C "_ declspec (dllimport) to set the dllset. lib and dlltest. DLL copy depends on the project directory, and add is bought. The usage is the same as above.

The following is an overview of the explicit call process. Create a function pointer p that can point to add. Obtain the return handle using loadlibrary (dll path + file name. Use startadress, an object of P instance, to obtain the start address using getprocadress (handle, function name). Int I = the start address is to call add, and finally release the resource free (handle ). \

 

Additional instructions:

1. implicit link-static call

The implicit link loads the DLL file into the application when the program starts execution. It is easy to implement implicit links. You only need to write the name of the imported function keyword _ declspec (dllimport) to the corresponding header file of the application. The following example uses an implicit link to call the min function in the mydll. dll library. First, generate a project named testdll and enter the following code in the dlltest. h and dlltest. cpp files:
// Dlltest. h
# Pragma comment (Lib, "mydll. lib ")
Extern "C" _ declspec (dllimport) int max (int A, int B );
Extern "C" _ declspec (dllimport) int min (int A, int B );

// Testdll. cpp
# Include "dlltest. H"
Void main ()
{
Int;
A = min (8, 10) printf ("compare result: % d \ n", );
}
Before creating the dlltest.exe file, copy mydll. dll and mydll. lib to the directory where the current project is located, or copy it to the Windows System directory.IfDLLYesDefFile to be deletedTestdll. hKeyword in the fileExtern "C". Testdll. the keyword progam commit in the hfile is linked to mydll when the compiler of Visual C + is linked. lib file, of course, developers can also not use # pragma comment (Lib, "mydll. lib "), and enter mydll directly in the object/moduls column of the Setting-> link page of the project. lib is supported. (Note: DLL files created by def can be supplied

2. explicit link-dynamic call

Explicit links allow applications to load DLL files or uninstall DLL files at any time during execution, which is not implemented by implicit links. Therefore, explicit links provide better flexibility, it is more suitable for explanatory languages. However, implementing explicit links requires a little effort. In the application, call the dynamic link library provided by loadlibrary or the afxloadlibrary provided by MFC explicitly. The file name of the Dynamic Link Library is the parameter of the above two functions, and then use getprocaddress () obtain the function to be introduced. Since then, you can call the introduced function like using a function defined in the application. Before exiting the application, use the afxfreelibrary provided by freelibrary or MFC to release the dynamic link library. The following is an example of calling the max function in DLL through an explicit link.

Void main (void)
{
Typedef int (* Pmax) (int A, int B );
Typedef int (* pmin) (int A, int B );
Hinstance hdll;
Pmax maxhdll = loadlibrary ("mydll. dll"); // load the dynamic link library mydll. dll file;
Max = (Pmax) getprocaddress (hdll, "Max ");
A = max (5, 8 );
Printf ("compare result: % d \ n", );
Freelibrary (hdll); // uninstall the mydll. dll file;
}
In the above example, use the type definition keyword typedef to define the pointer to the same function prototype as the DLL, and then use loadlibray () load the DLL to the current application and return the handle of the current DLL file. Then, use the getprocaddress () function to obtain the function pointer imported to the application. After the function is called, use freelibrary () to uninstall the DLL file. Before compiling a program, copy the DLL file to the project directory or the Windows System directory.

Compiling and using DLL

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.