Software Archive
Read-only legacy content
17061 Discussions

Re: Callback routine in mixed language programming

Jugoslav_Dujic
Valued Contributor II
191 Views
IMO the simplest mechanism is to pass address of a Delphi callback function into a Dll. The callback need not be in Delphi DLL, but you must follow guidelines for mixed-language programming. STDCALL calling convention is kinda standard for mixed-language stuff, so your code should look something like:

 
SUBROUTINE MyFortranDll(..., fnCallback) 
!DEC$ATTRIBUTES DLLEXPORT, STDCALL, ALIAS: "_MyFortranDll@XXX":: MyFortranDll 
 
INTERFACE 
   SUBROUTINE fnCallback(n) 
   !DEC$ATTRIBUTES STDCALL:: fnCallback 
   INTEGER n 
   END SUBROUTINE 
END INTERFACE 
 
DO i=1, large_computation 
   ... 
   CALL fnCallback(i) 
END DO 
... 
!---8<--Callback in Delphi: ----------- 
 
procedure DllProgressBar( nProgress: integer); stdcall; 
... 
{do something with progress indicator here} 
... 
end; 
 
!---8<--Delphi's calling routine: ----------- 
 
procedure MyFortranDll(... fnCallback: pointer); stdcall; external "MyDll.dll"; 
... 
MyFortranDll(... @DllProgressBar); 


I don't speak Delphi best, so I might have made some syntax error,
but I hope you get the idea.

One final remark: if you have to call fnCallback from somewhere deep in the dll's calling tree, it's sufficient that you pass fnCallback through all arg-list declared as EXTERNAL. Only in the routine that actually calls fnCallback you'll need full INTERFACE.

HTH

Jugoslav
0 Kudos
0 Replies
Reply