Use Borland C ++ builder to write DLLs

Source: Internet
Author: User
Tags export class
Use Borland C ++ builder to write DLLs
Author: [Yin Jia Jun (yinjiajun@gmail.com) Nanjing Shuncheng technology]
Preface
The methods for compiling and calling DLL for BCB circulating on the Internet mostly come from the BCB ultimate DLL coding manual, which is reposted on most websites. Article The source and author are not specified, so it is quite chilling. Some examples cannot be correctly run due to some leaks during the reprinting process. I have reorganized and published some examples based on the network information.
If you want to reprint it, please indicate the source and author, and send an email to the author. Thank you.

1. Note:
When creating a dynamic link library, if you want to create a dynamic link library not only for borland development tools, you need to follow the following rules:
(1 ). do not use Borland-specific data types and struct, such as ansistring, in the return values and parameters of the exported function, please use the C/C ++ standard data type or struct defined using the C/C ++ standard data type (in particular, do not use the string data type, A large part of the DLL project file generated by the bcb dll wizard is described here. Please check it yourself );
(2 ). use the extern "C" naming convention. In this way, the export function in the generated dll will not use the naming convention of C ++, but the C naming convention, that is, the exported function does not break down the name, but is the same as the function you defined;
(3 ). to export functions, use the Win32 API call method _ stdcall (winapi) or the call convention between VC and BorlandC ++ _ cdecl. Do not use the Borland-specific _ fastcall call convention, otherwise, only Borland development tools can use these dynamic link libraries;

Ii. Example:
(1). Export function (VCL is not used)
Use the bcb dll creation Wizard to create a project, select not to use VCL, Code As follows:
//---------------------------------------------------------------------------
# Include
//---------------------------------------------------------------------------
// Important note about dll Memory Management when your DLL uses
// Static version of The Runtime Library:
//
// If your DLL exports any functions that pass string objects (or structs/
// Classes containing nested strings) as parameter or function results,
// You will need to add the library memmgr. lib to both the DLL project and
// Any other projects that use the DLL. You will also need to use memmgr. Lib
// If any other projects which use the DLL will be using Ming new or delete
// Operations on any non-tobject-Derived classes which are exported from
// DLL. Adding memmgr. lib to your project will change the DLL and its calling
// EXE's to use the borlndmm. dll as their memory manager. In these cases,
// The file borlndmm. dll shocould be deployed along with your DLL.
//
// To avoid using borlndmm. dll, pass string information using "char *" or
// Optional string parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need
// Explicitly add memmgr. Lib as this will be done implicitly for you
//---------------------------------------------------------------------------
# Pragma argsused
Int winapi dllentrypoint (hinstance hinst, unsigned long reason, void * lpreserved)
{
Return 1;
}
//---------------------------------------------------------------------------
Extern "C" _ declspec (dllexport) int _ stdcall calc (int A, int B) // export the Function
{
Return A + B;
}
//---------------------------------------------------------------------------
Token tool generation (see this article for instructions ).

(2). Export the function (using VCL)
Use the bcb dll Wizard to create a project, select VCL support, and create a new form (with the default name tform1) to save as unit1.cpp, set the size of the form to the size of the normal okcancel pair box (set to 277*119 here), add two buttons on the form: The button1 label is "OK", and the modalresult is mrok, the button2 label is "canceled", and the modalresult is mrcancel;
Then in the DLL master Program REFERENCE The created form and add the export function. The Code is as follows:
//---------------------------------------------------------------------------
# Include
# Include
# Pragma hdrstop
//---------------------------------------------------------------------------
// Important note about dll Memory Management when your DLL uses
// Static version of The Runtime Library:
//
// If your DLL exports any functions that pass string objects (or structs/
// Classes containing nested strings) as parameter or function results,
// You will need to add the library memmgr. lib to both the DLL project and
// Any other projects that use the DLL. You will also need to use memmgr. Lib
// If any other projects which use the DLL will be using Ming new or delete
// Operations on any non-tobject-Derived classes which are exported from
// DLL. Adding memmgr. lib to your project will change the DLL and its calling
// EXE's to use the borlndmm. dll as their memory manager. In these cases,
// The file borlndmm. dll shocould be deployed along with your DLL.
//
// To avoid using borlndmm. dll, pass string information using "char *" or
// Optional string parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need
// Explicitly add memmgr. Lib as this will be done implicitly for you
//---------------------------------------------------------------------------
# Include "unit. H" // reference the design form
# Pragma argsused
Int winapi dllentrypoint (hinstance hinst, unsigned long reason, void * lpreserved)
{
Return 1;
}
//---------------------------------------------------------------------------
Extern "C" _ declspec (dllexport) int _ stdcall userclick (void) // export Function
{
Tform1 * form1 = new tform1 (null );
If (form1-> showmodal () = mrok ){
Delete form1;
Return 1;
} Else {
Delete form1;
Return 0;
}
}
//---------------------------------------------------------------------------
Generate the token tool.

