Fortran Dll导入

埃德加姆采

Robert L. Parker和Philip B. Stark在Fortran中有一个代码

福特兰

      subroutine bv(key, m, n, a, b, bl, bu, x, w, act, zz, istate,  loopA)
      implicit double precision (a-h, o-z)
!      x  is an unknown n-vector
!      a  is a given m by n matrix
!      b  is a given  m-vector 
!      bl is a given n-vector of lower bounds on the components of x.
!      bu is a given n-vector of upper bounds on the components of x.
!      key = 0
!   ---Output parameters:
!      x   the solution vector.
!      w(1)    the minimum 2-norm || a.x-b ||.
!  istate  vector indicating which components of  x  are active 
!  loopA   number of iterations taken in the main loop, Loop A.
!  ---Working  arrays:
!  w      dimension n.               act      dimension m*(mm+2). mm=min(m,n).
!  zz     dimension m.               istate   dimension n+1.

我试图从C#中的dll调用该函数,例如:

C#

class Program
{
    [DllImport("bv.dll", CallingConvention = CallingConvention.StdCall )]
    public static extern void bvls(
         int key, //key = 0, the subroutine solves the problem from scratch. If key > 0 the routine initializes using the user's guess about which components of  x  are `active'
         int m,
         int n,
        double[] a, //  m by n matrix
        double[] b, //  m-vector 
        double[] bl, //n-vector of lower bounds on the components of x.
        double[] bu, //n-vector of upper bounds on the components of x.
        ref double[] x, //unknown n-vector
        //Working  arrays:
        ref double[] w,  //dimension n
        double[] act, //dimension m*(mm+2). mm=min(m,n).
         double[] zz, //dimension m
        ref double[] istate, //dimension n+1.
        ref int loopA //   number of iterations taken in the main loop, Loop A.
        );
    
       
    static void Main(string[] args)
    {
        double[] a = new double[3 * 3] { //M*N
                  1.0, 10.0, 10.0,
                  2.0, 18.0, 16.0,
                  1.8, 69.0, 16.0
        };
        double[] b = new double[3]  {  //LDB*NRHS
             4.3, 6.8, 1.0,
        };
        double[] bl = new double[3];
        double[] bu = new double[3];
        double[] x = new double[3];
        double[] w = new double[3];
        double[] act = new double[3* 5];  //dimension m*(mm+2). mm=min(m,n).
        double[] zz = new double[3];
        double[] istate = new double[4];
        int loopA =0;
        Program.bv(0, 3, 3, a, b, bl, bu, ref x, ref w, act, zz, ref istate, ref loopA);
        for (int j = 0; j < 3; j++)
            Console.Write(" \t" + x[j]);
            
    }
}

但是在执行代码时我得到

EntryPointNotFoundException: Entry point was not found. in 'bv' on file 'bv.dll'.
    myProject.Program.bv(Int32 key, Int32 m, Int32 n, Double[] a, Double[] b, Double[] bl, Double[] bu, Double[]& x, Double[]& w, Double[] act, Double[] zz, Double[]& istate, Int32& loopA)

基本上我有两个问题,如何使它起作用?和其他问题,它是否正确我定义函数的方式

 [DllImport("bv.dll", CallingConvention = CallingConvention.StdCall )]
            public static extern void bvls(...)

基于fortran代码的常规信息?

使用依赖行者时,我得到:在此处输入图片说明

我怀疑dll不正确并且没有例程,是否可以检查dll是否以正确的方式生成?

更新

在尝试了ILSpy之后,我得到了以下提示似乎不正确,您能否建议如何正确生成dll文件?,ILSpy是否告诉bvlsFortran是我应使用的函数?我尝试过,但无法正常工作在此处输入图片说明

在此处输入图片说明

约翰·阿列克修

FORTRAN以下尝试

MODULE CALCBV

    INTEGER, PARAMETER :: sp = SELECTED_REAL_KIND(p=6,r=37)     ! IEEE Single Precision (32-bit)
    INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(p=15,r=307)   ! IEEE Double Precision (64-bit)       

CONTAINS

    subroutine bv(key, m, n, a, b, bl, bu, x, w, act, zz, istate,  loopA)
    IMPLICIT NONE
    !DEC$ ATTRIBUTES DLLEXPORT :: bv
    !DEC$ ATTRIBUTES ALIAS:'BV' :: bv
    !DEC$ ATTRIBUTES VALUE :: key, m, n

    INTEGER, INTENT(IN)   :: key, m, n
    REAL(dp), INTENT(IN)  :: a(m,n), b(m), bl(n), bu(n)
    REAL(dp), INTENT(OUT) :: x(n), w(n)
    REAL(dp), INTENT(IN)  :: act(m,MIN(m,n)+2), zz(m)
    INTEGER, INTENT(OUT)  :: istate(n+1)
    INTEGER, INTENT(OUT)  :: loopA

    ! DO CALC HERE

    end subroutine

