0

Say I have two Delphi methods:

// Delphi
procedure FuncNoParam();
procedure FuncWithParam(myInt: Integer);

These need to be provided to a C++ DLL. The DLL will then call these methods when some event occurs:

// C++
int event = GetLatestEvent();
if (event <= 0)
  funcNoParam();
else if (event < 10)
  funcWithParam(event);
else
  DoSomethingElse();

How do I package up the Delphi methods, pass them to the C++ DLL and save them for future use like a CALLBACK?

UPDATE

Some comments suggested this question was a duplicate. There is some overlap, but that question does not address two aspects:

  • an example of how to define the above, less complex, methods which are void in C++
  • how to save the function in the C++ DLL for later use
AlainD
  • 5,413
  • 6
  • 45
  • 99
  • Should be pretty simple to define a C++ function pointer type for both of these signatures. If you can do the complex, as shown in the dupe, the simpler versions here should be easier. – David Heffernan Jun 26 '19 at 18:57
  • `procedure FuncNoParam(); procedure FuncWithParam(myInt: Integer);` are using Delphi's default `register` calling convention, which is not portable to C++ (except for C++Builder, which has `__fastcall` for compatibility with Delphi's `register`). Use `cdecl` or `stdcall` instead. Try this: (Delphi side) `procedure FuncNoParam(); cdecl; procedure FuncWithParam(myInt: Integer); cdecl;` (C++ side) `void (*funcNoParam)(); void (*funcWithParam)(int);` Then you can simply assign the addresses of the two Delphi functions as-is to the two C++ variables, and then call them like any other function. – Remy Lebeau Jun 26 '19 at 21:57

0 Answers0