Call c++ exe program from Delphi DLL

Abdulatif AL ABDOU
Abdulatif AL ABDOU used Ask the Experts™
I have a program c++ which call a Delphi DLL; The Delphi DLL has to inform the program c++ by the result of his work several times during his lifecycle.

in yours opinions what are the possibles methods for doing this? without using files ou databases like a third part!

Thanks in advance.

I will list here what i think:

- First idea is by passing a pointer to delphi dll when launching it; but i don't know which types to use in the two sides and how Delphi treat pointers!

- Second idea is by using an intermediate c++ DLL; Delphi will call a function in the DLL C++ which will modify the values of the c++ dll variables. in the other side, when the c++ program launch the c++ dll, it will pass a pointer. When delphi call the c++ function, the function will fill the pointer passed by c++ program. Do you think that works?

I don't know if i was clear; Thank you
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

the usual way to do this is using a thread in the c++ app.

the thread was started by the c++ program and you pass a handle to global memory together with the windows handle of the main window to the thread (use a structure that contains 2 members of type HANDLE resp. HWND. the thread runs asynchronously to the main program and would call the dll functions. if it has results to report it would put them (copy them) into the global memory which can be accessed via the handle. then the main thread was notified by a window message that the results are ready to fetch. the main thread would read the global memory in a handler function that handles the private message and then shows or stores the results in the c++ program.

use WINAPI function GlobalAlloc to allocate global memory and get a handle which you can pass to the thread.  to access the memory you would use GlobalLock function.

messages between worker thread and main thread should be sent by PostMessage function (also WINAPI). PostMessage is thread-safe.

Abdulatif AL ABDOUIngénieur études et développement


Thanks Sara for your answer, I will try this, do you have an example?
Top Expert 2016

i can give you code snippets for c++ and windows but not for delphi and/or unix.

so, in my opinion you would need to either port the delphi dll to c++  or write a c++ wrapper dll that can call functions from delphi dll to exchange data from c++ to delphi and back.

Sinisa VukSoftware architect
Top Expert 2012

You can call c++ dll from Delphi and Delphi app can call c++ dll too.
When doing that make sure that use same calling convention (let be stdcall).
Using pointers is a must and works great.

Consume those articles first:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial