• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1065
  • Last Modified:

Wrapping C++ classes in a DLL called from Delphi

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 www.cgal.org and http://www.magic-software.com/Approximation.html ).

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 ?


0
Mutley2003
Asked:
Mutley2003
1 Solution
 
Wim ten BrinkCommented:
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...
0
 
Mutley2003Author Commented:
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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now