C++Introduction to C++Learn C++

How to Create a New Windows FMX DLL In C++

In this post, you’ll get answers to these questions:

  • What is Static Library?
  • What is DLL?
  • How do I create a new DLL Dynamic Library?
  • Can I create a new DLL using FMX framework? How can I create a function in a Dynamic Library? Where can I find a simple DLL example as a C++ Builder FMX?
  • Can we develop Dynamic Link Libraries in C++ Builder for Windows FireMonkey Applications?
  • Is it possible to use DLLs in multi-device applications?

By learning how to Create a New Windows FMX DLL In C++, it will help you to build C++ applications with the use of a C++ IDE.

What is a Library in C and C++?

library in C and C++ is a collection of functions, methods, classes and many other features of C++ exposed for use by other programs. In C++ we can use libraries by adding their header files.

In C++ programming, a library consists of an interface expressed in a .hpp file (header) and an implementation expressed in a .cpp file. This .cpp file might be precompiled as a lib file that can be used with it’s header by other applications

In C programming, a library consists of an interface expressed in a .h file (“header) and an implementation expressed in a .c file. This .c file might be precompiled as a lib file that can be used with it’s header by other applications.

Both C and C++ library sources and headers are in text forms. When they compiled , compiler generates library files.

What is a Static Library in C++?

Static Library is a C or C++ library that can be used by other applications sources in compilations. Standard C++ libraries are good example to static libraries. Static library is a part of your application when you compile your application. That means, if you are using a static library, it costs a memory whatever you use or not. Compiler compiles static library every time you compile them. Thus, we should be careful about static libraries to avoid using extra memory and having extra file size.

What is Dynamic Link Library (DLL) in C++?

Dynamic Link Libraries (DLLs) are modules of compiled code that work in conjunction with an executable to provide functionality to an application. There are two typical uses of dynamic link libraries. The first is to use a repository of external DLLs written by a third party (for example, Microsoft). The second is to write your own DLLs that you want to share between your programs, and another application in a different programming language.

The second use of DLLs is to write your own DLLs to be used in a program outside of RAD Studio. You can create a DLL project from this menu,

New > Other > C++Builder Projects > Dynamic-link Library

You can compile and build a DLL by using Run/Parameters, and browse for the executable of the host application. Packages are special DLLs used by Delphi applications, the IDE, or both. There are two kinds of packages: runtime packages and design-time packages. Runtime packages provide functionality to a program while that program is running. Design-time packages extend the functionality of the IDE.

DLLs and libraries should handle all exceptions to prevent the display of errors and warnings through Windows dialogs.

How to create a new DLL in C++?

We can create a new DLL by using File -> New -> C++ Builder menu,

How to Create a New Windows FMX DLL In C++ creating a new DLL

Choose Dynamic Library and press OK. Now you will see more options about your new DLL,

How to Create a New Windows FMX DLL In C++ choosing the DLL type

We can create C or C++ DLL and also we can choice Target Framework if it is needed. If you want to develop Windows VCL DLL. More details about these options can be found here in official DocWiki of Embarcadero.

Here let’s choice C++ as a Source Type and choice Visual Component Library as a Target Framework. Press OK. This will generate our DLL project automatically as below

As mentioned in the reminder lines of code above we should bold that there is an Important note about DLL memory management when your DLL uses the static version of the RunTime Library that says,

“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”
.

How to create and call a function in a Dynamic Library with C?

Let’s assume we have an application and we want to add some of the functions in a Dynamic Library. This DLL can be used by other applications too. For example, let’s create a very simple circular_area() circular area calculation function that operates with a given radius as below,

We want users to use this function with this library, so we should extern this function as DLL function. To do this we must declare this function before with extern “C” __declspec(dllexport) prefix. This declares this function is a DLL function that can be used as a external function. You don’t need to extern all functions. This allows you select which functions can be used by user which can not be used. For example we can extern our circle_area() function as below,

We can declare this in the earlier lines of our DLL source lines. Thus, our first simple DLL example will be like this,

We can create C or C++ DLL and also we can choice Target Framework if it is needed. If you want to develop Windows VCL DLL. More details about these options can be found here in official DocWiki of Embarcadero.

Here let’s choice C++ as a Source Type and choice Visual Component Library as a Target Framework. Press OK. This will generate our DLL project automatically as below

As mentioned in the reminder lines of code above we should bold that there is an Important note about DLL memory management when your DLL uses the static version of the RunTime Library that says,

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.”

How to reference a function in a Dynamic Library in C++?

Let’s assume we have an application and we want to add some of the functions in a Dynamic Library. This DLL can be used by other applications too. For example, let’s create a very simple circular_area() circular area calculation function that operates with a given radius as below,

We want users to use this function with this library, so we should extern this function as DLL function. To do this we must declare this function before with extern “C” __declspec(dllexport) prefix. This declares this function is a DLL function that can be used as a external function. You don’t need to extern all functions. This allows you select which functions can be used by user which can not be used. For example we can extern our circle_area() function as below,

We can use the __declspec keyword to indicate the storage class attributes for a variable or function. The __declspec keyword extends the attribute syntax for storage class modifiers so that their placement in a declarative statement is more flexible. The __declspec keyword and its argument can appear anywhere in the declarator list, as opposed to the old style modifiers, which could only appear immediately preceding the identifier to be modified.

We can declare this in the earlier lines of our DLL source lines. Thus, our first simple DLL example will be like this,

If you need more examples, please check this official DLL example from the DocWiki of Embarcadero.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?