Some problems encountered when using the DLL generated by C to create executable modules using tools from other vendors

Source: Internet
Author: User

Author: closeall
Source: http://blog.csdn.net/closeall2008
Disclaimer: This article may be reproduced or reproduced without the consent of the author. However, please keep the author, source, and declaration of any reference to this article. Thank you.

Later last year, I wrote a simple network update program. It is indeed complicated to use VC to make the client program interface more beautiful. I want to rewrite it these days, that is, using C to make the communication module of the client into a DLL, and then using other programming environments as the interface, during communication, let it call the DLL written in C. But there are some minor issues involved here-if you use visual c ++ to create a DLL, and this dll should be linked to the executable module created using any vendor's tool, you must do some additional work.

We all know that when using C and C ++ for mixed programming, we need to use extern "c" to solve the problem of modifier. How to use C to write a DLL, and when using a third-party tool to create an executable module, there will also be a problem similar to modifying the function name symbol. When you use _ stdcall to output the C function, the Microsoft editor will modify the function name, set a leading underline, and add a @ symbol prefix, followed by a number, used to represent the number of bytes passed to the function as a parameter.

For example, the following function is output as _ dllfunction @ 4 of the DLL output byte. The number 4 indicates the number of bytes of the parameter.

_ Declspec (dllexport) long _ stdcall dllfunction (int x)

If you use a tool provided by another vendor to create an executable module, he will try to link it to a function named dllfunction, which does not exist in the existing DLL of Microsoft compiler, because its output name is _ dllfunction @ 4, the link will fail.

To create an executable module using a tool provided by a third-party vendor, you must inform the Microsoft compiler of the function name that has not been changed during output. How can I tell Microsoft compilers? There are two ways to solve this problem.

1. When compiling a DLL project, create a. Def file and add the following exports section in the. Def file:

Exports dllfunction

After this operation, when Microsoft connector is being analyzed. def file, he will find that both the _ dllfunction @ 4 and the dllfunction function names are output. Because the two function names match each other, the linked program will use the dllfunction. def file name outputs this function, which will solve this problem.

2. Do not use the. Def file. In the DLL source code file, you can add the following line of code:

# Pragma comment (linker, "/export: dllfunction = _ dllfunction @ 4 ")

(Full text)

Translator; closeall

Time: 2007.04 29

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.