(3). Export class
Use the bcb dll Wizard to create a project and select not to use VCL (in this example, if VCL is not used, whether or not VCL is supported depends on your own application). The Code is as follows:
//---------------------------------------------------------------------------
# Include
//---------------------------------------------------------------------------
// Important note about dll Memory Management when your DLL uses
// Static version of The Runtime Library:
//
// If your DLL exports any functions that pass string objects (or structs/
// Classes containing nested strings) as parameter or function results,
// You will need to add the library memmgr. lib to both the DLL project and
// Any other projects that use the DLL. You will also need to use memmgr. Lib
// If any other projects which use the DLL will be using Ming new or delete
// Operations on any non-tobject-Derived classes which are exported from
// DLL. Adding memmgr. lib to your project will change the DLL and its calling
// EXE's to use the borlndmm. dll as their memory manager. In these cases,
// The file borlndmm. dll shocould be deployed along with your DLL.
//
// To avoid using borlndmm. dll, pass string information using "char *" or
// Optional string parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need
// Explicitly add memmgr. Lib as this will be done implicitly for you
//---------------------------------------------------------------------------
# Pragma argsused
Int winapi dllentrypoint (hinstance hinst, unsigned long reason, void * lpreserved)
{
Return 1;
}
//---------------------------------------------------------------------------
_ Declspec (dllexport) _ stdcall class ttest {// declare the export class
Int A, B;
Public:
Void _ stdcall setvalue (int x, int y );
Int _ stdcall calc (void );
};
//---------------------------------------------------------------------------
Void _ stdcall ttest: setvalue (int x, int y) // class implementation
{
This-> A = X;
This-> B = y;
}
//---------------------------------------------------------------------------
Int _ stdcall ttest: calc (void) // class implementation
{
Return this-> A + this-> B;
}
//---------------------------------------------------------------------------
Generate the token tool.
Note that not all code needs to be written in the main file of the Project DLL. The export functions and classes can be designed in other units, and then # include in the main file, looking at your habits, of course, a huge DLL project cannot be written in a file.

(4). Use (1) the DLL created
Create a project.
[1]. Static call:
Add the static library DLLs. Lib generated in (1) to the project, and then add the definition of the export function in. H, as follows:
Extern "C" _ declspec (dllimport) int _ stdcall calc (INT, INT); // export the function in DLL
Then you can use the int _ stdcall calc (INT, INT) function in this unit, for example:
Showmessage (inttostr (calc (5, 10 ));
[2]. Dynamic call
You do not need to add the static library. Lib file. You only need to load the DLL when you need to call it, as shown below:
Hinstance HDL;
Int _ stdcall (* calc) (INT, INT); // define the function prototype
HDL =: loadlibrary ("DLLs. dll"); // load the DLL
If (HDL! = NULL ){
Calc = (INT _ stdcall (*) (INT, INT): getprocaddress (HDL, "calc"); // obtain the function entry address
If (calc! = NULL ){
Showmessage (inttostr (calc (5, 10); // call the function in the DLL
} Else {
Showmessage ("function entry cannot be found! ");
}
: Freelibrary (HDL); // do not forget to release the DLL after the call is completed.
} Else {
Showmessage ("cannot load DLL! ");
}

(5). Use (2) the DLL created
[1]. Static call:
Add the static library DLLs. Lib generated in (2) to the project, and then add the definition of the export function in. H, as follows:
Extern "C" _ declspec (dllimport) int _ stdcall userclick (void); // export the function in DLL
Then you can use the int _ stdcall userclick (void) function in this unit, for example:
If (userclick ()){
Showmessage ("click" OK "");
} Else {
Showmessage ("click" cancel "or close ");
}
[2]. Dynamic call
You do not need to add the static library. Lib file. You only need to load the DLL when you need to call it, as shown below:
Hinstance HDL;
Int _ stdcall (* userclick) (void );
HDL =: loadlibrary ("DLLs. dll ");
If (HDL! = NULL ){
Userclick = (INT _ stdcall (*) (void): getprocaddress (HDL, "userclick ");
If (userclick! = NULL ){
If (userclick ()){
Showmessage ("click" OK "");
} Else {
Showmessage ("click" cancel "or close ");
}
} Else {
Showmessage ("function entry cannot be found! ");
}
: Freelibrary (HDL );
} Else {
Showmessage ("cannot load DLL! ");
}

(6). Use (3) the DLL created
The class exported in dll can only be called using the declarative method. In this example, the DLL call method is as follows:
Add the static library DLLs. Lib generated in (3) to the project, and then add the declaration of the export class in. H, as follows:
_ Declspec (dllimport) _ stdcall class ttest {
Int A, B;
Public:
Void _ stdcall setvalue (int x, int y );
Int _ stdcall calc (void );
};
Then you can use the ttest class in this unit, for example:
Ttest * inst = new ttest;
Inst-> setvalue (5, 10 );
Showmessage (inttostr (inst-> calc ()));
Delete inst;

3. some tools:
(1).impdef.exe
usage: impdef.exe deffile. def yourdll. DLL
generates the def file of the specified DLL file. You can use it to view the function declaration in the DLL. For example, when BCB uses a vc dll, you may need to check the name of the function to be exported from VC, or if the extern "C" is not used to call the function, you can use it to view the C ++ naming method of the exported function in the DLL, so that it can be called correctly.
(22.16.implib.exe
usage: implib.exe libfile. Lib yourdll. dll
this tool is used to generate a static library called by DLL for the static call method of DLL.
(32.16.vctobpru.exe
this tool can be used to import the VC project to the BCB project. It automatically converts the VC project file to the BCB project file and generates the corresponding. and other files.
(42.16.tdump.exe(dumpbin.exe in vc)
usage: tdump.exe [Options] [inputfile] [listfile] [Options]
ignore (without parameters ).

Related Article

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.