C ++ Dynamic Link Library

Source: Internet
Author: User

Dynamic Link Library

I

1. dll

. H file

# Ifdef dll.pdf API
# Else
# Define Dllsort api _ declspec (dllimport)
# Endif
Dll.pdf API Int Add ( Int A, Int B );
Dll.pdf API Int Sub ( Int A, Int B );
Class Dllw.api point
{
Public :
Void Output ( Int X, Int Y );
};

 

. Cpp File

# Define Dllsort api _ declspec (dllimport)
# Include " Dll1.h "
# Include <windows. h>
# Include <stdio. h>
Int Add ( Int A, Int B)
{
Return A + B;
}
Int Sub ( Int A, Int B)
{
Return A-B;
}
Void Point: output ( Int X, Int Y)
{
Hwnd;
Hwnd = getforegroundwindow ();
HDC = getdc (hwnd );
Char Buf [ 20 ];
Memset (BUF, 0 , 20 );
Sprintf (BUF, " X = % d, y = % d " , X, y );
Textout (HDC, 0 , 0 , Buf, strlen (BUF ));
Releasedc (hwnd, HDC );

}

Call DLL to put the compiled. dll and. Lib files inProgramDirectory to copy the. h file of the DLL to the directory of the referenced program. Project-> setting-> link: Set the Lib File Location of the Object/library modules. For example, "Debug/dll3.lib ".

Add Referenced File # include "dll1.h"

Void Cdlltestdlg: onbtnadd ()
{
// Todo: add your control notification handler code here
Cstring STR;
Str. Format ( " 5 + 3 = % d " , Add ( 5 , 3 ));
MessageBox (STR );
}

void cdlltestdlg: onbtnsub ()
{< br> // todo: add your control notification handler code here
cstring STR;
Str. format ( " 5-3 = % d " , sub ( 5 , 3 );
MessageBox (STR );
}

VoidCdlltestdlg: onbtnoutput ()
{
//Todo: add your control notification handler code here
Point pt;
PT. Output (5,3);
}

II. The def method solves the name adaptation problem and uses different languages.

Create a new file with the extension name def in the program directory and add it to the project in project-> Add to project \ files.

DLL

. Def

Library dll3
Exports
Add

. Cpp

 

Int _ stdcall add (int A, int B)
{
Return A + B;
}

Call

. H

 

_ Declspec (dllimport) Int _ Stdcall add ( Int A, Int B );

. Cpp

Void Cdll3testdlg: onbtnadd ()
{
Cstring STR;
Str. Format ( " 5 + 3 = % d " , Add ( 5 , 3 ));
MessageBox (STR );
}

 

All of the preceding call methods are implicitly linked to achieve dynamic link library access.

Dynamic Loading Mode

Void Cdll3testdlg: onbtnadd ()
{
Hinstance hinst; // DLL handle
Hinst = loadlibrary ( " Dll3.dll " ); // Dynamic DLL Loading
Typedef Int (_ Stdcall * lpadd )( Int , Int ); // Define function pointer type
Lpadd addfun = (lpadd) getprocaddress (hinst, " Add " );
If (! Addfun)
{
MessageBox ( " An error occurred while obtaining the function address! " );
Return ;
}
Cstring STR;
Str. Format ( " 5 + 3 = % d " , Addfun ( 5 , 3 ));
Freelibrary (hinst ); // No need to access DLL, release
MessageBox (STR );
}

 

Summary. Def can solve cross-language problems, which is favored by individuals. Please leave a message. You are learning C ++.

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.