DLL header file format and application

Source: Internet
Author: User
Tags header

1, the origin of the DLL

Dynamic link library (DLL) is developed from the concept of C language function library and Pascal Library unit. All of the C language standard library functions are stored in a library of functions. In the process of linking the application, the linker copies the function code called by the program from the library file and adds the function code to the executable file. This approach is more useful for code reuse than storing functions in a compiled obj file.

But with the advent of a multitasking environment such as windows, the method of functional libraries is cumbersome. If every program has its own function to complete screen output, message processing, memory management, dialog boxes, and so on, then Windows programs will become very large. The development of Windows requires allowing several simultaneous programs to share a single copy of a set of functions. This is where the dynamic-link library occurs. Dynamic link libraries do not have to be compiled or linked, and once loaded into memory, DLL functions can be used by any running application software in the system without having to load another copy of the DLL function into memory.

2. Declaration of functions in DLL

According to the writing and calling specification of Microsoft DLLs, in DLLs, when declaring and defining an exported function, you need to use the __declspec (dllexport) keyword before the function to indicate that the function is the exported function of the DLL; in the implicit invocation of the DLL, when the application calls the exported function, Imported functions must be declared first using the __declspec (dllimport) keyword. The Declaration method for this import and export function also conforms to the call specification for the first declaration and invocation of the C + + function.

3. DLL export function link category and reference method

Export function in the compilation, link process, you can use C link and C + + link two ways, when the C link, the compiler does not change the name of the exported function, in contrast, when using C + + links, the compiler changes the name of the exported function.

The export function can be written in C or in the C + + language. For execution files written in C, if you invoke the Export function written in C + + language, you should force the designation to use C link instead of C + + chain to deliver the exported function library, and for the execution file written in C + + language, if you call the Export function written in You should force the designation to deliver the exported function library using the C chain. Depending on the compiler specification, specifying and declaring a function uses the C link, you should use the keyword extern "C" before the function declaration.

In general, in order to ensure that executable modules written in different languages can correctly access the exported function, extern "C" is customarily used to specify the export function in C link mode.

4. dll header file format

In actual programming, the declaration of the exported function is usually unified in a header file, and its definition is distributed in different CPP files according to the need, such realization way is more convenient to the management and maintenance of the file and its function. Therefore, the DLL header file is formatted as follows:

#ifndef _DLLMODULENAME_H
#define _DLLMODULENAME_H
......
/*
*   if using C++ Compiler to compile the file, adopting C linkage mode
*/
#ifdef __cplusplus
extern "C" {
#endif
// macro define __declspec(dllexport)
#define DLLMODULENAME_LIB_API __declspec(dllexport)
// define export functions
DLLMODULENAME_LIB_API returntype FuncName (parameters);
// ... more declarations as needs
#undef DLLMODULENAME_LIB_API
#ifdef __cplusplus
}
#endif
#endif

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.