DLL static call and dynamic call

Source: Internet
Author: User
There are two methods to call a DLL: static call and dynamic call.

(1) The static call procedure is as follows:

1. Copy your youapp. DLL to the DEBUG directory of your target project (the project of youapp. DLL needs to be called;

2. Copy your youapp. lib to the directory of your target project (the project of youapp. DLL needs to be called;

3. Copy your youapp. H (including the definition of the output function) to your target project (you need to call the project of youapp. dll ).

Directory;

4. Open the selected project of your target project and select the settings menu of the Project Main Menu of Visual C ++;

5. After step 4 is executed, VC will pop up a dialog box, and select the link page in the Multi-page display control of the dialog box. However

Enter youapp. Lib in the object/library modules input box.

6. Select your target project head files and add: youapp. h file;

7. At last, include your: # include "youapp. H" in your target project (*. cpp, you need to call the function in DLL"

Note: youapp is the project name of your DLL.

2. Call it dynamicallyProgramAs follows:

For dynamic calls, you only need to perform static call step 1.

{

Hinstance hdllinst = loadlibrary ("youapp. dll ");

If (hdllinst)

{

Typedef DWORD (winapi * myfunc) (DWORD, DWORD );

Myfunc youfuntionnamealias = NULL; // youfuntionnamealias function alias

Youfuntionnamealias = (myfunc) getprocaddress

(Hdllinst, "youfuntionname ");

// The Name Of The function declared by youfuntionname in DLL

If (youfuntionnamealias)

{

Youfuntionnamealias (param1, param2 );

}

Freelibrary (hdllinst );

}

}

Explicit (static) call:

Lib + dll +. H. Note that the dllexport in. H is changed to dllimport.

Implicit (dynamic) call:

Dll + function prototype declaration, loadlibrary first, and then getprocaddress (that is, find the address of the function in DLL), without freelibrary

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.