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 ?


Mutley2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.