Before trying to run the DLL generated by C ++ Builder, review how to call a DLL created by C ++ Builder may be of great help to your operations, you can load the DLL at runtime, instead of importing it to the database.
To call the DLL function, first select the menu Project | Add to Project method, and Add the import library to your C ++ Builder Project. Secondly, insert the # include declaration for the DLL header file in the C ++ source file that needs to call the DLL function, and add the code to call the DLL function. Program list A and B contain the source code used as the test DLL. Note that the test code implements two different call habits: _ stdcall and _ cdecl ).
There are good reasons for this. When you try to call a DLL compiled with Visual C ++, most of the headaches are caused by handling different call habits. Note that there is a function that does not explicitly list the call habits used. This unknown function is used as the identifier of a DLL function that does not list call habits.
- #ifdef __cplusplus
- extern "C" {
- #endif
- #ifdef _BUILD_DLL_
- #define FUNCTION __declspec(dllexport)
- #else
- #define FUNCTION __declspec(dllimport)
- #endif
- FUNCTION int __stdcall StdCallFunction(int Value);
- FUNCTION int __cdecl CdeclFunction (int Value);
- FUNCTION int UnknownFunction(int Value);
- #ifdef __cplusplus
- }
- #endif
- //------------------------------------------
- //Listing B: DLL.C
- #define _BUILD_DLL_
- #include "dll.h"
- FUNCTION int __stdcall StdCallFunction(int Value)
- {
- return Value + 1;
- }
- FUNCTION int __cdecl CdeclFunction(int Value)
- {
- return Value + 2;
- }
- FUNCTION int UnknownFunction(int Value)
- {
- return Value;
- }
Create A test DLL from list A and list B, Open C ++ Builder, and select File | New to call up the Object Repository. Select the DLL icon and click OK. C ++ Builder creates a new project with a source file. This file contains a DLL entry function and some include declarations. Select File | New Unit.
Save the new unit as DLL. CPP. Copy and paste the text from list A and insert the header file DLL. H. Copy and save the project from list B as BCBDLL. BPR. Next, compile the project and check the generated file. C ++ Builder generates a DLL and a import database with the. LIB extension.
At this time, you have three elements required to call the DLL in C ++ Builder: The DLL itself, the header file with the function prototype, which is used to connect to the database for import. Now we need a C ++ Builder project to Call DLL functions. Create a new project in C ++ Builder and save it to your hard disk.
- Introduction to C ++
- C ++ functions in C ++
- How to Learn C ++ well?
- How to Develop C ++ in Linux
- Select the major elements of the C ++ Editor
Copy the DLL, import to database, and DLL. H header files from the DLL project directory to the new directory. Second, add the # include declaration in the main unit, including DLL. H. Finally, add the code to call the DLL function. Listing C lists the code that calls each function in the DLL generated by listing A and B.