END MODULE

然后使用以下命令调用它C#

[DllImport("bv.dll", CallingConvention=CallingConvention.Cdecl, EntryPoint="BV")]
static extern void bvls(
    int key, //key = 0, the subroutine solves the problem from scratch. If key > 0 the routine initializes using the user's guess about which components of  x  are `active'
    int m,
    int n,
    double[] a, //  m by n matrix
    double[] b, //  m-vector 
    double[] bl, //n-vector of lower bounds on the components of x.
    double[] bu, //n-vector of upper bounds on the components of x.
    double[] x, //unknown n-vector
    //Working  arrays:
    double[] w,  //dimension n
    double[] act, //dimension m*(mm+2). mm=min(m,n).
    double[] zz, //dimension m
    int[] istate, //dimension n+1.
    ref int loopA //   number of iterations taken in the main loop, Loop A.
    );

// Test code
static void BVTEST()
{
    int key=0, n=2, m=3;
    double[] a= { 1.0, 2.0, 3.0, 4.0, 5.0 };
    double[] b= { 10.0, 20.0, 30.0 };
    double[] bl= { 0.0, 1.0 };
    double[] bu= { 1.0, 2.0 };
    double[] x=new double[n];
    double[] w=new double[n];
    double[] act=new double[m*Math.Min(m, n)+2];
    double[] zz=new double[m];
    int[] istate=new int[n+1];
    int loopA = 0;
    // Call Fortran .dll
    bvls(key, m, n, a, b, bl, bu, x, w, act, zz, istate, ref loopA);
}

屏幕截图

请记住,数组已经是引用类型(默认值),因此它们不需要ref关键字。输出值需要像loopA,而是由值参数传递的需要VALUE,以避免将它们与属性声明ref,像keymn您可能需要将其大小固定为act更大,因为此后我在参数中遇到了一些内存损坏。

此发布应该使您朝正确的方向前进。请记住,始终Cdecl与FORTRAN .dll一起使用,并始终使用implicit none声明。x86Win32和一起编译,不要使用AnyCPU声明您的出口与ALIAS服装,以显示出来。

DependecyWalker

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Fortran编译的和C编译的DLL是否应该可以互换导入?(x86目标)

来自分类Dev

Fortran编译的和C编译的DLL是否应该可以互换导入?(x86目标)

来自分类Dev

将fortran文件导入python

来自分类Dev

将fortran文件导入到python

来自分类Dev

OpenMP:将fortran指针导入PRIVATE()

来自分类Dev

在R中导入FORTRAN数据的问题

来自分类Dev

将接口模块过程从 fortran 导入 C

来自分类Dev

minpack fortran .dll用于C#

来自分类Dev

Qt应用程序从Fortran调用Dll

来自分类Dev

MapleSim / Modelica的FORTRAN DLL调用约定

来自分类Dev

Qt应用程序从Fortran调用Dll

来自分类Dev

MapleSim / Modelica的FORTRAN DLL调用约定

来自分类Dev

DLL导入

来自分类Dev

在C ++ / FORTRAN中使用非托管C#-DLL

来自分类Dev

使用多变量函数时Fortran DLL错误

来自分类Dev

在C ++ / FORTRAN中使用非托管C#-DLL

来自分类Dev

从VBA调用Fortran 95 dll,其结构包含动态数组

来自分类Dev

将字符串从Fortran dll传递到C#

来自分类Dev

如何在 Fortran 中设置 dll 搜索路径?

来自分类Dev

如何导入libusb dll

来自分类Dev

如何从DLL导入Cmdlet

来自分类Dev

DLL导入/导出

来自分类Dev

如何导入libusb dll

来自分类Dev

如何从DLL导入Cmdlet

来自分类Dev

导入 .dll-s

来自分类Dev

将ctypes与由intel fortran编译器编译的fortran dll一起使用并链接到intel的mkl库时,缺少dll依赖项

来自分类Dev

DLL DLL中的原始函数的导入

来自分类Dev

通过FORTRAN(DLL)返回双精度数组以在python中进一步处理

来自分类Dev

Fortran77 write(0,*)DLL命令未刷新到R GUI控制台