VC + + 6.0 How to create and invoke a dynamic link library

Source: Internet
Author: User
Tags requires win32 root directory

1. Static link library and dynamic link library difference:

Static link library: The directives in Lib are directly contained in the resulting EXE file.

dynamic-link libraries: DLLs do not have to be included in the final EXE, which can dynamically reference and unload DLL files when they are executed.

Also, static link libraries cannot contain other dynamic or static libraries, and dynamic link libraries can contain other dynamic or static libraries.

DLL supported by 2.vc++:

DLL's programming language and compiler independent of the specific, dynamic link library everywhere, VC + + support three kinds of DLLs: Non-MFC dynamic Library, MFC rule DLL and MFC extension DLL. DLL export functions (or variables, classes) are available for application invocation, DLL intrinsic functions can only be used within DLL programs, and applications cannot invoke them.

3. How to declare a function:

One is to add "_declspec (dllexport)" Between the function declaration type and the name of the letter.

Another type of module definition (. def) file declaration requires that the module file be added to the library project in the following format:

Library project Name

Exports the name of the exported function

How to call 4.DLL:

A static call that completes the loading of the DLL and the uninstallation of the DLL at the end of the application by the compilation system.

Another dynamic invocation, in which programmers load and unload DLLs using API functions (DLL loading-dll function addresses to obtain-dll release).

5. All library works must be release mode when compiling:

Build-set Active configuration-Selection Library project release mode

6. Example:

function----Create a dynamic link library (MFC rule DLL)

1. New--projects--mfc AppWizard (DLL)--regular DLL using shared MFC DLL//named Mfc_dll

2. def file add: Function name (add_new)

3. h file Add: External function declaration//SUM function, function name Add_new

extern "C" __declspec (dllexport) int __stdcall add_new (int a,int b);

4. cpp file add: 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. In the root directory in the Release folder Dll,lib and root directory H file is required

function----Call dynamic link library (copy Mfc_dll.dll and mfc_dll.lib to the project directory)

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

1. New--projects--win32 Console Application--an Empty Project

2. Add h file: (test.h)

#pragma comment (lib, "Mfc_dll.lib")//tell the compiler DLL the path and file name where the lib file corresponds

extern "C" _declspec (dllimport) int _stdcall add_new (int a,int b);//Declaration Import function

3. Add CPP File: (main.cpp)

#include "test.h"

int main ()

{

cout<

return 0;

}

Dynamic invocation

#include

#include

typedef INT (* lpaddfun) (int, int);//define a function pointer type that takes the same parameter type and return value as the Add_new function

int main ()

{

HINSTANCE hdll;//Handle

Lpaddfun addfun;//function pointer

Hdll=loadlibrary ("DllTest.dll")//dynamically loading DLL module handle

if (hdll)

{

Addfun= (Lpaddfun) GetProcAddress (hDLL, "add_new")//Get the address of the function in the loaded DLL module

if (Addfun)

{

int Result=addfun (2,3);

printf ("%d", result); } freelibrary (hDLL);/free DLL module that has already been loaded

}

return 0;

}

Third, variable----create a dynamic link library (non-MFC DLLs)

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)

When Windows loads a DLL, it requires a portal function, just as a console or DOS program requires a main function, and a Win32 program requires a WinMain function. So introduce a functional version of the default DllMain that does nothing. 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.