Results 1 to 5 of 5

Thread: Mixing C++ and Delphi without wrappers

  1. #1

    Mixing C++ and Delphi without wrappers

    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!)

    Tested in Windows, OSX and Linux, it works fine with all of them.
    Any questions, just ask! - Crossplatform 3D game engine

  2. #2
    very nice and thanks for the trick , it's very helpful . i've seen the same technique used in irrdelphi which uses irrlicht.dll without "C" wrappers .

    btw : does your terra-engine compiles fine with fpc ? , i tried last time but faced alots of problems .

  3. #3
    Oh, I need to check irrdelphi then, I'm interested to see how they did it, probably same then!

    And yes, it compiles fine on latest FPC, I use it frequently to compile my games for all platforms except Windows (where I stil use the old Delphi 7).
    And soon I'm adding support for Oxygene and Pascal Studio, however that still requires some big refactoring of the engine to work around the diferences/limitations of those 2 compilers

    PS: Just checked irrdelphi, and it uses a different approach (more complete and complex). It seems to be able to emulate a c++ class in a more generic way, really interesting and clever, thanks for sharing!
    Last edited by Relfos; 07-07-2015 at 11:48 AM. - Crossplatform 3D game engine

  4. #4
    I have seen that technique few weeks ago from this link:

    But i got lost from "Virtual Method Table Reconstruction" technique, so i give up and went to the old way to coding a C dll wrapper.

    I really wish to use C++ class into my delphi app, with not need to wrapp every property and methods first in the C source and then in the pascal side.

    Last edited by Bpascal; 07-07-2015 at 09:41 PM.

  5. #5
    Thanks for that link too, that is more about doing opposite though, calling C++ classes from Delphi, instead of letting C++ call Delphi code.
    Writing that kind of stuff manually for big APIs is lots of work though, ideally it should be done either via a preprocessing tool or even better, if someone could add direct support for that in FPC... - Crossplatform 3D game engine


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts