Export and Import DLL Functions in C ++

Source: Internet
Author: User
Tags export class

Export from DLL using def File

The module definition (. Def) file is a text file that contains one or more module statements describing various DLL attributes. If you do not use_ Declspec (dllexport)If the keyword is used to export the DLL function, the DLL needs the. 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 a DLL. The library statement is followed by the DLL name. The linker puts this name into the DLL Import and Export Database.

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

For example, the DLL containing the Code that implements the binary search tree may look like the following:

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 will create the main. Def file for you and automatically add it to the project. Add the function name to export to this file. For non-mfcdll, you must create a. Def file and add it to the project.

If you export a function in the C ++ file, you must put the modifier name in the. Def file, or use external "C" to define an export function with a standard C link. To put the modifier name in the. Def file, you can use the dumpbin tool or the/map linker option to obtain the modifier name. Note that the modifier generated by the compiler is specific to the compiler. If you place the modifier name generated by the visual C ++ compiler. def file, the application linked to the DLL must also be generated using the same version of Visual C ++, so that the modifier in the application can be called
The exported name in the. Def file of DLL matches.

If the extension DLL is generated and exported using the. Def file, place the following code at the beginning and end of the header file containing the export class:

# Undefafx_data

# Defineafx_data afx_ext_data

// <Body of your header file>

# Undefafx_data

# Defineafx_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 exampleDeclare_dynamicThis macro extendsCruntimeclassAdd member variables to the class. Saving these four lines of code may result in incorrect compilation or link to the DLL, or errors when the client application is linked to the DLL.

When a DLL is generated, the linker uses the. Def file to create the exported (. exp) file and the imported (. Lib) file. Then, the linker uses the exported file to generate the DLL file. The executable file that is implicitly linked to the DLL is linked to the import/export warehouse during production.

Note that MFC uses the. Def file to export functions and classes from mfcx0.dll.

 

Export from DLL using _ declspec (dllexport)

Microsoft introduced_ ExportTo enable the compiler to automatically generate export names and put them in a. Lib file. Then, the. Lib file can be used to link to the DLL like static. Lib.

In the updated compiler version, you can use_ Declspec (dllexport)Keyword: exports data, functions, classes, or class member functions from DLL._ Declspec (dllexport)The Export command is added to the object file, so you do not need to use the. Def file.

This convenience is the most obvious when you try to export a C ++ modifier. Because there is no standard specification for name modification, the name of the export function may change in different compiler versions. If you use_ Declspec (dllexport). The DLL and dependent. EXE files must be re-compiled only when any naming conventions are changed.

Many export commands (such as serial numbers, noname, and private) can only be created in the. Def file, and these attributes must be specified using the. Def file. However, we also use the. Def file._ Declspec (dllexport)Will not cause generation errors.

To export a function,_ Declspec (dllexport)The keyword must appear on the left side of the call 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 the class, the keywords must appear on the left of the class name, as shown below:

Class _ declspec (dllexport) cexampleexport: publiccobject

{... Classdefinition ...};

When a DLL is generated, a header file containing the exported function prototype and/or class is usually created, and_ Declspec (dllexport)Add it to the declaration in the header file. To improve code readability_ Declspec (dllexport)Define a macro and use the macro for each symbol being exported:

# Define dllexport _ declspec (dllexport)

_ Declspec (dllexport)Store the function name in the DLL export table. To optimize the table size, see export functions from DLL by sequence number rather than by name.

Note:Use_ Declspec (dllexport)Replace_ Export.

For reference, search in the Win32 WINBASE. h header file. It contains
_ Declspec (dllimport)
.

Use afx_ext_class to export and import data

Extended DLL usageAfx_ext_classMacro export class; Use the macro import class to link to the executable file of the extended DLL. The same header file used to generate the extension DLL can beAfx_ext_classMacros are used together with executable files linked to DLL.

In the DLL header fileAfx_ext_classThe keyword is added to the class declaration as follows:

Classafx_ext_class cmyclass: Public cdocument

{

// <Body of class>

};

