Introduction to DLL

Source: Internet
Author: User

DLL (Dynamic Link Library), a dynamic link library.

1. About _ declspec (dllimport) and _ declspec (dllexport)

When writing a DLLProgramCall. Add the _ declspec (dllimport) keyword to the function declaration.

Example:

A variable is introduced:
_ Declspec (dllexport) int iex;

Introduce a function:
_ Declspec (dllexport) int fnex (int I );

Introduces a class:
Class _ declspec (dllexport) cclassex
{......};

When an external program references a DLL, if you use the implicit call method (discussed later), you need to include the header file related to the DLL file. This header file uses _ declspec (dllimport) keyword indicates the DLL export content (in fact, replace all the original _ declspec (dllexport) with _ declspec (dllimport )).

Example:

Introduce a variable:
_ Declspec (dllimport) int iex;

Introduce a function:
_ Declspec (dllimport) int fnex (int I );

Introduce a class:
Class _ declspec (dllimport) cclassex
{......};

To improve program readability, you can define the following macros:
# Define dllimport _ declspec (dllimport)
# Define dllexport _ declspec (dllexport)
For ease of use.

Generally, the macro is defined as follows:
# IfdefMydll_exports

# Define Mydll_api_ Declspec ( Dllexport )

# Else

# Define Mydll_api_ Declspec ( Dllimport )

# Endif

In this way, first define in the implementation file of the source program Mydll_exports And then include this header file. In an external program, this header file can be directly included. This method can be used for multiple purposes.

  2 . Implicit call and explicit call

Implicit call is the most common method. It requires the original DLL File, the original header file, and the library file. This header file must be included in the program and compiled together with the input file. When the program starts, all the implicitly called DLL Load the file to the memory.

If You Want To DLL you can use explicit calling to add files to the memory. Explicit call only requires the original DLL file, however, you must know DLL which exported symbols are available. vs in command line mode, enter" depends " command to view DLL information.

  Example:

  Implicit call:

# Include ".. \ Dlltest. H"

......

Void Calldll (Void )

{

Wchar_t Wszbuf [32];

Wsprintf (wszbuf, text ("% D" ), Fndlltest (9 )); // Fndlltest YesDLLFile Export Method

Wprintf (wszbuf );

}  

Explicit call:

Void Calldll (Void )

{

Typedef Int (* Addproc )(Int I );

Addproc FN;

 

Hinstance hinst = loadlibrary (L "Dlltest. dll" );

If (Null = hinst)

{

Wprintf (L "Failed on Load DLL ." );

Return ;

}

Fn = (addproc) getprocaddress (hinst,"Fndlltest" );

// If you use_ Stdcall,We should also use(_ Stdcall addproc)

If (! FN)

Wprintf (L "Failed on get process address ." );

Else

{

Int I;

I = FN (9 );

Wchar_t Wszbuf [32];

Wsprintf (wszbuf, l "% D" , I );

Wprintf (wszbuf );

}

Freelibrary (hinst ); // ReleaseDLL

/*

 * Addrproc add = (addproc) getprocaddress (hinst, "add ");

 *The serial number can also be used for the second parameter.:

 * Makeintresource (1) Replace function name.

 */

}

3 . Notes

If you use C ++ During program compilation, the variable name and other symbols are replaced with ? Fndlltest @ qaeaav 0 " OfClassOfForm,IsDisplayTypeCallTapeComeNowNoConvenient. ForThisItemsProblem,MeAreYesIsYesExportOfSymbolAddUpper" Extern "C" "Modifier to keep the original name. However, the exported class name is still the renamed name, which should be further studied.

If you use MFC to generate mfc dll, you only need to define the following class afx_ext_class yourclass.

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.