使 Fortran 中的其他函数/模块可以访问外部函数
我是 Fortan 新手,有一个关于使用使回调函数可用于整个 Fortran 代码的问题。
我正在编写一个从 C# 访问 Fortran DLL 的接口。
module csWrapper
interface
subroutine vdiTestFuncCllBak(inputValue, retValue)
INTEGER, INTENT(IN) :: inputValue
INTEGER, INTENT(INOUT) :: retValue
end subroutine
end interface
procedure(vdiTestFuncCllBak), pointer :: m_vdiTestFuncCllBak
end module csWrapper
module VdiFunctionRunnerMain
use csWrapper
implicit none
contains
integer function VdiFunctionRunner (XTGA, ARRAY_810, vdiCwertCllbak, vdiIwertCllbak, vdiRwertCllbak, vdiCwert2Cllbak, vdiIwert2Cllbak, vdiRwert2Cllbak, vdiErsterCllBak, vdiLetzterCllBak, vdiTestFuncCllBak)
!DEC$ ATTRIBUTES DLLEXPORT ::VdiFunctionRunner
!DEC$ ATTRIBUTES REFERENCE :: XTGA, ARRAY_810, vdiCwertCllbak, vdiIwertCllbak, vdiRwertCllbak, vdiCwert2Cllbak, vdiIwert2Cllbak, vdiRwert2Cllbak, vdiErsterCllBak, vdiLetzterCllBak, vdiTestFuncCllBak
implicit none
external vdiCwertCllbak, vdiIwertCllbak, vdiRwertCllbak, vdiCwert2Cllbak, vdiIwert2Cllbak, vdiRwert2Cllbak, vdiErsterCllBak, vdiLetzterCllBak, vdiTestFuncCllBak
!procedure(vdiErsterCllBak), pointer :: m_vdiErsterCllBak
CHARACTER (len=256) XTGA
CHARACTER (len=256) TGA, ARRAY_810(10), retValue, satzArt, satzArt2
CHARACTER (len=256) :: cWertCallBackRet
integer :: nrReturnValues = 1
m_vdiTestFuncCllBak => vdiTestFuncCllBak
call vdiTestFuncCllBak(nrReturnValues, nrReturnValues)
call m_vdiTestFuncCllBak(1, nrReturnValues)
VdiFunctionRunner = nrReturnValues
end function VdiFunctionRunner
end module VdiFunctionRunnerMain
因为 Fortran 代码需要能够使用 C# 代码的某些函数,所以将两个委托传递给 Fortran 代码(vdiCwertCllbak、vdiIwertCllbak)。
当它们在 MainFunction 中使用时,效果非常好,因此到目前为止接口工作正常。
现在需要的是,c# 函数必须可从 MainFunction 之外的其他函数甚至不同模块中使用。
我尝试使用函数指针来处理这个问题,但是在调用m_vdiTestFuncCllBak时总是出现以下错误。调用 vdiTestFuncCllBak 可以正常工作。
在函数或外部模块中初始化指针时,行为相同。
调用以下c#代码:
private void vdiTestFunc(ref int inputValue, ref int retValue)
{
retValue = inputValue + 1;
return;
}
问题是,使用funtionpointer时,没有设置inputValue和retValue的引用。
有人以前遇到过同样的问题并且知道可能的解决方案或有帮助链接吗?我在搜索中没有找到相关信息。
我正在使用 Intel 11 编译器。
非常感谢您的帮助。
I am new in Fortan and have a question regarding using make callback-functions available for the whole fortran-code.
I am writing on a interface which accesses a Fortran DLL from C#.
module csWrapper
interface
subroutine vdiTestFuncCllBak(inputValue, retValue)
INTEGER, INTENT(IN) :: inputValue
INTEGER, INTENT(INOUT) :: retValue
end subroutine
end interface
procedure(vdiTestFuncCllBak), pointer :: m_vdiTestFuncCllBak
end module csWrapper
module VdiFunctionRunnerMain
use csWrapper
implicit none
contains
integer function VdiFunctionRunner (XTGA, ARRAY_810, vdiCwertCllbak, vdiIwertCllbak, vdiRwertCllbak, vdiCwert2Cllbak, vdiIwert2Cllbak, vdiRwert2Cllbak, vdiErsterCllBak, vdiLetzterCllBak, vdiTestFuncCllBak)
!DEC$ ATTRIBUTES DLLEXPORT ::VdiFunctionRunner
!DEC$ ATTRIBUTES REFERENCE :: XTGA, ARRAY_810, vdiCwertCllbak, vdiIwertCllbak, vdiRwertCllbak, vdiCwert2Cllbak, vdiIwert2Cllbak, vdiRwert2Cllbak, vdiErsterCllBak, vdiLetzterCllBak, vdiTestFuncCllBak
implicit none
external vdiCwertCllbak, vdiIwertCllbak, vdiRwertCllbak, vdiCwert2Cllbak, vdiIwert2Cllbak, vdiRwert2Cllbak, vdiErsterCllBak, vdiLetzterCllBak, vdiTestFuncCllBak
!procedure(vdiErsterCllBak), pointer :: m_vdiErsterCllBak
CHARACTER (len=256) XTGA
CHARACTER (len=256) TGA, ARRAY_810(10), retValue, satzArt, satzArt2
CHARACTER (len=256) :: cWertCallBackRet
integer :: nrReturnValues = 1
m_vdiTestFuncCllBak => vdiTestFuncCllBak
call vdiTestFuncCllBak(nrReturnValues, nrReturnValues)
call m_vdiTestFuncCllBak(1, nrReturnValues)
VdiFunctionRunner = nrReturnValues
end function VdiFunctionRunner
end module VdiFunctionRunnerMain
Because the Fortran-code need the possibility to use some functions of the C#-code to, a pass two delegates to the Fortran-code (vdiCwertCllbak, vdiIwertCllbak).
This works quite well when they are used in the MainFunction, so the interfacing works so far.
Now it is needed, that the c#-functions must be available from other functions outside of the MainFunction and even in different modules.
I tried to use functionpointers to deal with this problem, but always get the following error when calling m_vdiTestFuncCllBak. Calling vdiTestFuncCllBak works without problems.
It is the same behaviour when initializing the pointer in the function or in an external module.
The following c# code is called:
private void vdiTestFunc(ref int inputValue, ref int retValue)
{
retValue = inputValue + 1;
return;
}
The problem is, that the references of inputValue and retValue are not set when using the funtionpointer.
Does someone had the same issue before and knows a possible solution or has a link with help? I haven't found information about that in my searches.
I am using the Intel 11 compiler.
Help is very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种快速而肮脏的方法是创建一个包含过程指针的模块:
编辑:
访问冲突可能是因为指针没有关联的接口。
您需要正确定义接口的过程参数。
它也可能是由可选参数引起的。 vdiIwertCllbak 有选项吗?
A quick and dirty way would be to make a module that has a procedure pointer in it:
EDIT:
The access violation might be because the pointer has no associated interface.
You'll need to correctly define the procedure arguments for the interface.
It might also be caused by having optional arguments. Does vdiIwertCllbak have optionals?