Wrapping C++ classes in a DLL called from Delphi

Posted on 2004-11-25
Last Modified: 2012-05-05
Hi all .. some advice, any gotchas or things to look out for ...

I am not a C++ programmer and don't want to be, and I don't know CPP Builder and am not sure if I have a copy .. if I do it is very old. (I don't want to write my projects in CPP Builder). However I need some code that is in C++ (to do with computational geometry - see cgal at and ).

If these were just C function calls, I could just put them in a DLL. But they are C++ classes.  I am wondering if there is a good way of "wrapping" them such that I can get access to their functionality.

Q1. Is it OK to have classes in a DLL?
Q2. Can I use interfaces in some fashion to help me call these C++ classes from Delphi,
and have the interfaces manage construction/destruction etc ?

Question by:Mutley2003
    LVL 17

    Accepted Solution

    Yes, you can have classes within a DLL. But a Delphi class and a C++ class are incompatible so you cannot use them in a mixed environment.

    You could use interfaces to communicate between C++ and Delphi but it will require you to change this C++ code quite a bit. Basically, you should make an ActiveX control out of it.

    One trick could be the use of simple function calls, but with one additional field for a pointer to the object. Basically, you just create functions for every C++ class method that you need to call and add one additional pointer to these methods. In the Delphi code, all you know is that it's a pointer to an object but you can't do anything with it. The C++ function will see it as a pointer to the object, thus it is able to call the method for this class and return a result value.

    You would get something like this in Delphi:

    type TSomeCPPObject=Pointer;
    function CreateSomeCPPObject:TSomeCPPObject;
    procedure FreeSomeCPPObject(Obj:TSomeCPPObject);
    function SomeMethodCall(Obj:TSomeCPPObject; SomeParam: integer);

    You get where I'm going at? It's a lot of work, though...

    Author Comment

    thanks Alex

    That is a neat trick. I get to create the C++ class once only and then call its methods via exported wrapper functions .. this will mean minimal rewrites at the C++ end.

    Pity about having to pass around that pointer though.

    hmm, what if I were SURE that the DLL would only ever get called once .. like if my app had a single instance restriction (via a mutex). Then could I not get the DLL to store the pointer in some global variable? (not quite sure how to do this in C, though it should be simple).  If that won't work, how about stopring it in an MMF .. that seems like overkill though.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now