Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++Delphi

How To Use A C++ DLL In Any Delphi Program

How To Use A C++ DLL In Any Delphi Program

There is a glorious wealth of useful C++ libraries available on the internet. We have featured many great uses of C++ here on this blog too. C++ is typically extremely high performance. If we can have the source code of the C++ library, we can create a package which will then allow us to use the C++ in our Delphi programs. Often though the source code of the C++ library is not available. In commercial C++ libraries, it is common to get only a few C++ headers and the static library file (.lib) without any of the accompanying .cpp source files. So, in this case, when we want to use those C++ libraries in our Delphi application, we can use a Proxy DLL to make it possible.

This is great it you are looking to integrate existing C++ code into Delphi. If you want to write new C++ code you can use C++Builder which is a part of RAD Studio. It provides a streamlined integrated experience for C++ developers.

How to create a Proxy DLL to connect C++ DLL and Delphi?

To connect Delphi to a DLL, the DLL should expose a simple Windows API style API instead of C++ things. We can use any of the C++ compilers to compile the Proxy DLL.

Embarcadero Dev-Cpp Open Source Compiler

Visual Studio

C++Builder which is the C++ side of RAD Studio supports building package files (BPL) which can be used by Delphi directly. The great thing about C++Builder is that it integrates so closely with Delphi itself. You can use Object Pascal files from within C++Builder and the C++ BPL files from within Delphi. Find out more information.

Now, let’s take a simple example. Let’s think we have a DLL file and a harder file with this declaration and no .cpp file with the implementation. You can refer to the source code of this example in this link:

https://github.com/PacktPublishing/Delphi-High-Performance/tree/master/Chapter%208/StaticLib1

Here’s how to use a C++ library in a Delphi program with a proxy dll

Now we need to create the proxy DLL.

Create a new C++ DLL project with your preferred IDE.

How To Use A C++ DLL In Any Delphi Program = New Proxy DLL Project

It will automatically add “dllmain.cpp” file. But we need another unit to wrap the static library. Add new unit called “StaticLibWrapper.cpp”.

Now we should include the header file of the static library we want to import in our project.

Now copy the header file of the static library to the project folder. Now we should include the static library in our project. To do that add the static library folder to the library directories:

How To Use A C++ DLL In Any Delphi Program - import-static-library

Or in Visual Studio goto “Configuration Properties Linker General Additional Library Directories settings”.

How to mark C++ DLL functions as “exported”

Now we define a Macro to mark DLL functions are exported.

Next, implement the IndexAllocator class to cache the C++ objects. This class contains an array of pointers. It has three functions as “Allocate”,  “Release” and “Get” to store the pointer in the cache, release the cache, and get a pointer by index.

Then we need to Initialize and Finalize functions to allocate and deallocate IndexAllocator objects.

Then we create an instance of CppClass class and store it in the cache with this function.

In this statement, we use “C” to make sure the same name is exported and WINAPI to change the calling conversion. DestroyCppClass is similar to this. Next, take a look at the main export functions “CppClass_setValue” and “CppClass_getSquare”. When the user calls these functions, it will get the object from the cache and call those functions and take the value.

The first function will get an index of the object and set the value of the variable. In the second function, it takes the index of the object, calls the “getSquare” function of the object, and store the value in the value variable.

How to use the C++ Proxy DLL in a Delphi Application?

We can link DLLs either statically or dynamically. With static loading, the DLL will be loaded when the application starts. With dynamic loading, the DLL will not load until we call the “LoadLibrary”. Let’s use static loading for this example. Let’s declare the functions exported in the DLL.

We should call the “Initialize” function when our Delphi application is created and call the “Finalize” function when our application is destroyed. Then when we are using the Proxy DLL in our Delphi code, first we must call “CreateCppClass” to create an object. It will set the class ID to use in the future. Then we can call all the functions of the DLL and finally we should call “DestroyCppClass” function to destroy the class instance. In our example, we do it like this:

Finally the Delphi application will work like this:

How To Use A C++ DLL In Any Delphi Program = delphi cppclass import

RAD Studio allows you to rapidly create Delphi and C++ programs which can work on Windows, macOS, Linux, iOS and Android. Bring your program ideas to the devices your customers actually use by downloading a free trial today!


What's New for RAD Studio 11

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
DelphiRAD Studio

This Is How To Modernize Your Apps On Time And Under Budget

News

Announcing the Availability of RAD Studio 11 Alexandria

C++Code

This Week's Favorite Modern C++ Picks On LearnCPlusPlus.org

C++Code

This Week's New C++ Post Picks From LearnCPlusPlus.org

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES