请帮忙解决VB与FORTRAN混编时遇到的问题,困扰多天
目的:采用DLL动态链接库,实现VB和FORTRAN混合编程
计算机系统:
WIN xp_sp2
编程软件:
VB6.0_SP6(企业版),Fortran PowerStation4.0
程序算例:
1. Fortran程序(对一个数加100)
SUBROUTINE testsub(d,y)
! MS$ attributes alias:'testsub'::testsub
! MS$ attributes dllexport::testsub
REAL(4) d,y
y = d+100
END SUBROUTINE testsub
通过编译连接后,生成testsub.dll,装入指定文件夹下。
2. VB程序(调用FORTRAN程序)
Private Declare Sub testsub Lib "D:\Program\testsub.dll" (d As Single, y As Single)
Private Sub Command1_Click()
Dim d As Single
Dim y As Single
d = Val(Text1.Text)
Call testsub(d, y)
Text2.Text = y
End Sub
Private Sub Command2_Click()
End
End Sub
3. 运行VB程序
经调试多次,程序中始终出现“实时错误‘453’找不到DLL入口点testsub in D:\Program\testsub.dll”。
问题:
错误出在什么地方,是fortran程序有误,还是vb调用时出错,请帮忙调试,并指出应该怎样避免以上错误,请指教。 请参考
http://www.absoft.com/Products/Compilers/Fortran/Windows/IA32/vbasicdlls.html
http://support.microsoft.com/kb/123841
或者细读以下例子:
Creating The Fortran DLL
Write your FORTRAN source code in the usual manner, declaring the program unit as either a subroutine or a function. Insert the additional keyword STDCALL before each SUBROUTINE or FUNCTION keyword. Adhere to the normal FORTRAN CALL/RETURN sequences and argument passing rules. For example, consider the following subroutine
stdcall subroutine DegreeSin(input, output)
implicit none
double precision input
double precision output
output = dsind(input)
return
end
This subroutine simply computes the double precision sin in degrees of the input argument and returns the result in the output argument.
Compile the file normally, using the -c option which instructs the compiler to produce an object file only.
Next, create a new linker control file containing the names of the program units that will be placed in the DLL. This file should contain only those names in the library that will be externally visible. If the DLL contains any programs units that are used only by the routines within the DLL, they should not be placed in this file. In the case of the above example, we would create the file, "DegreeSin.xps", containing the single line:
DegreeSin
You must also create a second new linker control file containing the aliases for the internal names that the compiler generated for your subprogram names. The compiler creates internal names to avoid conflicts with other global symbolic names. It does this by prepending an underscore to the name and, in the case of STDCALL subprograms, appending the size of the call stack to the name. The size of the call stack will be equal to 4 times the argument list count. In the case of the DegreeSin subroutine, this value will be 8. This value is separated from the subprogram name with a commercial at sign (@) since the subprogram name may legally end with a number. In the case of the above example, we would create the file, "DegreeSin.als", containing the single line:
_DegreeSin@8 DegreeSin
which says that the internal name, _DegreeSin@8, will be known externally as DegreeSin.
To create the DLL, we would enter the following line on the MS-DOS Prompt command line:
lnk /dll DegreeSin.obj /exports:DegreeSin.xps frt0.lib ntf77dll.lib
kernel32.lib /aliases:DegreeSin.als
The three .lib file references allow the linker to resolve any runtime library routines that may needed. That is, your DLL may itself reference other DLLs.
Creating the Visual Basic code
Complete documentation about calling Fortran DLLs from Microsoft Visual Basic can be found in the Microsoft Visual Basic Programmer's Guide. Refer to Chapter 26, Calling Procedures in DLLs. This section will describe the basics of referencing a Fortran Subroutine or Function.
The first step is to declare the Fortran subprogram in your Basic program. The declaration for the subroutine discussed in the previous section would be:
Declare Sub DegreeSin Lib "c:\...\DegreeSin.dll" (inval As Double, _
outval As Double)
The string following the Lib keyword should be the path to the DLL.
The actual reference to the Fortran subroutine is simply:
Dim inval As Double
Dim outval As Double
.
.
.
Call DegreeSin(inval, outval)
页:
[1]