DLL Export method (two kinds)

Source: Internet
Author: User
Tags class definition naming convention
Exporting from a DLL using a DEF file

A module definition (. def) file is a text file that contains one or more module statements that describe various attributes of a DLL. If you do not use the __declspec (dllexport) keyword to export a DLL's functions, the DLL requires a. def file.

The. def file must contain at least the following module definition statements:

The first statement in the file must be a LIBRARY statement. This statement identifies the. def file as belonging to a DLL. The LIBRARY statement is followed by the name of the DLL. The linker places this name in the DLL's import library.

The EXPORTS statement lists the name and, if possible, the ordinal value of the DLL export function. Assign an ordinal value to a function by adding the @ character and a number after the function name. When an ordinal value is specified, the range of ordinal values must be from 1 to N, where n is the number of DLL export functions. If you want to export functions by ordinal, see exporting functions from a DLL by ordinal rather than by name, and this topic.

For example, a DLL that contains code that implements a binary search tree might look like this:

LIBRARY   btree
exports
   Insert   @1
   Delete   @2
   member   @3
   Min   @4

If you use the MFC DLL Wizard to create an MFC DLL, the wizard creates the skeleton. def file for you and automatically adds it to your project. Add the name of the function to export to this file. For non-MFC DLLs, you must create the. def file yourself and add it to your project.

If you export a function in a C + + file, you must place the decorated name in a. def file, or you can define an exported function with standard C links by using external "C". If you need to place the decorated name in a. def file, you can get the decorated name by using the DUMPBIN tool or the/MAP linker option. Note that the compiler-generated decorated name is compiler-specific. If you place the decorated name produced by the Visual C + + compiler in a. def file, the application that is linked to the DLL must also be generated in the same version of Visual C + +, so that the decorated name in the calling application can match the exported name in the DLL's. def file.

If you build an extension DLL and export using a. def file, place the following code at the beginning and end of the header file that contains the exported class:

#undef afx_data
#define AFX_DATA afx_ext_data
/<body of your header file>
#undef afx_data
# Define Afx_data

These lines of code ensure that the MFC variables used internally or the variables added to the class are exported (or imported) from the extension DLL. For example, when you use declare_dynamic derived classes, the macro expands to add CRuntimeClass member variables to the class. Omitting these four lines of code may result in the failure to compile or link the DLL correctly, or cause an error when the client application links to the DLL.

When you build a DLL, the linker uses a. def file to create the export (. exp) file and the import library (. lib) file. The linker then uses the export file to generate the DLL file. An executable file that is implicitly linked to a DLL is linked to the import library at build time.

Note that MFC itself uses a. def file to export functions and classes from MFCx0.dll.

URL: https://msdn.microsoft.com/zh-cn/d91k01sh (v=vs.90)

Using __declspec (dllexport) to export from a DLL

Microsoft introduced __export in the 16-bit compiler version of Visual C + +, allowing the compiler to automatically generate export names and place them in a. lib file. The. lib file can then be used to link to the DLL like static. Lib.

In an updated compiler version, you can use the __declspec (dllexport) keyword to export data, functions, classes, or class member functions from a DLL. __declspec (dllexport) adds the export directives to the object file, so you do not need to use a. def file.

This convenience is most obvious when trying to export a C + + decorated function name. Because there is no standard specification for name adornments, the name of the exported function may change in different compiler versions. If you use __declspec (dllexport), you must recompile DLLs and dependent. exe files only if you resolve any naming convention changes.

Many export directives, such as ordinal, NONAME, and PRIVATE, can only be created in a. def file and must be specified using a. def file. However, using __declspec (dllexport) on the basis of a. def file does not cause a build error.

To export a function, the __declspec (dllexport) keyword must appear to the left of the calling convention keyword, if a keyword is specified. For example:

__declspec (dllexport) void __cdecl Function1 (void);

To export all public data members and member functions in a class, the keyword must appear to the left of the class name, as follows:

Class __declspec (dllexport) cexampleexport:public CObject
{. class definition ...};
Description

__declspec (dllexport) cannot be applied to functions that have a __clrcall calling convention.

When you build a DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting, and add __declspec (dllexport) to the declarations in the header file. To improve the readability of your code, define a macro for __declspec (dllexport) and use the macro for each symbol that you are exporting:

#define Dllexport   

__declspec (dllexport) stores the function name in the exported table of the DLL. If you want to optimize the size of a table, see Exporting a function from a DLL by ordinal rather than by name.

Description

When porting DLL source code from Win16 to Win32, replace each instance of __export with __declspec (dllexport).

For your reference, search in the Win32 Winbase.h header file. It contains an example of the use of __declspec (dllimport).

URL: https://msdn.microsoft.com/zh-cn/a90k134d (v=vs.90)

Module definition (. DEF) file

The module definition (. def) file provides the linker with information about the export, attributes, and other aspects of the linked program. When you build a DLL, the. def file is most useful. Because there are linker options that can be used in place of module definition statements,. def files are not usually required. You can also use __declspec (dllexport) as a means to specify the exported function.

You can invoke a. DEF file in the linker phase using the/DEF (Specify module definition file) linker option.

If the generated. exe file is not exported, using the. def file will make the output file larger and reduce the load speed.

For an example, see exporting from a DLL using a DEF file.

For more information, see the following sections:

Rules for module definition statements

Exports

HeapSize

LIBRARY

NAME

SECTIONS

StackSize

STUB

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.