Mutley2003
asked on
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 ?
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 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.