C++Introduction to C++Language FeatureLearn C++

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

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

  • What is a Static Library?
  • What is a DLL? How can I create a new DLL Dynamic Library?
  • Can I create a new DLL using the VCL framework?
  • How can I create and call a function in a Dynamic Library?
  • Where can I find a simple DLL example in C++?
  • Can we develop Dynamic Link Libraries in C++ Builder?

By learning how to create a new Windows VCL 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 (or our own). In C++ we can use libraries by adding their header files to our code.

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 C or C++ static library?

Static Library is a C or C++ library that can be used by other applications. A static library is combined with the output of your code a part of your application when you compile your application. This combination happens at compilation and linking time. That means, if you are using a static library, it costs a memory whatever you use or not. The compiler compiles static library every time you compile/link them. Thus, we should be careful about static libraries. The standard C++ libraries are good example of static libraries.

What is a dynamic link library (DLL)?

Dynamic Link Libraries (DLLs) are modules of pre-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.

Another use of DLLs is to write your own DLLs to be used in a program written in a code editing / compiler program other than 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 RAD Studio C++ Builder?

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

How to Create a New Windows VCL DLL In C++ Choosing to create a DLL

Choose “Dynamic Library” and Press OK. Now you will see more options about your new DLL

How to Create a New Windows VCL DLL In C++ Selecting the target framework

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 the official DocWiki of Embarcadero.

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

As mentioned in the comment lines of code above we should note that there is an Important point 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 can I create a function in a Dynamic Library?

Let’s assume we have an application to which 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 (export) 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++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?