【发布时间】:2016-06-22 07:25:21
【问题描述】:
我是 R 的初学者,我正在尝试将用 C 编写的名为 dll.dll 的 .dll 文件加载到 R 中。它似乎有效,现在我想使用以下函数存储在.dll 文件中,我遇到了问题。
我已经在手册、此处和谷歌上搜索了解决方案或其他方法。如果我能得到关于使用什么或任何想法的建议,将非常感激!
我的代码:
setwd("C:/Users/MyUser/R")
dyn.load("dll.dll")
is.loaded("DLL_FUNK")
# For some reason True with capital letters, not in lower case
output <- .C("DLL_FUNK", in9 = as.integer(7))
#output # R Crashes before I can write this.
# R Crashes
# In outdata.txt: "in-value= 139375128"
该函数应该返回一个数字,1955。但我似乎无法得到那个值。我究竟做错了什么?
使用代码更新(Fortran 以 C 运行),这是 dll.dll 中的代码:
subroutine dll_funk(in9)
implicit none
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!*** Declarations: variables, functions
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer(4) :: in9
!integer :: in9
! Definitions of variables in the external function calls
!!dec$ attributes c,alias :'dll_funk' :: dll_funk
!dec$ attributes dllexport :: dll_funk
!dec$ attributes value :: in9
open(194,file='outdata.txt')
write(194,*) 'in-value=', in9
! in9 = 1955
close(194)
end subroutine
!end function
所以现在当它运行时,R 在写入我的文件 (outdata.txt) 之前崩溃了,但它不是我的号码,可能是某种地址...
另一个问题,你建议我用 .C 运行代码,然后从 C 运行 Fortran 代码,还是只用 Fortran 代码用 .Fortran 运行它更好? 似乎 .Fortran 处理字符串有问题,或者这就是我的理解:Interface func .C and .Fortran
【问题讨论】:
-
您的通话中可能缺少
"。试试dyn.load("2dll.dll") -
终于让它工作了,删除了所有的定义并使代码尽可能简单。