For many years I've always found situations where I needed to write Delphi code that could interface directly with C++ code.
Usually the old way was to use C to use as bridge between both languages, or use COM interfaces (yuck!), but now there is a new way that allows C++ code to create, destroy and call methods from a Delphi class.
Usually interfacing with the Valve Steamworks API requires C++ methods as callbacks (Yuck!), so it was necessary to develop this new tecnhnique.
I did not discover this trick alone, it was with help from another Pascal developer who I talked in the private Steam forums (sadly I don't know his name).
Mainly, there 2 big obstacles:
1 - C++ and Delphi classes are not binary compatible, so you can't just pass mix pointers to a Delphi class to a C++ class. This technique emulates C++ classes via Delphi records.
2 - C++ methods use 'thiscall' as calling convention, which does not even exist in Delphi (and other languages). The trick here is to use some proxy functions with a bit of assembly to dispatch the call to the right method.
A sample implementation of this technique can be seen in this Github project I've uploaded today.
It shows how to write Milkshape 3D plugins using only Delphi (Milkshape requires plugins to use C++, so before the only was to use some C-dll proxy, which was a bit ugly!)
https://github.com/Relfos/milkshape_plugins
Tested in Windows, OSX and Linux, it works fine with all of them.
Any questions, just ask!
Bookmarks