Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi thiscall calling convention

Tags:

c++

delphi

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.

like image 657
ChrisB Avatar asked Aug 15 '15 17:08

ChrisB


People also ask

What is Stdcall calling convention?

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl . Functions that use this calling convention require a function prototype. The __stdcall modifier is Microsoft-specific.

What is Winapi calling convention?

A calling convention is a scheme for how functions receive parameters from their caller and how they return a result. The calling conventions can differ in where parameters and return values are placed (in registers; on the call stack; a mix of both), the order they are placed.

What calling convention does GCC use?

The calling convention for the gcc compiler on Linux systems should be to pass arguments via the stack. Here I assume the arguments are passed using eax and ebx .

What is the purpose of calling convention?

A calling convention governs how functions on a particular architecture and operating system interact. This includes rules about includes how function arguments are placed, where return values go, what registers functions may use, how they may allocate local variables, and so forth.


1 Answers

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.

like image 96
David Heffernan Avatar answered Sep 28 '22 00:09

David Heffernan