Tutorial: Using Dynamic Libraries in C++Builder Applications (2025)

Go Up to Introductory Tutorials


This tutorial describes the main steps to use Dynamic-Link Libraries (DLLs) in your C++Builder applications (both 32-bit Windows and 64-bit Windows).

A Dynamic-Link Library is a collection of routines that can be loaded at run time. For further information on DLLs, see Libraries and Packages (Delphi).

The following example contains a DLL that exports a class describing a stack of integers. The class is declared and implemented in the DLL project. The scope of this example is to dynamically load the DLL in a Firemonkey application, and to import and use the stack of integer class.

To export the class from the DLL:

  1. Write a C++ header file that declares a virtual base class for the stack. This virtual base class must contain the methods of the class that will be exported. This header file will be included in the project where the class from the DLL is imported.
  2. Declare and implement a class deriving from the virtual class of the stack. This is where the stack methods are implemented. In the following tutorial an integer implementation will be used for the stack.
  3. Implement and export a method which returns an instance of the stack of integer class. This method will be used to get an instance of the class declared in the DLL project.

Contents

    • 1.1 The Dynamic-Link Library
      • 1.1.1 BaseStack.h
      • 1.1.2 StackOfInt.h
      • 1.1.3 StackOfInt.cpp
      • 1.1.4 StackExport.cpp
    • 1.2 The Firemonkey Application
      • 1.2.1 TestUnit.h
      • 1.2.2 TestUnit.cpp
  • 2 Uses
  • 3 See Also

Steps & Source code

This tutorial will guide you through the steps of creating and exporting a class using Dynamic Linked Libraries. Also, you will learn how to import and use the previously exported class.

The tutorial is composed of two main projects:

  • StackLibrary.dll - This is the DLL project. Here the stack of integer is declared and implemented. Also, this project contains a method used to get an instance of the stack class. This method is exported from the DLL.
  • TestStackLibrary - This is a FireMonkey Application. In this project, the previously declared DLL is loaded and the exposed method is imported. After invoking this method, an instance of the stack class is retrieved. This project tests the methods of the stack class.

Here are the steps for creating the two projects:

The Dynamic-Link Library

  1. Select File > New > Other > C++Builder Projects > Dynamic-link Library and click OK.
  2. On the New Dynamic-link Library dialog box that opens, select Firemonkey as the Target Framework and click OK.

    Note: This DLL will not use any framework, even if the application that will load the DLL is a Firemonkey application.

  3. Select File > Save All and set the names of the created files as follows:
    1. Name the C++ file in the DLL project StackExport.cpp.
    2. Name the precompiled header file StackLibraryPCH1.h.
    3. Name the DLL project StackLibrary.
  4. Select File > New > Other > C++Builder Files > Header File or File > New > Other > C++Builder Files > CPP File as necessary to add the following new three files to the StackLibrary project:
    • BaseStack.h holds the definition of the virtual base class for the stack. This class is used in both the DLL and the Firemonkey project.
    • StackOfInt.h holds the declaration for the Stack class. Stack is a descendant of BaseStack, that describes a stack of integers.
    • StackOfInt.cpp contains the implementation for the Stack class.
  5. Add code to the files created above, as detailed in the subsections below.
  6. Build your project to generate the StackLibrary.dll DLL.

BaseStack.h

BaseStack is the virtual base class for Stack (declared in StackOfInt.h). Therefore, no implementation is required for its methods.

// Base Virtual class for Stack.// Declared to be used both in StackLibrary.dll and TestStackLibrary.exe projectsclass BaseStack {public: virtual bool IsEmpty() = 0; virtual int Top() = 0; virtual int Pop() = 0; virtual void Push(int value) = 0;};

StackOfInt.h

This file declares the Stack class. Stack describes a stack of integer, and declares its methods: Push, Pop, Top, and IsEmpty.

