Site icon Learn C++

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

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

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,

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

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

[crayon-66066764bd7e7311579266/]

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,

[crayon-66066764bd7f3020207136/]

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,

[crayon-66066764bd7f9991490670/]

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

[crayon-66066764bd7fb819020057/]

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,

[crayon-66066764bd7fd461199402/]

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,

[crayon-66066764bd802581936216/]

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,

[crayon-66066764bd804716406119/]

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.

Exit mobile version