When the pre-processor symbol is defined_ AfxdllAnd_ AfxextThe macro is defined_ Declspec (dllexport). However, when_ AfxdllBut Not Defined
_ AfxextThe macro is defined_ Declspec (dllimport). Pre-processor symbol after Definition_ AfxdllIndicates that the shared MFC version is being used by the target executable file (DLL or application. When_ AfxdllAnd_ AfxextThis indicates that the target executable file is an extension DLL.

When exporting from an extended DLL,Afx_ext_classIs defined_ Declspec (dllexport)Therefore, you can export the entire class without placing the modified names of all the symbols of the class in the. Def file. This method is used by the MFC example dllhusk.

Although this method can avoid creating all the modified names of the. Def file and class, it is more efficient to create a. Def file because the names can be exported by serial number. To use the. Def File Export method, place the following code at the beginning and end of the header file:

# Undefafx_data

# Defineafx_data afx_ext_data

// <Body of your header file>

# Undefafx_data

# Defineafx_data

Warning be careful when exporting inline functions because they may cause version conflicts. Inline functions are extended to application code. Therefore, inline functions will not be updated unless the application itself is re-compiled if inline functions are rewritten later. Generally, you can update the DLL function without generating a new application that uses the DLL function.

Export individual members in the class

Sometimes, you may want to export individual members in the class. For example, if you exportCdialogDerived class, you may only need to export the constructor andDomodalCall. You can useAfx_ext_class.

For example:

Classcexampledialog: Public cdialog

{

Public:

Afx_ext_classcexampledialog ();

Afx_ext_classint domodal ();

...

// Rest of Class Definition

...

};

You no longer export all the members of the class, but you may encounter other problems due to the way the MFC macro works. Several MFC helper macros actually declare or define data members. Therefore, you must export the data members from the DLL.

For example, when an extension DLL is generated,Declare_dynamicMacro is defined as follows:

# Define declare_dynamic (class_name)

Protected:

Static cruntimeclass * pascal_getbaseclass ();

Public:

Static afx_data cruntimeclass class # class_name;

Virtual cruntimeclass * getruntimeclass () const;

Take staticAfx_dataThe internal static object of the class declared by the hitting line. To export the class correctly and access runtime information from the executable file on the client, you must export this static object. Because static objects useAfx_dataModifier Declaration, so you only needAfx_dataDefined_ Declspec (dllexport)And when the client executable file is generatedAfx_data
Defined_ Declspec (dllimport). As we have defined this methodAfx_ext_classTherefore, you only need to refer to the class definitionAfx_dataRedefinesAfx_ext_classSame.

For example:

# UNDEF afx_data

# Defineafx_data afx_ext_class

Classcexampleview: Public cview

{

Declare_dynamic ()

//... Class definition...

};

# UNDEF afx_data

# Defineafx_data

MFC is always used in the internal defined data items of its macroAfx_dataSymbol, so this technology applies to all such cases. For example, it appliesDeclare_message_map.

Note: If you export the entire class rather than the selected class member, the static data member is automatically exported.

Advantages and disadvantages of using a. Def File

The export function in the. Def file allows you to control the export sequence number. When you add additional export functions to the DLL, you can assign them higher sequence numbers (higher than any other export functions ). When you perform this operation, applications that use the implicit link do not have to reconnect to the new import and export library that contains the new function. This is important, for example, when designing a third-party DLL that will be used by many applications. You can add additional functions to continuously enhance the DLL and ensure that existing applications continue to use the new DLL normally. Mfc dll is generated using the. Def file.

Another advantage of using the. Def file is that you can use the noname attribute to export the function, which only places the sequence number in the DLL export table. For DLL with a large number of export functions, using the noname attribute can reduce the DLL file size. For information about Writing module definition statements, see Module Definition Statement rules. For more information about sequence number export, see export functions from DLL by sequence number instead of by name.

Use. def file has the following Disadvantages: when exporting a function in a C ++ file, you must put the modifier name in. def file, or use the external "C" to define the export function with the standard C link to avoid name modification by the compiler.

To put the modifier name in the. Def file, you can use the dumpbin tool or the/map linker option to obtain the modifier name. Note that the modifier generated by the compiler is specific to the compiler. If you place the modifier name generated by the visual C ++ compiler. def file, the application linked to the DLL must also be generated using the same version of Visual C ++, so that the modifier name in the application can be called with the DLL. the exported names in the def file match.

Advantages and disadvantages of using _ declspec (dllexport)

Use_ Declspec (dllexport)It is very convenient because you do not have to consider maintaining the. Def file and obtaining the modifier of the export function. For example, if the DLL you designed is used by applications under your control, this method is very suitable. If you use the new export function to regenerate the DLL, you must also regenerate the application, because if you use a different version of the compiler for re-compilation, the modified name of the exported C ++ function may change.

 

Related Article

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.