#include "BaseStack.h"// BaseStack implementation// Here the methods from BaseStack are implementedclass Stack : public BaseStack {private: // items holds the integers from the stack int* items; // represents the number of integers that are in the stack int count;public: // Stack constructor Stack(); // Stack destructor ~Stack(); // Checks whether the stack is empty bool IsEmpty(); // Returns the top of the stack int Top(); // Returns the top of the stack and removes the element from the stack int Pop(); // Inserts <code>value</code> into the stack void Push(int value);};

StackOfInt.cpp

#include "StackOfInt.h"#define MAX_SIZE 30#define INDEX_OUT_OF_BOUNDS_EXCEPTION 1#define EMPTY_STACK 2// Stack constructorStack::Stack() { count = 0; items = new int[MAX_SIZE];}// Stack destructorStack::~Stack() { delete[]items;}// Returns the top of the stackint Stack::Top() { if (count > 0) { return items[count - 1]; } else { throw EMPTY_STACK; }}// Returns the top of the stack and removes the element from the stackint Stack::Pop() { if (count > 0) { return items[--count]; } else { throw EMPTY_STACK; }}// Checks whether the stack is emptybool Stack::IsEmpty() { if (count == 0) { return true; } else { return false; }}// Inserts <code>value</code> into the stackvoid Stack::Push(int value) { if (count < MAX_SIZE) { items[count++] = value; } else { throw INDEX_OUT_OF_BOUNDS_EXCEPTION; }}

StackExport.cpp

This file exports the GetClassInstance function. This function returns an instance of Stack.

// 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 performing new or delete// operations on any non-TObject-derived classes which are exported from the// 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 should be deployed along with your DLL.//// To avoid using BORLNDMM.DLL, pass string information using "char *" or// ShortString parameters.//// If your DLL uses the dynamic version of the RTL, you do not need to// explicitly add MEMMGR.LIB as this will be done implicitly for you#pragma hdrstop#pragma argsused#include "StackOfInt.h"// This is the syntax for exporting methods from DLLs://// extern "C" __declspec(dllexport) void* __stdcall GetClassInstance();//// This method returns an instance of Stack:extern "C" __declspec(dllexport) void* __stdcall GetClassInstance() { return static_cast<void*>(new Stack);}extern "C" int _libmain(unsigned long reason) { return 1;}

The Firemonkey Application

  1. Right-click the project group in the Project Manager, and select Add new > Multi-Device Form - C++Builder.
  2. Select File > Save All and set the names of the created files as follows:
    1. Name the Firemonkey project TestStackLibrary.
    2. Name the C++ unit in the Firemonkey project TestUnit.

    Note: The source code provided later assumes that you save these files in the same folder where you saved the library files.

  3. In the TestStackLibrary form, add the following components:
    1. Add five TButton controls. Name them as follows and set their labels accordingly:
      • LoadButton
      • PushButton
      • TopButton
      • PopButton
      • IsEmptyButton
    2. For each button, add an OnClick event handler. You can add the code for the event handlers later.
    3. Add one TEdit control.
      This is how the Firemonkey form should look:
  4. In TestUnit.h, add the following two private members:
    • BaseStack* stack, to hold an instance of the stack.
    • void __fastcall EnableButtons().
    See TestUnit.h below.
  5. Implement TestUnit.cpp as described below.

You can now run your project. In the main window, click Load Library to load the library. After you manually load the library, the rest of the buttons in the form work against the dynamically-loaded library.

TestUnit.h

//---------------------------------------------------------------------------#ifndef TestUnitH#define TestUnitH//---------------------------------------------------------------------------#include <System.Classes.hpp>#include <FMX.Controls.hpp>#include <FMX.Edit.hpp>#include <FMX.StdCtrls.hpp>#include <FMX.Types.hpp>#include <FMX.Forms.hpp>//---------------------------------------------------------------------------#include "BaseStack.h"//---------------------------------------------------------------------------class TForm1 : public TForm{__published: // IDE-managed Components TButton *PushButton; TButton *PopButton; TButton *TopButton; TButton *IsEmptyButton; TButton *LoadButton; TEdit *Edit1; void __fastcall PushButtonClick(TObject *Sender); void __fastcall PopButtonClick(TObject *Sender); void __fastcall TopButtonClick(TObject *Sender); void __fastcall IsEmptyButtonClick(TObject *Sender); void __fastcall LoadButtonClick(TObject *Sender);private: // User declarations BaseStack* stack; void __fastcall EnableButtons();public: // User declarations __fastcall TForm1(TComponent* Owner);};//---------------------------------------------------------------------------extern PACKAGE TForm1 *Form1;//---------------------------------------------------------------------------#endif

