Create a DLL--VB for non-visual C ++ tools call C ++ DLL why def files must be used

Source: Internet
Author: User

In a task completed last week, I found a bit confused:

When writing C ++ DLL for VB call, the def file must be used to define the export symbol. OtherwiseProgramFailed to link. We all know that the def file is used to prevent the C ++ compiler from renaming the function. Another alternative to achieving the same effect is to use the extern "C" identifier. If the DLL is called by C ++, C #, Java, and so on, the latter can work well. However, VB cannot, even if extern "C" is used to identify the export function, you still need to define the def file.

 

Just now, I found the answer: Windows core programming -- 19.3.2 creating a DLL for non-visual C ++ tools

 

According to the description, we can see that when _ stdcall is called, the compiler still changes the function name even if extern "C" is used. Therefore, the link may fail.

This article also mentions a method to solve this problem without relying on Def files.

 

The original article is as follows:

If you use Microsoft Visual C ++ to create a d l and the executable module that will be linked to the d l, you can skip this section. However, if you use visual c ++ to create d l, and this d l should be linked to the executable module created using any vendor's tool, you must do some additional work.

We have discussed the problem of using the e x t e r n "C" modifier when performing C and C ++ mixed programming. I also talked about the C ++ class and why you must use the tool of the same compiler vendor because of the name change. Another problem occurs when you use C programming directly to multiple tool vendors. This problem is that even if you do not use C ++, the C compiler of m I c r o s o f t will damage the C function. This problem occurs when your function uses _ s t d c a l (w I n a p I) to call rules. This call rule is the most popular type. When _ s t d c a l is used to Output C functions, the compiler of m I c r o s o f t will change the name of the function and set a leading underline, add the prefix of the @ symbol, followed by a number to indicate the number of bytes passed to the function as a parameter. For example, the following function is output as _ m y f u n C @ 8 in the output section of d l:

 
_ Declspec (dllexport) long _ stdcall myfunc (int A, int B );

If an executable module is created using a tool from another vendor, it tries to link to a function named m y f u n C, this function does not exist in the existing d l of m I c r o s o f t compiler, so the link will fail.

To create an executable module using the m I c r o s o f t tool linked to tools from other compiler vendors, the compiler output of m I c r o s o f t must be told without changing the function name. You can perform this operation in two ways. The first method is to create a. d e f file for the programming project, and add the Section E x p o rt s similar to the following in the. d e f file:

 
Exports
Myfunc

When the m I c r o s o f t link program analyzes this. in the d e f file, it finds that both _ m y f u n C @ 8 and m y f u n C are output. Because the two function names match each other (except the truncated end), the linked program uses m y f u n C. d e f file name to output this function, instead of using the _ m y f u n C @ 8 name.

Now you may think that if you use the m I c r o s o f t tool to create an executable module and try to link it to a d l containing a name not truncated, the link program will fail because it will attempt to link to a function called _ m y f u n C @ 8. Of course, you will be glad to understand that the m I C R o s o f t link program has been correctly operated, link an executable module to a function named m y f u n C.

If you want to avoid using the. d e f file, you can use the second method to output the function version that has not been truncated. In d lSource codeModule, you can add the following lineCode:

 
# Pragma comment (linker, "/export: myfunc = _ myfunc @ 8 ")

This line of code causes the compiler to issue a link program instruction to tell the link program that a function named m y f u n C will be output, the entry point is the same as the entry point of the function called _ m y f u n C @ 8. The second method is not as easy as the first method, because you must cut off the function name to create the code line. In addition, when the second method is used, d l actually outputs two symbols used to identify a single function, that is, m y f u n C and _ m y f u n C @ 8, the first method outputs only the m y f u n C symbol. The second method does not bring you more benefits. It just allows you to avoid using. d e f files.

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.