Several Problems about function export in the dynamic link library

Source: Internet
Author: User
We know that to allow the DLL to export some functions, we need to add the identifier _ declspec (dllexport) before each function to be exported. For example, we can export such functions (methods) in the DLL)

# Define dll1_api _ declspec (dllexport)

Dllbench API int add (int A, int B)
{
Return A + B;
}

If you want to view the export status of your DLL, you can do this. There is a dumpbin.exe file in the vc98 \ bindirectory under your VC installation directory, which is used to view the DLL file information, you can run the dumpbin-Exports dllname command under the command line (CMD) to view the DLL export function list.

In the above list, add and substract are the export functions. When calling an API, you can use the extern keyword or the _ declspec (dllimport) identifier to declare it.CodeHigher efficiency.

Now we can solve the name adaptation problem. When the C ++ compiler generates a DLL, it will modify the name of the exported function, and different compilers use different adaptation rules, therefore, the modified name is different. In this way, if different compilers are used to generate the DLL and the client accessing the DLL respectivelyProgramThe latter will cause problems when accessing the export function of the DLL. In the preceding example, what is the name after function add is adapted from the C ++ compiler? Add @ yahhh @ Z. We hope that the name after compilation will not change. Here are several methods.

The first is to add the qualifier: extern "C" when defining the export function"

# Define dll1_api extern "C" _ declspec (dllexport)

However, extern "C" only solves the problem of calling between C and C ++. It can only be used to export global functions, but cannot export member functions of a class. In addition, if the calling conventions of the exported function change, even if extern "C" is used, the compiled function name will be adapted. For example, if we add the _ stdcall keyword, the call convention is the c call Convention (standard call Convention, that is, the winapi call Convention ).

# Define dll1_api extern "C" _ declspec (dllexport)

Dll1_api int _ stdcall add (int A, int B)
{
Return A + B;
}

After compilation, as shown in figure


The function name Add is adapted to _ add @ 8.

The second method is to solve the problem through a module definition file def.

Library dllname

Exports
Add
Subtract

The library is used to specify the internal name of the dynamic link library. The name must match the generated Dynamic Link Library name. This code is not required. Exports describes the functions to be exported by the DLL and the symbol names specified for these export functions.


As you can see, the function name exported after DLL compilation by using the module definition file of the second method will not change.

This article is rough and only records the Problems and Solutions encountered in the author's learning and application. It only paves the way for the loading and hook programming of Dynamic Link Libraries in the following blog posts. I hope it will be helpful for readers to learn DLL programming. If you have any questions, please post a message and post a post to indicate the source.

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.