Fortran Coder

查看: 9134|回复: 4
打印 上一主题 下一主题

[混编] IVF调用 C 语言DLL发生错误

[复制链接]
回帖奖励 30 元F 币 回复本帖可获得 10 元F 币奖励! 每人限 1 次

35

帖子

17

主题

0

精华

熟手

F 币
136 元
贡献
240 点
跳转到指定楼层
楼主
发表于 2017-2-15 13:48:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
先贴代码
[Fortran] 纯文本查看 复制代码
program s
!<-------------------------------------------------------------------------------------------------------------------------------
use link_cont
use dfwin     !< win api
!<-------------------------------------------------------------------------------------------------------------------------------
implicit none
integer(kind=4)                  :: add
integer(kind=4)                  :: a,b
integer(kind=4)                  :: hwnd
integer(kind=4)                  :: n_case
integer(kind=4)                  :: n_data
integer(kind=4)                  :: lpool
character(len=40), dimension(10) :: c_files  
logical                          :: fail
logical                          :: syslink
!<-------------------------------------------------------------------------------------------------------------------------------
!<-------------------------------------------------------------------------------------------------------------------------------
pointer(add,FileOpenCont) 

hwnd = LoadLibrary('control.dll'C)
if (hwnd <= 0) then
  fail    = .true.
  syslink = .false.
  pause 'DLL Loading failure because control.dll was not found'
else
  write(*,*) 'Succeed Contempt DLL (control.dll) Loading'
  add     = GetProcAddress(hWnd,'FILEOPENCONT'C)  
  call FileOpenCont(n_case,n_data,lpool,c_files)    
endif

end program s

[Fortran] 纯文本查看 复制代码
module link_cont
  interface
    subroutine FileOpenCont (ncase,ndata,lpool,xfiles)
    !MS$ ATTRIBUTES DLLIMPORT :: FileOpenCont
    integer(kind=4) :: ncase
    integer(kind=4) :: ndata
    integer(kind=4) :: lpool
    character(len=40), dimension(10) :: xfiles
    endsubroutine FileOpenCont  
  endinterface
endmodule link_cont
     

问题描述:
1. 代码为工程使用,用于调用C语言dll库。编译时不需要dll库,所以没有贴出。
2. 此两段代码在CVF下可以编译通过,
而在IVF下编译会报错:error #6401: The attributes of this name conflict with those made accessible by a USE statement.   [FILEOPENCONT]
指的是FILEOPENCONT重名,不知道如何修改,让原有功能同时实现,请教各位大大。






分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2017-2-15 15:02:28 | 只看该作者

回帖奖励 +10 元F 币

你的写法非常不标准,比如  pointer(add,FileOpenCont),这就是不对的(尽管IVF允许这样,但是其他编译器会出错)

可以用标准的写法

[Fortran] 纯文本查看 复制代码
module link_cont
  interface
    subroutine I_FileOpenCont (ncase,ndata,lpool,xfiles)
      integer(kind=4) :: ncase
      integer(kind=4) :: ndata
      integer(kind=4) :: lpool
      character(len=40), dimension(10) :: xfiles
    endsubroutine I_FileOpenCont
  endinterface
endmodule link_cont

program s
  use link_cont
  use Kernel32
  use , intrinsic :: ISO_C_Binding !// 标准模块
  implicit none
  type(C_FUNPTR) :: C_FileOpenCont !// C 语言指向函数的指针
  procedure(I_FileOpenCont) , pointer :: FileOpenCont !// fortran 指向函数的指针
  !<-------------------------------------------------------------------------------------------------------------------------------
  integer(kind=4)                  :: add
  integer(kind=4)                  :: hwnd
  integer(kind=4)                  :: n_case
  integer(kind=4)                  :: n_data
  integer(kind=4)                  :: lpool
  character(len=40), dimension(10) :: c_files
  logical                          :: fail
  logical                          :: syslink
  !<-------------------------------------------------------------------------------------------------------------------------------
  hwnd = LoadLibrary('control.dll'//c_null_char)
  if (hwnd <= 0) then
    fail    = .true.
    syslink = .false.
    write(*,*) 'DLL Loading failure because control.dll was not found'
    read(*,*)
  else
    write(*,*) 'Succeed Contempt DLL (control.dll) Loading'
    add     = GetProcAddress(hWnd,'FILEOPENCONT'//c_null_char) !//获得地址(整数)
    C_FileOpenCont = transfer( add , C_FileOpenCont ) !//把整数的地址转换成 C 指针
    call C_F_PROCPOINTER( C_FileOpenCont , FileOpenCont ) !//把C指针转换成 fortran 指针
    call FileOpenCont(n_case,n_data,lpool,c_files)
  endif
end program s

35

帖子

17

主题

0

精华

熟手

F 币
136 元
贡献
240 点
板凳
 楼主| 发表于 2017-2-15 15:55:45 | 只看该作者
非常感谢,已测试过代码完全可以。
原有代码比较老,想进行转化,确实有很多古老的写法。

另: 我想知道,为何原有代码ivf编译会出现变量名冲突的问题,而cvf不报错?

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

地板
发表于 2017-2-15 16:02:41 | 只看该作者
因为 pointer(add,FileOpenCont) 这种写法本身就是不规范的,语法并不允许这样。我也不知道为什么 CVF 不报错~~

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

5#
发表于 2017-2-15 16:10:16 | 只看该作者

回帖奖励 +10 元F 币

ksfengjia 发表于 2017-2-15 15:55
非常感谢,已测试过代码完全可以。
原有代码比较老,想进行转化,确实有很多古老的写法。

若是仅考虑Windows平台下使用,完全可以虚拟机安装XP和cvf进行编译,再拷贝dll出来
后续开发工作,可以在保持接口函数不变的情况下另起炉灶
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-20 20:07

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表