9

I need to call non-static C++ member functions

Hence i need to use the thiscall calling convention.

Delphi doesn't support this calling convention.

So before i can call any of the member functions i have to manually push the parameters in the stack.

  asm
    mov ecx, myClassPointer
  end;

This works fine but i am looking for a better way to do this.

At the moment i am calling the asm code everytime with the classpointer before calling the function, which isn't nice.

As i am no Delphi expert i am wondering if you can declare your own calling convention or automate the calling of those thiscall class methods.

1
  • 2
    I wrote some thiscall adaptors for Microsoft's rich-edit interfaces, which incorrectly omitted the calling conventions. My adaptors wrap interfaces in both directions, so Delphi code can call Microsoft's code, and Microsoft's code can call back to a Delphi interface, and each side thinks the other is providing the expected calling convention. Maybe you'd find it useful. Aug 16, 2015 at 4:07

3 Answers 3

5

You have the following options:

  • Write adapters in asm to call the function. This is your current solution, and as you know the approach is brittle and awkward.
  • Write a C++ adapter that presents an interop friendly interface.

The latter option is, in my view, the correct solution. The C++ code is not suitable for interop. Exposing C++ classes through interop is simply wrong. It places unreasonable demands on the consumer. Reasonable approaches include COM and plain C style interop as found in Win32.

Write an adapting C++ DLL that consumes the unreasonable C++ classes and exposes a proper interop friendly interface. The adapter is written in C++ and so is capable of consuming the C++ code. But then it exports an interop friendly variant of the interface that can be readily called from any tool chain.

3
  • Could you please elaborate about the adapters part. I've never heard about that in termins of delphi function calling. Basically it should call the asm code with the desired class pointer before calling the function / procedure itself.
    – ChrisB
    Aug 15, 2015 at 18:18
  • No the adapter is a layer between the original thiscall C++ and your Delphi code. It can call the thiscall C++, and it re-exports it as a proper interop safe interface. Aug 15, 2015 at 18:46
  • 1
    See the article I mentioned in my answer. It describes the adapter code extensively. Aug 16, 2015 at 14:01
4

Delphi's register calling convention uses registers for the first three parameters. The first two are EAX and EDX, which are unused in the thiscall calling convention and can be set to anything you like. The third of those registers is ECX.

thiscall passes all arguments on the stack, and register passes remaining arguments on the stack. Both register and thiscall have the callee clean up arguments passed on the stack.

So what you can do is declare a register function, with dummy parameters for EAX and EDX. You can still wrap this in a different function to make it easier to call, but at least now that wrapper function can be declared inline.

Note though that the order in which arguments get pushed on the stack is different between thiscall and register, so you need to reverse parameters to match.

3

As David already said, you can go on using assembler, but there are better ways. I wrote an article called "Using C++ Objects in Delphi" that decribes extensively, with code, how you can use both viable alternatives:

  • Write a C layer that exports C-style functions that simply use the C++ class and expose each of the methods as simple functions. This is pretty simple, but a little awkward to use from the Delphi side.
  • Write a COM layer, which is not as easy as it sounds. I needed quite some help from a C++ guru to do it right. The article describes the steps and you can apply them 1 to 1 to your class(es).

I personally would prefer the COM-layer though. It is much easier to use from the Delphi side.

The article also describes a few of the problems you can encounter when writing such code or using such objects. Read all of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.