从 Fortran 到 C# 的 PInvoke
我想从 Fortran 编译库导入一个函数,我有权访问的函数形式的签名在 c 中:
typedef void (__stdcall *fp_SUBLTdllTYPE)(double &,double *,double &,long &,char*,long );
我的 C# 代码如下:
//(long &,char*,char*,char*,long &,char*,long ,long ,long ,long );
[DllImport(@"C:\Program Files\REFPROP\refprop.dll",
CallingConvention=CallingConvention.StdCall,
CharSet = CharSet.Auto,
EntryPoint = "SETUPdll")
]
public static extern void Setup([In] long nc,[In]
[MarshalAs(UnmanagedType.LPStr)] StringBuilder hfiles,
[In] [MarshalAs(UnmanagedType.LPStr)] StringBuilder hfmix,
[In] [MarshalAs(UnmanagedType.LPStr)] StringBuilder hrf,
[In,Out] long ierr, [Out] [MarshalAs(UnmanagedType.LPStr)] StringBuilder herr,long l1, long l2, long l3,long l4);
而 Fortran 定义是:
subroutine SETUP (nc,hfiles,hfmix,hrf,ierr,herr)
implicit double precision (a-h,o-z)
implicit integer (i-k,m,n)
implicit logical (l)
c
cDEC$ ATTRIBUTES DLLEXPORT :: SETUP
c dll_export SETUP
c
parameter (ncmax=20) !max number of components in mixture
parameter (nrefmx=10) !max number of fluids for transport ECS
parameter (n0=-ncmax-nrefmx,nx=ncmax)
parameter (nrf0=n0) !lower limit for transport ref fluid arrays
parameter (nrefluids=4) ! numb
问题是我无权访问Fortran编译器和我对Fortran的了解几乎为零。
当我从 C# 代码调用该函数时:
long ierr=0;
long i = 2;
StringBuilder herr=new StringBuilder("");
Setup(i, new StringBuilder("R410a.mix"), new StringBuilder("hmx.bnc"), new StringBuilder("DEF"), ierr, herr, refpropcharlength * ncmax, refpropcharlength,
lengthofreference, errormessagelength);
出现以下错误:
尝试读取或写入受保护的内存。这通常是一个 表明其他内存已损坏。
谁能帮助我吗?
I want to import a function from a Fortran compiled library the signature of the function form for that I have access is in c:
typedef void (__stdcall *fp_SUBLTdllTYPE)(double &,double *,double &,long &,char*,long );
My C# code is as follows:
//(long &,char*,char*,char*,long &,char*,long ,long ,long ,long );
[DllImport(@"C:\Program Files\REFPROP\refprop.dll",
CallingConvention=CallingConvention.StdCall,
CharSet = CharSet.Auto,
EntryPoint = "SETUPdll")
]
public static extern void Setup([In] long nc,[In]
[MarshalAs(UnmanagedType.LPStr)] StringBuilder hfiles,
[In] [MarshalAs(UnmanagedType.LPStr)] StringBuilder hfmix,
[In] [MarshalAs(UnmanagedType.LPStr)] StringBuilder hrf,
[In,Out] long ierr, [Out] [MarshalAs(UnmanagedType.LPStr)] StringBuilder herr,long l1, long l2, long l3,long l4);
and the fortran definition is:
subroutine SETUP (nc,hfiles,hfmix,hrf,ierr,herr)
implicit double precision (a-h,o-z)
implicit integer (i-k,m,n)
implicit logical (l)
c
cDEC$ ATTRIBUTES DLLEXPORT :: SETUP
c dll_export SETUP
c
parameter (ncmax=20) !max number of components in mixture
parameter (nrefmx=10) !max number of fluids for transport ECS
parameter (n0=-ncmax-nrefmx,nx=ncmax)
parameter (nrf0=n0) !lower limit for transport ref fluid arrays
parameter (nrefluids=4) ! numb
the problem is that I do not have access to a Fortran compiller and my knolege of fortran is almost zero.
When I call the function from C# code:
long ierr=0;
long i = 2;
StringBuilder herr=new StringBuilder("");
Setup(i, new StringBuilder("R410a.mix"), new StringBuilder("hmx.bnc"), new StringBuilder("DEF"), ierr, herr, refpropcharlength * ncmax, refpropcharlength,
lengthofreference, errormessagelength);
I get the following error:
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您需要字符串时传递 StringBuilder 对象似乎是不正确的。作为起点,我会尝试传递 herr.ToString() 或仅构建字符串而不是 StringBuilder 之类的事情。
It seems incorrect to pass StringBuilder objects when you want a string. As a starting point I would try things like passing herr.ToString() or just build Strings to begin with instead of StringBuilder(s).