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. 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 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 #pragma once class CppClass { int data; public: CppClass(); ~CppClass(); void setData(int); int getSquare(); }; #pragma once class CppClass { int data; public: CppClass(); ~CppClass(); void setData(int); int getSquare(); }; 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. 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. #include “stdafx.h” #include “CppClass.h” #include “stdafx.h” #include “CppClass.h” 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: 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. #define EXPORT comment(linker, “/EXPORT:” __FUNCTION__ “=” __FUNCDNAME__) #define EXPORT comment(linker, “/EXPORT:” __FUNCTION__ “=” __FUNCDNAME__) 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. bool Allocate(int& deviceIndex, void* obj) bool Release(int deviceIndex) void* Get(int deviceIndex) bool Allocate(int& deviceIndex, void* obj) bool Release(int deviceIndex) void* Get(int deviceIndex) Then we need to Initialize and Finalize functions to allocate and deallocate IndexAllocator objects. extern “C” int WINAPI Initialize() extern “C” int WINAPI Finalize() extern “C” int WINAPI Initialize() extern “C” int WINAPI Finalize() Then we create an instance of CppClass class and store it in the cache with this function. extern “C” int WINAPI CreateCppClass (int& index) extern “C” int WINAPI CreateCppClass (int& index) In this statement, we use “C” to make sure the same name is exported and […]
