DLL Import and Export

Source: Internet
Author: User
Tags export class xslt
Visual c ++ Export from DLL

 

The. dll file layout is very similar to the. exe file, but there is an important difference: the DLL file contains the export table. The export table contains the name of each function that the DLL exports to other executable files. These functions are the entry points in the DLL. Only the functions in the export table can be accessed by other executable files. Any other function in the DLL is private to the DLL. You can use the dumpbin tool with the/exports option to view the DLL export table.

There are two methods to export functions from dll:

  • When a DLL is generated, create a module definition (. Def) file and use the. Def file. Use this method if you want to export functions from DLL by serial number instead of by name.

  • Use in Function Definition_ Declspec (dllexport)Keyword.

When exporting a function using any of the above methods, make sure to use the _ stdcall call convention.

 

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:

Copy code
LIBRARY   BTREEEXPORTS   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-mfc dll files, 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 name in the application can be called with the DLL. the exported names in the def file match.

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:

Copy code
#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 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.

Use _ declspec (dllexport) to export data from DLL

 

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:

Copy code
__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:

Copy code
class __declspec(dllexport) CExampleExport : public CObject{ ... class definition ... };

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:

Copy code
#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.

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.