TestUnit.cpp

// ---------------------------------------------------------------------------#include <fmx.h>#pragma hdrstop#include "TestUnit.h"#include "StackOfInt.h"// ---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.fmx"TForm1 *Form1;// Holds the name of the dynamic-link library.const wchar_t* library = L"StackLibrary.dll";// type definition for pointer to functions that return (void*)typedef void* (*VoidReturnFunc)();// syntax that declares an imported functionextern "C" __declspec(dllimport) void* __stdcall GetClassInstance();// ---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {}// ---------------------------------------------------------------------------// Method used to enable the buttons when the library is loadedvoid __fastcall TForm1::EnableButtons() { PushButton->Enabled = true; PopButton->Enabled = true; IsEmptyButton->Enabled = true; TopButton->Enabled = true;}// ---------------------------------------------------------------------------// OnClick event handler for LoadButtonvoid __fastcall TForm1::LoadButtonClick(TObject *Sender) { // Dynamically loads the library HINSTANCE load = LoadLibrary(library); if (load) { ShowMessage("Library loaded!"); EnableButtons(); VoidReturnFunc myFunc; // GetProcAddress returns a pointer to the loaded method myFunc = (VoidReturnFunc)GetProcAddress(load, "GetClassInstance"); // Invoke GetClassInstance method stack = (BaseStack*) myFunc(); // Note: // The BaseStack methods are pure virtual methods, but myFunc() returns an instance of the Stack class. // Wherever we call stack->methodName() later, we are actually calling Stack::methodName(), not // BaseStack::methodName(). } else { ShowMessage("Library not loaded!"); }}// ---------------------------------------------------------------------------// OnCLick event handler for PushButtonvoid __fastcall TForm1::PushButtonClick(TObject *Sender) { if (stack) { try { int value = StrToInt(Edit1->Text); stack->Push(value); Edit1->Text = ""; } catch (int) { ShowMessage("Please insert an integer into the editbox!"); } }}// ---------------------------------------------------------------------------// OnClick event handler for IsEmptyButtonvoid __fastcall TForm1::IsEmptyButtonClick(TObject *Sender) { if (stack) { if (stack->IsEmpty()) { ShowMessage("The stack is empty!"); } else { ShowMessage("The stack is not empty!"); } }}// ---------------------------------------------------------------------------// OnClick event handler for PopButtonvoid __fastcall TForm1::PopButtonClick(TObject *Sender) { if (stack) { try { int value = stack->Pop(); ShowMessage("Popped " + IntToStr(value)); } catch (int Exception) { ShowMessage("Exception " + IntToStr(Exception)); } }}// ---------------------------------------------------------------------------// OnClick event handler for TopButtonvoid __fastcall TForm1::TopButtonClick(TObject *Sender) { if (stack) { try { int value = stack->Top(); ShowMessage("The top of the stack is " + IntToStr(value)); } catch (int Exception) { ShowMessage("Exception " + IntToStr(Exception)); } }}// ---------------------------------------------------------------------------

Uses

  • FMX.StdCtrls.TButton
  • FMX.Edit.TEdit
  • FMX.StdCtrls.TButton.OnClick
  • Libraries and Packages (Delphi)

See Also

  • Creating Packages and DLLs
Tutorial: Using Dynamic Libraries in C++Builder Applications (2025)

FAQs

How to build a dynamic library in C++? ›

