How to create a DLL in C ++ Builder

zhaozj2021-02-16  58

How to create a DLL in C Builder

Since the launch of C Builder, it has attracted a lot of Delphi, VC, VB programmers since the romantic Valentine's Day last year, and a lot of C, C programmers sigh: finally has C visual development tools, the same From BC, Delphi to C Builder.

Dynamic Link Library (DLL) is a programming method that is often encountered by Windows. Here I introduce how to create how to use DLL and some tips in BCB (C Builder).

First, create:

Use BCB File | New to create a new DLL project and save file BCB to generate a DLL program frame.

1. The DLLLENTRYPOINT function is an inlet method. If the user is called when the DLL is initialized or logged out, it is used to write to the DLL initializer and uninstaller; parameter: HinST is used to indicate the base address of the DLL; Reason is used to indicate the DLL Calling mode, used to distinguish between multi-thread single thread pairs of DLL, create, uninstall DLL;

2. Add your own DLL procedure, function to the program;

3. Describe the exit with DLLIMPORT;

The routines are as follows:

#Include

#Pragma HDRSTOP

EXTERN "C" __DECLSPEC (DLLEXPORT) INT TEST ();

Int WinAPI DLLENTRYPOINT (Hinstance Hinst, Unsigned Long Reason, Void *)

{

Return 1;

}

Int test ()

{

Return 3;

}

Note: The dynamic link library is called process, and there are different CALL methods __cdecl, __pascal, __fastcall, __pascal, __fastcall, __cdcall, and BCB, if considering compatibility is available, and if the compatibility is available, the __stdcall declaration method is:

Extern "C" __DECLSPEC (DLLEXPORT) INT __STDCALL TEST ();

For the process, the function is also changed to:

INT __STDCALL TEST ()

Second, use DLL

There are two ways to use DLL in BCB:

1. Static invoice

First, you need to add an input interface library (Import Library), open the project, open the project list, and add the interface library (* .lib) to the project using BCB View | Project Manager.

Second, add an interface declaration in the header file.

The routines are as follows:

// deflude file

Extern "C" __declspec (dllimport) int __cdecl test ();

// Use function in main program

INT I;

I = test ();

note:

(1) Dynamic Link Library calls, the CALL mode is not written as __cdecl as the mode of creating, and other statements are required.

(2) The DLL created by BCB has a corresponding input interface library (Import library), if there is only DLL without the library, the BCB's IMPLIB tool can be generated: IMPLIB XXX.LIB XXX.DLL; additional: Tlib XXX.LIB, XXX .lst produces a list of internal functions of the DLL, many Windows unless otherwise discovered by this method.

2. Dynamic Call Method Dynamic Call Method To use loadLibrary () and getProcAddress () in the Windows API to tune the DLL library, pointing to the function location in the library, which is more common.

The routines are as follows:

Hinstance DD;

INT _STDCALL (* DDD) (VOID);

DD = loadLibrary ("xxx.dll");

DDD = GetProcAddress (DD, "TEST");

CAPTION = INTSTOSTR (DDD ());

Freelibrary (DD);

Third, pay attention:

When you create a DLL, you should pay attention to the Project Options when compiling.

Packages Tags: Remove Builder With Runtime Packages Check box.

Linker Tags: Remove the USE Dynamic RTL check box.

Otherwise, the DLL that is created requires Runtime Packages or Runtime Library.

转载请注明原文地址:https://www.9cbs.com/read-27125.html

New Post(0)