[ZT] C ++ builder calls vc dll and VC calls C ++ builder DLL

Source: Internet
Author: User

Resolve the differences between _ cdecl ,__ fastcall and _ stdcall:
In the function call process, stacks are used, which indicate different stack call methods and release methods.
For example, _ cdecl is the stack calling method of the Standard C method, that is, the parameter pushing to the stack when a function is called is the opposite of the declared order of the function. The other two can be viewed on msdn, however, this does not play a major role in programming.
---------------------------------------------------------------
Call conventions
The call Convention (calling convention) determines the order in which function parameters are pushed to the stack. The caller or the caller pushes the parameter to the stack and generates the function modifier name. MFC supports the following call conventions:
_ Cdecl
The parameter is pushed from right to left to the stack. The caller pushes the parameter to the stack. For "C" functions or variables, the modifier name is underlined before the function name. For the "C ++" function, it is different.
For example, if the modified name of function void test (void) is _ test, the modifier name is _ test @ zaxxz (how does it feel like garbled Characters ??).
This is the default MFC call convention. Because the caller is responsible for popping up the parameter stack, you can define a variable number of parameters for the function, such as the printf function.
_ Stdcall
The parameter is pushed from right to left to the stack. The parameter is pushed to the stack by the caller. For a "C" function or variable, the modifier name is prefixed with the following line, followed by the function name, followed by the symbol "@" and the number of bytes of the parameter, such as the function int func (int, double B) the modifier is _ FUNC @ 12. For the "C ++" function, it is different. All Win32 API functions follow this convention.
_ Fastcall
The first two DWORD types or parameters that occupy less bytes are put into the ECX and EDX registers, and the remaining parameters are pushed to the stack in the order from right to left. The called calls bring up the parameter stack. For "C" functions or variables, the modifier name is prefixed with "@" and then the function name, the symbol "@" and the number of bytes of the parameter are followed. For example, the modifier of the int func (int A, double B) function is @ func @ 12. For the "C ++" function, it is different.
In the future, compilers may use different registers to store parameters.

Functions declared in DLL using _ declspec (dllexport:
_ Declspec (dllexport) only indicates that this function is a DLL export function, and _ stdcall is a function call convention, and there should be no conflict between the two.
For example, __declspec (dllexport) void _ stdcall atry ();

Differences between C ++ builder and VC descriptor Definitions
In C ++ Builder
_ Cdecl will contain: "_" before function output :"_"
_ Stdcall: No feature. Only the function name is output.
_ Fastcall function output Prefix :"@"
No "@ nn" suffix format!
In VC
_ Cdecl: No feature. Only the function name is output.
_ Stdcall is preceded by "_" Suffix: "@ nn"
_ Fastcall: "@" Suffix: "@ NN

C ++ builder calls the VC dll:
When compiling DLL in VC, The. Def file is used, and the _ declspec (dllexport) description is added before the exit function declaration. Place the DLL file generated by VC in the current directory and use the command line tool implib of BCB. lib file in the format of implib BCB. lib VC. DLL, then add the Lib file generated by implib according to the DLL to the project, and then add the DLL export function declaration to the project (before the function name, add winapi, that is, _ stdcall; _ declspec (dllimport) is added at the beginning of each function definition )).
In addition, BCB and VC ++ use different methods to set up function name conversion. Therefore, in VC, it is best to output a DLL whose function is a C function. If the output function is a C ++ class, it may not be called.
My solutions (two methods have been proved by my experiments)
Method 1: add the extern "C" {} keyword to the exported function header file when the c file is compiled by VC to generate the DLL. Add the call Convention descriptor _ cdecl to the Function Description and definition, then, you can add an underscore to both the function declaration and definition.
Example:
Suppose that the DLL of my vc contains int myfunction (void). The correct method of function implementation in the. c file is as follows:
_ Declspec (dllexport) int _ cdecl _ myfunction (void)
{
// Add your code here
}
In the. h file, the correct statement of function declaration is as follows:
_ Declspec (dllexport) int _ cdecl _ myfunction (void );
When calling BCB, you only need to include the Lib file. The procedure is as follows:
Run implib BCB. Lib VC. dll
Select the. Lib type from the project-> Add to... drop-down box and open the Lib file generated by the DLL of implib and VC.
The. C source file of the DLL used in the project contains the header file of the DLL.
You can directly write int I = myfunction.

Method 2: this parameter is valid only when the DLL is generated for the c file compiled by VC. Add the extern "C" {} keyword to the exported function header file. In BCB project-> Option-> advanced compiler, select stdcall in calling convention to directly call the dynamic link library generated by compiling the. c file of VC.

VC calls the DLL of C ++ builder:(Refer to msdn2000)
If there is no Lib in VC, the DLL is implicitly linked. Make the Lib function symbol input Library (for conversion) that matches VC ++. Please note that! This method can only be applied to the _ stdcall call method output in C format!
1. Use the dumpbin tool of VC ++ to export the export function table in DLL to a certain definition (. Def) file.
Example:
Dumpbin videodecoder. dll/exports/out: videodecoder. Def
2. Sort the exported. Def file into a function export file that conforms to the number of. Def files (the sorting process is very messy and complicated, so I am too lazy to give an example. There is a simple method later ^_^)
3. Use the Lib tool of VC ++ with/DEF :(. Def file name)/machine: ix86 (80 x86 machine) to output lib files in the VC ++ format.
Example:
LIB/DEF: videodecoder. DEF/machine: ix86
4. link to the Lib file during connection. Note that when some dynamic library dumpbin only has the function name and does not have the "@ nn" parameter format, such as the DLL written by C ++ builder, only the function name is output, and an error will be reported during the link:
Error lnk2002: unresolved external symbol "functionname @ nn"
The system prompts that the function symbols introduced in the program cannot be identified. In this case, you only need to change the corresponding function name in the def file to the functionname @ NN method, re-create the Lib, and re-link it.
In this way, the lib that meets the VC call method is created!

It is worth noting that! Borland C ++ builder has a good tool impdef which can directly output functions in the DLL. in the def file, this method can only be used to output the _ stdcall call method in the C format. You only need to make a few modifications to become a Def file that complies with VC!
Impdef XXX. Def XXX. dll
You only need to convert the function declaration format in the def file of BCB to the format recognized by VC. You can use the Lib tool to generate Lib. You need to use the C-cell output (extern "C! And don't forget to include "_" in the function declaration in the def file! :) Otherwise, the Link error of error lnk2001 will appear!
I have not tried BCB calling by VC, But I can modify it by referring to the above format :)

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.