A simple example of calling a dynamic library

Source: Internet
Author: User

Create a dynamic library dll project first
Add dlltest. cpp dlltest. def dlltest. h to the Project
Dlltest. h
[Cpp]
// Dlltest. h
Extern _ declspec (dllexport) int FuncTest ();
Dlltest. cpp
[Cpp]
// Dlltest. cpp
_ Declspec (dllexport) int FuncTest (int)
{
If (a = 1)
{
Return 100;
}
}
Dlltest. def
[Cpp]
LIBRARY "testmydll"
EXPORTS
FuncTest
 
 
Generate dlltest. dll after compilation
Create a Win32 console project to call dlltest. dll.
Copy dlltest. dll to the Debug directory of Win32.
The dll. cpp file in Win32 project is as follows:
[Cpp]
# Include <iostream>
# Include "string"
# Include <stdio. h>
# Include <windows. h>
Using namespace std;
 
Int main ()
{
Typedef int (* HFUNC) (int );
HINSTANCE hDLL = LoadLibrary ("testmydll. dll ");
If (hDLL)
{
HFUNC hFun = (HFUNC) GetProcAddress (hDLL, "FuncTest ");
If (hFun)
{
Int a = 1; www.2cto.com
Int B = hFun ();
Printf ("% d \ n", B );
}
}
 
}

 
During compilation and execution, dlltest. dll is called to print the 100

Related Article

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.