Import and export in Windows DLL programming: __declspec (dllimport), __ declspec (dllexport ),

Source: Internet
Author: User

During windows DLL programming, you can use the _ declspec (dllimport) keyword to import functions or variables.

Function Import when you need to use functions in the DLL, the compiler can automatically import functions without displaying them. However, if you import functions explicitly, the compiler will produce code of better quality. Since the compiler knows exactly whether a function is in a DLL, it can produce better code without indirect call transfer. In Win32 PE format (portable executable format), all import addresses are placed in one import address table. The following example shows the difference between using _ declspec (dllimport) to import a function and not using it: Assume that func is a feature in a dll. currently, the main function of .exe is used to call func, if you do not explicitly import the func function (that is, there is no :__ declspec (dllimport), the code example is as follows: int main () {func ();} the compiler will generate a call code like this: Call func, then the linker translates the call into code like this: Call 0x40000001; ox40000001 is the address of "func" and, the linker will generate a thunk, such as: 0x40000001: jmp dword ptr _ imp_func. imp_funcis the address of the function slot in the import address table of func.pdf. Then, the local machine only needs to update the import address table of .exe when the local machine is running. If _ declspec (dllimport) is used to import functions explicitly, the linker will not generate thunk (if not required) and directly generate an indirect call. Therefore, the following code: _ declspec (dllimport) void func1 (void );
Void main (void)
{
Func1 ();
} The following call command will be called: Call dword ptr _ imp_func1. Therefore, the explicit import function can effectively reduce the target code (because Thunk is not generated ). In addition, functions other than dll can also be used in DLL to improve space and time efficiency. The import of variables is different from that of functions. When using the variables in DLL, You need to import the variables explicitly. Use the _ declspec (dllimport) keyword to import a variable. If you use. Def in the DLL to export the variable, you should use data to modify the variable instead of using the abandoned constant. Because constant may need to use pointers to indirectly access variables, it is not sure when the problem will occur.

First look at the code: Below is the code in DLL. h when you create your own DLL in Dev-C ++. Here is a: _ declspec (dllexport)

# Ifndef _ dll_h _
# DEFINE _ dll_h _ // anti-repeated Definition

# If building_dll
# Define dllimport _ declspec (dllexport)
# Else/* Not building_dll */
# Define dllimport _ declspec (dllimport)
# Endif/* Not building_dll */

Dllimport void helloworld (void );

# Endif/* _ dll_h _*/

 

The _ delcspce (dllexport) in the above Code is defined as a macro, which can improve the readability of the program! This is to define the function as an export function. That is to say, this function will be called by a program other than the function! In this statement, void helloword (void ):

From msdn: in the 32-bit compiler version, you can use_ Declspec (dllexport)Keyword: exports data, functions, classes, or class member functions from DLL._ Declspec (dllexport)Add the Export command to the object file

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 : 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)Declaration added to 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 determine the method (. Def file or _ declspec (dllexport) keyword used to export a function), answer the following questions:

  • Do you want to add additional export functions all the time?

  • Who wants to use DLL? For example, is it a third-party DLL used by many executable files that cannot be regenerated or a DLL used only by applications that can be easily regenerated?

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)

It is very convenient to use _ declspec (dllexport) because you do not need to maintain the. Def file and get 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.

 

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.