To create and use a dynamic library in C++ we can follow the below steps:
  1. Step 1: Create the Header File. ...
  2. Step 2: Create the Implementation File for the Header File. ...
  3. Step 3: Compile the Dynamic Library using GCC. ...
  4. Step 4: Using the Dynamic Library.
Jun 7, 2024

What is the difference between a static library and a dynamic library? ›

What is the main difference? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file.

How to load DLL in c++ builder? ›

The Dynamic-Link Library
  1. Select File > New > Other > C++Builder Projects > Dynamic-link Library and click OK.
  2. On the New Dynamic-link Library dialog box that opens, select Firemonkey as the Target Framework and click OK. ...
  3. Select File > Save All and set the names of the created files as follows:

How to create and use a DLL dynamic link library with C++? ›

Making and Using a Dynamic Link Library
  1. To make a DLL, open Visual Studio and start a new project for C++. Set the project type as Library.
  2. Click Dynamic-link Library (DLL) and create it. Add the code from the Microsoft website.
  3. Add a CPP file to the DLL, and then select Build Solution.
Apr 4, 2024

Can I make my own library in C++? ›

First, open your preferred text editor or IDE and start off by creating a new header file with the . h a and declaring the functions that we want to have in the library. Now, create another file that is the source code file with .

How do I create a dynamically linked library? ›

To create a Dynamic-link library (DLL), you must create one or more source code files, and possibly a linker file for exporting the functions. If you plan to allow applications that use your DLL to use load-time dynamic linking, then you must also create an import library.

Why use dynamic libraries? ›

Dynamic linking leads to smaller file sizes. The code of linked libraries does not need to be shipped with the executable file. This means less resource usage such as disk space or RAM. If a shared library is already available in the operating system, the start-up time can be reduced.

Which is faster static or dynamic library? ›

Static libraries resist vulnerability because it lives inside the executable file. The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker.

What is the difference between library and DLL? ›

Static Library (.lib) vs Dynamic Library (.dll)

LIB file) (or archive) contains code that is linked to users' programs at compile time. The executable file generated keeps its own copy of the library code. A dynamic library (. dll) (or shared library) contains code designed to be shared by multiple programs.

Is DLL static or dynamic? ›

Virtual memory links the same page of physical memory to different programs' virtual addresses -- also known as address space -- as different processes are run. In the Windows operating systems, dynamic files have a ". dll" file extension, and static files have a ". lib" extension.

How do you debug a DLL in C++? ›

Select the C++ DLL project in Solution Explorer. Select the Properties icon, press Alt+Enter, or right-click and choose Properties. In the <Project> Property Pages dialog box, make sure the Configuration field at the top of the window is set to Debug. Select Configuration Properties > Debugging.

What is an example of a dynamic-link library? ›

A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Each program can use the functionality that is contained in this DLL to implement an Open dialog box.

What is dynamic-link library in C++? ›

In Windows, a dynamic-link library (DLL) is a kind of executable file that acts as a shared library of functions and resources. Dynamic linking is an operating system capability. It enables an executable to call functions or use resources stored in a separate file.

What is a static and dynamic library in C++? ›

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

How do you create dynamic memory in C++? ›

The syntax for dynamically allocating memory in C++ is as follows: datatype * pointer = new datatype; Where "datatype" is the data type variable to allocate memory and "pointer" is the name of the pointer to which the new memory is allocated. The above syntax allocates memory for a variable of type "data type".

How do you create a dynamic object in C++? ›

C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically. The dynamic objects can be created with the help of pointers.

What is dynamic link library in C++? ›

In Windows, a dynamic-link library (DLL) is a kind of executable file that acts as a shared library of functions and resources. Dynamic linking is an operating system capability. It enables an executable to call functions or use resources stored in a separate file.

How to create a static library in C programming? ›

Creating Static Libary in C
  1. Step 1: Define the Header File.
  2. Step 2: Create the Source File for the Header.
  3. Step 3: Compile the Object File.
  4. Step 4: Create the Static Library.
  5. Example.
Jul 11, 2024

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6324

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.