How to create and call a dynamic link library in VC ++ 6.0

Source: Internet
Author: User

1. Static and Dynamic Link Libraries:

Static Link Library: commands in lib are directly included in the final generated EXE file.

Dynamic Link Library: dll does not need to be included in the final EXE. During execution of the EXE file, you can dynamically reference and uninstall the DLL file.

At the same time, the static Link Library cannot contain other dynamic or static libraries, while the dynamic link library can contain other dynamic or static libraries.

2. DLL supported by VC ++:

The DLL compilation has nothing to do with the specific programming language and compiler, and the dynamic link library can be seen everywhere. VC ++ supports three types of DLL: Non-MFC dynamic library, MFC rule DLL, and MFC extension DLL. DLL export functions (or variables, classes) can be called by programs. DLL internal functions can only be used in DLL programs, and applications cannot call them.

3. How to export the function declaration:

Add "_ declspec (dllexport)" between the function declaration type and function name )".

In addition, the module definition (. def) file Declaration must be used to add a module file to the library project. The format is as follows:

LIBRARY Project Name

EXPORTS function name

4. DLL call method:

A static call. The compilation system loads the DLL and uninstalls the DLL when the application ends.

Another dynamic call method is provided by the programmer to load and detach the DLL using API functions (obtain the DLL load-DLL function address and release the DLL.

5. All library projects must be Release:

Build-set active configuration-select the release mode of the Library Project

6. Example:

1. Function ---- create a dynamic link library (MFC rule DLL)

1. New -- projects -- MFC AppWizard (dll) -- Regular DLL using shared mfc dll // obtain the name MFC_dll

2. Add the function name (Add_new) to the def file)

3. Add the external function declaration // summation function to the H file. The function name is Add_new.

Extern "C" _ declspec (dllexport) int _ stdcall Add_new (int a, int B );

4. Add in the cpp file: external function implementation

Extern "C" _ declspec (dllexport) int _ stdcall Add_new (int a, int B)

{

Return a + B;

}

5. build -- set active configuration -- win32 release -- OK

6. Generate

7. dll in the release folder under the root directory. lib and H files under the root directory are required

Ii. Function ---- call the dynamic link library (copy MFC_dll.dll and MFC_dll.lib to the project directory)

// Static call (. h can be written to the. cpp file)

1. new -- projects -- win32 console application -- an empty project

2. Add an h file: (test. h)

# Pragma comment (lib, "MFC_dll.lib") // inform the compiler of the path and file name of the lib file corresponding to the DLL.

Extern "C" _ declspec (dllimport) int _ stdcall Add_new (int a, int B); // declare the import function

3. Add the cpp file: (main. cpp)

# Include "test. h"

Int main ()

{

Cout <

Return 0;

}

// Dynamic call

# Include

# Include

Typedef int (* lpAddFun) (int, int); // defines a function pointer type that is the same as that of the Add_new function.

Int main ()

{

HINSTANCE hDll; // handle

LpAddFun addFun; // function pointer

HDll = LoadLibrary ("dllTest. dll"); // dynamically load the DLL module handle

If (hDll)

{

AddFun = (lpAddFun) GetProcAddress (hDll, "Add_new"); // obtain the address of the function in the loaded DLL module

If (addFun)

{

Int result = addFun (2, 3 );

Printf ("% d", result);} FreeLibrary (hDll); // release the loaded DLL module

}

Return 0;

}

3. Variable ---- create a dynamic link library (non-mfc dll)

1. new --- projects --- win32 dynamic-link library ---- an empty project (Sample)

2. Add sample. h

# Ifndef SAMPLE_H

# Define SAMPLE_H

Extern int dllGlobalVar;

# Endif

3. Add sample. cpp

# Include "sample. h"

# Include

Int dllGlobalVar;

Bool APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)

// Windows requires an entry function when loading DLL, just as the console or DOS program requires the main function and win32 program requires the winmain function. Therefore, introduce a default DllMain function version that does not perform any operation. Is the internal function of the DLL.

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.