本文介绍了从VBA而不是从VB.NET调用时,64位DLL可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

Hi!

我想将我的32位VB-Fortran代码移植到64位平台,包括从以下位置调用fortran DLL: VBA (即Excel) and VB.NET .在32位环境下可以正常工作.有人知道为什么在64位Windows中VBA可以工作,但VB.NET崩溃吗?我在做什么错了?

I want to port my 32bit VB-Fortran code to the 64bit platform, consisting in calling a fortran DLL both fromVBA (ie: Excel) andVB.NET. This works fine in 32bit. Does anybody has an idea why in 64bit windows VBA works, but VB.NET crashes? What am I doing wrong?

VB.NET代码
   私有声明子检查1库"c:\ temp \ polar.dll"; (ByRef s1为Single,ByRef i1为Integer)
   私有声明子检查0 Lib"c:\ temp \ polar.dll"; (ByRef d1为Double,ByRef i1为整数)

VB.NET code
    Private Declare Sub check1 Lib "c:\temp\polar.dll" (ByRef s1 as Single, ByRef i1 As Integer)
    Private Declare Sub check0 Lib "c:\temp\polar.dll" (ByRef d1 As Double, ByRef i1 As Integer)

        Dim aa为单数,ii为整数:aa = 0.5:ii = 6
       致电check1(aa,ii)'正常运行
        MsgBox("aa ="& aa.ToString&"ii ="& ii.ToString)

        Dim aa As Single, ii As Integer : aa = 0.5 : ii = 6
        Call check1(aa, ii) 'works OK
        MsgBox("aa=" & aa.ToString & " ii=" & ii.ToString)

       将dm bb(0至1000-1)设为Double,将jj(0至1000-1)设为整数
       呼叫check0(bb(0),jj(0))'崩溃!!!
        MsgBox("bb(0)="& bb(0).ToString&"jj(0)="& jj(0).ToString)

        Dim bb(0 To 1000 - 1) As Double, jj(0 To 1000 - 1) As Integer
        Call check0(bb(0), jj(0)) 'crashes !!!
        MsgBox("bb(0)=" & bb(0).ToString & " jj(0)=" & jj(0).ToString)

VBA代码
       私有声明PtrSafe Sub check1 Lib"c:\ temp \ polar.dll"; (s1为单身,i1为长)
       私有声明PtrSafe Sub check0 Lib"c:\ temp \ polar.dll"; (d1为Double,i1为Long)

VBA code
        Private Declare PtrSafe Sub check1 Lib "c:\temp\polar.dll" (s1 As Single, i1 As Long)
        Private Declare PtrSafe Sub check0 Lib "c:\temp\polar.dll" (d1 As Double, i1 As Long)

       昏暗aa为单,ii为长:aa = 0.5:ii = 6
       致电check1(aa,ii)'正常运行
        MsgBox"aa =" & Str $(aa)& " ii =" & Str $(ii)

        Dim aa As Single, ii As Long: aa = 0.5: ii = 6
        Call check1(aa, ii) 'works OK
        MsgBox "aa=" & Str$(aa) & " ii=" & Str$(ii)

        Dim bb(0到1000-1)为Double,jj(0到1000-1)为Long
       呼叫check0(bb(0),jj(0)) '可以正常使用
        MsgBox" bb(0)=" & Str $(bb(0))& " jj(0)="" & Str $(jj(0))

        Dim bb(0 To 1000 - 1) As Double, jj(0 To 1000 - 1) As Long
        Call check0(bb(0), jj(0)) 'works OK
        MsgBox "bb(0)=" & Str$(bb(0)) & " jj(0)=" & Str$(jj(0))

如果您了解fortran语言,则以下是相关文件.但是,我重复一遍,从VBA7调用时64位DLL可以正常工作,但VB.NET崩溃. Fortran编译 (32位和64位)都以相同的方式完成:  ifort -dll -static polar.for

In case you understand fortran language, below is the file in question. However, I repeat that the 64bit DLL works fine when called from VBA7 but VB.NET crashes. Fortran compilations (both 32bit and 64bit) have been done in the same way: ifort -dll -static polar.for 

FORTRAN代码
      SUBROUTINE check1(a,n)
!dec $属性dllexport,stdcall,reference,alias:"check1"; :: check1
      REAL * 4 a
     整数* 4 n
      a = a + 1.0
      n = n-1
     返回
     结束

FORTRAN code
      SUBROUTINE check1 (a,n)
!dec$ attributes dllexport,stdcall,reference,alias : "check1" :: check1
      REAL*4 a
      INTEGER*4 n
      a = a + 1.0
      n = n - 1
      RETURN
      END

      SUBROUTINE check0(a,n)
!dec $属性dllexport,stdcall,reference,alias:"check0&"; :: check0
      REAL * 8 a(0:999)
     整数* 4 n(0:999)
     一个= 3.14159
      n = -999
     返回
      END

      SUBROUTINE check0 (a,n)
!dec$ attributes dllexport,stdcall,reference,alias : "check0" :: check0
      REAL*8 a(0:999)
      INTEGER*4 n(0:999)
      a = 3.14159
      n = -999
      RETURN
      END

推荐答案

您可能会尝试使用DllImport表示法,在这种特殊情况下(VB.NET的FORTRAN),我使用了这种方式最为成功.

You might try to use DllImport notation, which I've had the most success with in this particular scenario (FORTRAN from VB.NET).

我还建议您尝试使用目标CUP x64重新编译VB Fortran dll.

I could also suggest that you could try to recompile you VB Fortran dll with target CUP x64.


这篇关于从VBA而不是从VB.NET调用时,64位DLL可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:35