Fortran Coder

查看: 11267|回复: 1
打印 上一主题 下一主题

[混编] 实现C++的整个多维数组的数据向fortran dll的函数中传递

[复制链接]

14

帖子

4

主题

0

精华

入门

F 币
11 元
贡献
42 点
QQ
跳转到指定楼层
楼主
发表于 2017-7-17 14:18:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
20F 币
本帖最后由 cfortran 于 2017-7-17 14:21 编辑

fortran代码:
[Fortran] 纯文本查看 复制代码
01module hydromodelUtil
02  use iso_c_binding
03  implicit none
04  double precision, parameter :: pi=acos(-1.d0)
05  !这部分为输出值
06  type ,Bind(C):: singleinfo
07    integer(c_int) :: controlno
08    integer(c_int) :: unittype
09    integer(c_int) :: solid
10    real(c_double) :: coearea
11    real(c_double):: volume
12    real(c_double), dimension(3) :: center
13    integer (c_int):: pointcount, trianglecount
14    type(C_PTR) :: pointchain , trianglechain
15  end type singleinfo
16contains
17    subroutine prismmeshing(pn, plistdown, plistup, h, prisminfo) Bind(C,Name="prismmeshing")
18    !DEC$ ATTRIBUTES DLLEXPORT::prismmeshing 
19    implicit none
20  
21    integer(c_int) :: pn 
22    real(c_double), dimension(2, pn) :: plistdown, plistup
23    real(c_double) :: h
24    type(singleinfo) :: prisminfo
25    real(c_double),dimension(:,:),allocatable,target,save::pointchain
26    integer(c_int) :: meshi
27    prisminfo%pointcount=2*pn
28    allocate(pointchain(3, prisminfo%pointcount+2))        
29    do meshi=1, pn
30      pointchain(1:2,2*meshi-1)=plistdown(:,meshi)
31      pointchain(3  ,2*meshi-1)=0.d0
32      pointchain(1:2,2*meshi)=plistup(:,meshi)
33      pointchain(3  ,2*meshi)=h
34    end do
35    pointchain(:,2*pn+1)=pointchain(:,1)
36    pointchain(:,2*pn+2)=pointchain(:,2)
37    prisminfo%pointchain=c_loc(pointchain)
38    end subroutine prismmeshing  
39end module hydromodelUtil
C++代码:
[C++] 纯文本查看 复制代码
01struct singleinfo
02{
03        int cotrolno;
04        int unittype;
05        int solid;
06        double coearea;
07        double volume;
08        double center[3];
09        int pointcount;
10        int trianglecount;
11        double *pointchain;
12        double *trianglechain;
13};
14public:
15        singleinfo cylinderinfo;
16extern "C"{
17        void prismmeshing(int &,double (*plistdown)[][2],double (*plistup)[][2],double &,struct singleinfo*);
18}
19void Chydro2View::OnMeshing()
20{
21 
22     
23        int pn=3;
24        double plistdown[][2]={
25                {0,0},
26                {0,-8},
27                {12.5,0}
28        };
29        double plistup[][2]={
30                {0,0},
31                {0,-8},
32                {12.5,0}
33        };
34 
35        double h1=15;
36        prismmeshing(pn,&(plistdown),&(plistup),h1,&cylinderinfo);
37        CString str3;
38        str3.Format("%lf",cylinderinfo.trianglechain[24]);
39        this->MessageBox(str3);
40}

在网上找的传递方法,可是怎么改都不对,想问一下如何实现C++的整个多维数组的数据向fortran dll的函数中传递


最佳答案

查看完整内容

传递时,视为单变量传递。(即传递第一个元素的地址即可) [mw_shl_code=fortran,true]extern "C" { void prismmeshing(int &, double * plistdown, double *plistup , double &, struct singleinfo*); } struct singleinfo cylinderinfo; int main() { int pn = 3; double plistdown[][2] = { { 0, 0 }, { 0, -8 }, { 12.5, 0 } }; double plistup[][2] = { { 0, 0 }, ...
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

268

帖子

0

主题

0

精华

版主

World Analyser

F 币
749 元
贡献
526 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
沙发
发表于 2017-7-17 14:18:37 | 只看该作者
传递时,视为单变量传递。(即传递第一个元素的地址即可)
[Fortran] 纯文本查看 复制代码
01extern "C" {
02    void prismmeshing(int &, double * plistdown, double *plistup , double &, struct singleinfo*);
03  }
04  struct singleinfo cylinderinfo;
05  int main()
06  {
07 
08 
09    int pn = 3;
10    double plistdown[][2] = {
11      { 0, 0 },
12      { 0, -8 },
13      { 12.5, 0 }
14    };
15    double plistup[][2] = {
16      { 0, 0 },
17      { 0, -8 },
18      { 12.5, 0 }
19    };
20 
21    double h1 = 15;
22    prismmeshing(pn, &plistdown[0][0], &plistup[0][0], h1, &cylinderinfo);
23    cout << cylinderinfo.trianglechain[24] ;
24  }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2025-5-2 03:35

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

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