Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

12/04/13

Delphi calling Fortran calling Delphi | Intel Developer Zone

App Development

Intel Platform Development

Intel Technologies

Business Resources

What can we help you find today

Login or Register

Delphi calling Fortran calling Delphi

Tue, 02/06/2007 dirkjan.eertink@getronics.com 14:10

The Delphi-program (see below) calls a procedure DELPHICALLSFORTRAN(testfunc) which resides in FtnLib3.DLL, compiled with Intel Visual Fortran 9.1.033 (IVF) The parameter "testfunc" is a pointer and Delphi 'connects' that pointer to a Delphi-procedure "testfunc". On its turn, the Fortran-procedure DELPHICALLSFORTRAN "calls back" this function.

With Compaq Visual Fortran 6.5 (CVF) this went fine. With IVF this produces an exception error: 'the instruction "0x00403102 referenced memory at "0x011e2c20". The memory could not be "read"' When the line "call DelphiLijn" (the "call-back") is left out, it runs perfect under both CVF and IVF.

Settings in Project / FtnLib3 properties... / Fortran / External Procedures: calling convention = STCALL, REFERENCE changing this into "C, REFERENCE" (both in Fortran and in Delphi) does not help.

What's wrong ?

Dirk Jan Eertink

======== the Fortran-code ================

! FtnLib3.f90 ! subroutine DELPHICALLSFORTRAN(testfunc)

English
!DEC$ ATTRIBUTES STDCALL, DLLEXPORT::DELPHICALLSFORTRAN !DEC$ ATTRIBUTES ALIAS :'DELPHICALLSFORTRAN' :: DELPHICALLSFORTRAN !DEC$ ATTRIBUTES REFERENCE :: testfunc

pointer (testfunc, DelphiLijn)

character thistime*8, thisdate*10

call date(thisdate) call time(thistime) open(6,file='testfile.txt',status='unknown',form='formatted') write(6,'(4A)') ' Fortran (called by Delphi) made this file on ', thisdate, ' at ', thistime close(6) call DelphiLijn stop

end subroutine DELPHICALLSFORTRAN

======== the Delphi-code ===============

software.intel.com/en-us/forums/topic/276241

1/3

12/04/13
unit delfiunit;

Delphi calling Fortran calling Delphi | Intel Developer Zone

{ This program tests Delphi-programm calling a DLL, written in Fortran and compiled with the Intel Visual Fortran 9.1.033 compiler. This DLL contains a "call back": Delphi tells the Fortran-DLL the adress of the Delphi-function "testfunc" and then the Fortran-DLL "calls back" this Delphi-function.

This is working perfect when the Fortran part is compiled with the Compaq Visual Fortran Compiler 6.5. Compiling with the Intel Visual Fortran compiler 0.1.033 produces no compilation messages, but running the Delphi-program produces a nasty error. }

interface

uses // windows, messages, variants, // classes, graphics, controls, stdctrls, sysutils, forms, dialogs;

face="Courier New">type tform1 = class(tform) end;

var form1: tform1;

implementation

{$R *.dfm}

// declaration of Fortran-routine: procedure DelphiCallsFortran(var testfunc: pointer); stdcall; external 'ftnlib3.dll' name 'DELPHICALLSFORTRAN';

procedure testfunc; stdcall; // this procedure will be called by the fortran-dll var f: textFile; begin showmessage(' success !'); end;

var testfuncPointer : pointer; begin // tell the Fortran-routine "DELPHICALLSFORTRAN" the adress of // the Delphi-routine "test": testfuncPointer := @testfunc; showmessage(' try to call ...'); DelphiCallsFortran(testfuncPointer);

end.

=====================================================

Top RSS

4 posts / 0 new

Last post

software.intel.com/en-us/forums/topic/276241

2/3

12/04/13

Delphi calling Fortran calling Delphi | Intel Developer Zone


For more complete information about compiler optimizations, see our Optimization Notice.

to post comments

Steve Lionel (Intel)

Tue, 02/06/2007 14:48

to post comments

Try removing the line:

!DEC$ ATTRIBUTES REFERENCE :: testfunc

Steve
Top

Wed, 02/07/2007 dirkjan.eertink@getronics.com 11:35

to post comments

That's it ! So, by value, not by reference: I needed also to adapt the Fortran-settings from "STDCALL, REFENCE" to "STDCALL" and in the Delphi-source in "Procedure DELPHICALLSFORTRAN(var testfunc: stdcall)" to remove the "var " ("var" = by reference). And it works. Now I'm going to test if this works also in de rather complicated "real world"/ Thanks a lot.

Top

Steve Lionel (Intel)

Wed, 02/07/2007 14:07

to post comments

Typically, when a procedure is passed, the address of the procedure is passed by value. (You can think of this as the procedure is passed by reference.) When you use the passed argument as an address, then you need to make sure that it is accepted as by value.

If you are passing other types of arguments which you're going to modify, then those have to be passed by reference.

DELPHI, like pretty much all of the "interpreted" languages on Windows, uses STDCALL as the default calling convention. How individual arguments are passed depends on how you declare them.

Steve
Top

Back to original post

Intel DZ Content Content Library Code and Downloads Forums Blog Videos

Software Resources Intel Black Belt Software Developer Program Intel Software Partner Membership Intel Software Development Products

Publications Intel Software Adrenaline Magazine


(http://software.intel.com/sites/billboar d/isa-magazine)

Intel Software Subsidiaries McAfee (http://www.mcafee.com/) Havok (http://www.havok.com/) Wind River


(http://www.windriver.com/)

Intel Parallel Universe Magazine Intel Developer Zone Newsletter Contact Intel DZ

Intel Corporation (http://intel.com/)

FAQs Support Terms of Use (http://www.intel.com/content/www/us/en/legal/terms-ofuse.html) *Trademarks (http://www.intel.com/content/www/us/en/legal/trademarks.html)

Privacy (http://www.intel.com/content/www/us/en/privacy/intel-online-privacy-notice-summary.html) Cookies (http://www.intel.com/content/www/us/en/privacy/intel-cookie-notice.html)

software.intel.com/en-us/forums/topic/276241

3/3

You might also like