我有問題與delphi代碼。我想調用delphi中的函數來處理fortran函數,但我已轉移到DLL。這裏是代碼的Fortran如何從delphi中的Fortran DLL調用函數?
SUBROUTINE c_zsn(m,d,k,f,zsn,nf)
! Specify that the routine name is to be made available to callers of the
! DLL and that the external name should not have any prefix or suffix
!MS$ ATTRIBUTES DLLEXPORT :: c_zsn
!MS$ ATTRIBUTES ALIAS:'c_zsn' :: c_zsn
!MS$ ATTRIBUTES VALUE :: m,d,k,nf
!MS$ ATTRIBUTES REFERENCE :: f,zsn
IMPLICIT NONE
INTEGER :: nf,i
REAL(KIND(0.D0)) :: m,d,k,f(0:(nf-1)),zsn(0:(nf-1)),om,pi
COMPLEX(KIND(0.D0)) :: j
j = (0.d0, 1.d0)
pi = 4.d0 * datan(1.d0)
do i=0,nf-1
om = 2.d0*pi*f(i)
zsn(i) = abs(-om**2*m-j*om*d+k)
end do
END SUBROUTINE
,這裏是爲Delphi代碼,我用
procedure TForm1.Button2Click(Sender: TObject);
type tarray=array[0..10]of double;
var a:thandle;
fcn:function(s,d,f:double;var g,h:tarray;n:integer):double;
e,f,d,g,h,i,j:double;
k:tarray;
l,o:tarray;
n,m:integer;
begin
a:=LoadLibrary('dllsub.dll');
if (A=0) then
begin
Application.MessageBox('Failed to open library','Error', MB_OK or MB_ICONEXCLAMATION);
exit;
end;
@fcn:=GetProcAddress(a, 'c_zsn');
if @b=nil then
begin
ShowMessage('Failed to open function');
exit;
end;
e:=2;
f:=200;
d:=0.01;
n:=10;
for m:=0 to n do
l[m]:=m;
fcn(e,d,f,l,o,n); // this is the problem
FreeLibrary(a);
end;
我不能調用函數(粗體之一)。
C是DLL的「通用語言」。我認爲有很多教程可以爲你的Fortran文件製作C頭文件(* .h)。然後有很多教程如何將C頭文件翻譯成Pascal。 –
@ Arioch'The不需要將接口轉換爲C. –
@DavidHeffernan當然,直接翻譯總是可以比使用中間語「通用語法」更好的結果。但是後者有時候是「足夠好」的,並且還有更多的產品:-)這僅僅是某種意義上的「分而治之」的情況 –