Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

DLL VBA and VB.net -- protected memory error

kirbychapman
Beginner
1,211 Views
I have written a DLL in Intel Visual Fortran that does numerous calculations. Part of the code follows below from the Fortran DLL. I then call this in Excel 2007 using the Declare Function FTOK_F Lib "C:\\xxx" (Arg as double) as double. I then write a macro in VBA:

[vb]Public Function FtoK(T As Double) As Double
    FtoK = FTOK_F(T)
End Function
[/vb]

I then use this as a function in Excel, i.e., =FtoK(50).

This works fine in Excel on my machine. When i transfer the DLL to another machine, the DLL does not work (the macro in Excel just stops and i get the #Value in the cell that calls it.

Furthermore, if i include this in a vb.net (2008) project using the exact same method as i did in VBA, i receive an exception that states: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I am confused on why the macro seems to work fine within Excel on my machine, but then does not work within vb.net. I figure once i get that working, then i will focus on the "other machine" issue.

The Fortran DLL code follows (note: there is much more to this code, but this is the specific function i tried to get to work in vb.net -- they all work in VBA):

[fxfortran]      module ConversionFunctions
         implicit NONE
      CONTAINS  
      real(8) function FtoR_fnc(Arg)
      !dec$ attributes dllexport :: FtoR_fnc
      !dec$ attributes stdcall, REFERENCE, alias :'FTOR_F' :: FtoR_fnc
       implicit none
       real(8) Arg
      FtoR_fnc	= Arg + 459.67 
      return
      end function FtoR_fnc
      real(8) function RtoK_fnc(Arg)      
      !dec$ attributes dllexport :: RtoK_fnc       
      !dec$ attributes stdcall, REFERENCE, alias :'RTOK_F' :: RtoK_fnc
      implicit none       
      real(8) Arg       
      RtoK_fnc	= (Arg)/1.8       
      return       
      end function RtoK_fnc        
      real(8) function FtoK_fnc(Arg)       
      !dec$ attributes dllexport :: FtoK_fnc       
      !dec$ attributes stdcall, REFERENCE, alias :'FTOK_F' :: FtoK_fnc
      implicit none 
      real(8) Arg       
      FtoK_fnc =  RtoK_fnc(FtoR_fnc(Arg))       
      return       
      end function FtoK_fnc
      END MODULE ConversionFunctions
[/fxfortran]
0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,211 Views
You may also need to install the Microsoft Visual C++ Redistributables. Or, and this may be simpler, change the Libraries > Use Run-Time Library property to "Multithreaded" (and not Multithreaded DLL). Be sure you are building a Release configuration.

I'll get the "containscontains" issue fixed.

View solution in original post

0 Kudos
3 Replies
kirbychapman
Beginner
1,211 Views
When i posted this, i noticed in the fortran code that the key word CONTAINS is listed twice. I am not sure why this is the case, but it is not in the actual code.
0 Kudos
kirbychapman
Beginner
1,211 Views
Just figured out part of my problem. vb.net defaults to passing arguments ByVal. Changed the Declare statement to (ByRef Arg as double) and everything worked fine.

Still have not figured out how to transport the DLL to a different computer. I did install the Fortran redistributables to the target machine.

Help is greatly appreciated!
0 Kudos
Steven_L_Intel1
Employee
1,212 Views
You may also need to install the Microsoft Visual C++ Redistributables. Or, and this may be simpler, change the Libraries > Use Run-Time Library property to "Multithreaded" (and not Multithreaded DLL). Be sure you are building a Release configuration.

I'll get the "containscontains" issue fixed.
0 Kudos
Reply