DLL export function (Abstract)

Source: Internet
Author: User
Directory

1.There are two ways to export functions in dll:

2. view the DLL export function;

3. Differences between _ declspec (dllexport) and. Def files.

 

1. There are two ways to export functions in dll:

Method 1: add the identifier _ declspec (dllexport) before each function declaration)

For example, __declspec (dllexport) int add (int A, int B) {return a + B ;}

In this way, if the DLL is called by a C ++ program (the same compiler version), there is no problem. However, if you call this dll as a program in other languages (such as C # and VB), an error occurs. The reason is that there is a function overload in C ++ that allows the function to have multiple names. Therefore, when the compiler generates a DLL, it performs name conversion for certain algorithms. For example, the actual name of the add function is as follows.

Therefore, the function cannot be found directly through the function name Add, resulting in call failure. To solve this problem, we often add an extern "C" before the function to use the C-Way Function naming rule.

Extern "C" _ declspec (dllexport) int add (int A, int B );

In this way, the function name is "add.

 

Method 2: Use the module definition (. Def) file to declare the export Function

1. Create a New *. Def file in the project;

2. General Format of. Def file:

; Dlltestdef. Lib: export the DLL Function
;----
Library dlltestdef
Exports
Add @ 1
Sub @ 2

Note:

(1) The Library statement describes the DLL corresponding to the. Def file;

(2) Name of the function to be exported after the exports statement. You can add @ n after the export function name in the. Def file to indicate that the sequence number of the function to be exported is n (this sequence number will play its role during function calling );

(3) The annotation in the def file is specified by the semicolon (;) at the beginning of each comment line, and the comment cannot share a line with the statement.

3. Finally, remember to select the def file used in the linker option (this option is automatically added when the def file is added by default without manual modification ).

 

2. view the DLL export function;

Export dll's export tool to help dumpbin.exe. This tool is provided by Vs and is located in the [vs installation directory] \ Vc \ bin directory;

Run the following command on the console: dumpbin/exports test. dll.

If link.exe-system error occurs: "The program cannot be started because mspdb100.dll is lost in the computer. ", Run the [vs installation directory] \ Vc \ bin \ vcvars32.bat on the console before executing the preceding command;

3. Differences between _ declspec (dllexport) and. Def files.

1. Method 1: Add _ declspec (dllexport) before each function to be exported. This is cumbersome and the program is easy to use;

2. Reasons for using. Def:

Since there is no standard when defining DLL to export C ++ functions, GCC has its own export specifications and Vc has its own export specifications. Each compiler exports different functions, so the def file is generated. It tries to regulate the name of each export function. And these "compiler-generated function modifiers" are useful, such as _ stdcall or _ fastcall, in addition, the number and definition of function parameters (note that C ++ can be used for function overloading). The compiler will not add so many useless modifier names for no reason.

Finally, it is worth mentioning that the default call Method for C/C ++ is _ cdecl. In this way, the caller needs to clear the function stack. If other non-C ++ programs are provided for external APIs, the caller will not be able to clear the stack and make an error (C # will directly report the function declaration Mismatch Error ). Therefore, when providing APIs externally, you should also declare the interface as _ stdcall so that the API functions can be cleaned up by themselves. This is also the reason why a winapi macro is added before Windows APIs.

Def file has many other advanced usage, for further understanding, you can refer to the official documents of MS: http://msdn.microsoft.com/zh-cn/library/28d6s79h (V = vs.80). aspx

DLL export function (Abstract)

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.