当从VB6.0调用时,DLL中的Fortran`read(*,*)`会导致“运行时错误(39)”

时间:2017-02-11 14:35:41

标签: debugging dll vb6 fortran runtime-error

VB6.0中的代码是:

Private Declare Sub suba Lib "D:\try_vb\c_dll\Dll1\Dll1\Debug\Dll1.dll" (ByRef a As Single, ByRef b As Single, ByRef c As Single)
Private Sub Command1_Click()
    Dim a As Single
    Dim b As Single
    Dim c As Single
    a = 10#
    b = 2#
    Call suba(a, b, c)
    Text1.Text = c
End Sub

Fortran DLL中的代码是:

subroutine suba(a,b,c)
!DEC$ ATTRIBUTES stdcall,DLLEXPORT ::suba
!DEC$ ATTRIBUTES ALIAS:"suba"::suba
!DEC$ ATTRIBUTES reference :: a,b,c
    implicit none
    real::a
    real::b
    real::c
    real::aaa

    c=a+b
    read(*,*)aaa       !This sentence seems wrong
end subroutine

如果行read(*,*)aaa不存在,程序可以返回正确答案c=12,如下所示: Fig 1

但是当read(*,*)aaa存在时,会发生错误,即

forrtl:severe(39):error during read,unit -4,file CONIN$

forrtl:severe(39):error during read,unit -4,file CONIN$

我该怎么办?

2 个答案:

答案 0 :(得分:2)

这很简单 - Fortran代码试图从控制台读取,而VB应用程序中没有。通常,您应该使用一种语言进行所有I / O操作。您可以在执行Fortran中的READ之前调用Windows API例程AllocConsole来创建控制台,但对于具有图形界面的VB应用程序来说这似乎很愚蠢。

答案 1 :(得分:0)

最合适的方法是使用一种语言进行所有I / O(如帮助者所说)。

Howerver,作为临时解决方案,我找到了函数" AllocConsole()"可以避免错误,因为可以设置控制台窗口。