C + + dynamic link library

Source: Internet
Author: User

1. Dynamic Link Library Overview:

    • dynamic-link libraries do not usually run directly or accept messages; it only works when other modules invoke functions in the dynamic-link library.
    • All functions in the Windows API are included in the dynamic link library.
    • Dynamic link library is divided into static library and dynamic library.

2, the creation of dynamic link library:

The function to be exported is in the form:

extern int Add (intint  b) {      return a +b;   }

To solve the name adaptation problem, you can add a module definition file for the project. def

LIBRARY          MFC-addexports          add    

The classes to be exported are in the form of:

class _declspec (dllexport) point{public:      void OutPut (intint  y);};

Generated after compilation: a DLL (. dll) file, an ingest library (. lib) file, and so on

Note: If you specify an export flag when declaring a class, all functions in the class are exported, otherwise only the class member functions that have the export flags specified when they are declared are exported;

3, dynamic link library loading:

(1) Implicit loading

Use extern or _declspec (dllimport) to declare an external function (preferably write this declaration in a header file *.h)

int Add (intint b);

Import the corresponding ingest library (. lib) file: Link the Lib library in the project settings or use the Code link Lib library in the source file:

#pragma comment (lib, "*.lib")

Add the directory where the dynamic link library file is located to the environment variable in the system: path

(2) Explicit loading

Maps the specified executable module to the address space of the calling process

#include <Windows.h>= LoadLibrary (_t (". \\.. \\MFC-Add\\Debug\\MFC-Add.dll"));

Gets the address of the exported function in the dynamic link library

typedef int (_stdcall *addproc) (int a,int b); // declares that message is a pointer type to a function, the function returns a value of type int; _stdcall is the standard calling convention Addproc Addproc; Addproc  = (addproc) GetProcAddress (handlerdll,_t ("add"));

Calling the Export function

Addproc (5,6);

Release a reference to a DLL when you do not need to access the DLL

FreeLibrary (".. \\.. \\MFC-Add\\Debug\\MFC-add. dll");

Note:

    • Dumpbin and Depends Tools
    • calling convention: _stdcall: The standard calling convention is the WINAPI calling convention, which is the Pascal calling convention, the non-C calling convention
    • Name adaptation: the C + + compiler will name the exported function when it builds the DLL, and the different compilers use different adaptation rules

C + + dynamic link library

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.