Calling C Programs from IDL

51 篇文章 0 订阅

Here are some general tips for using Call_External.

  • All arrays which need to be passed between IDL and C must be allocated in IDL. This includes both arrays being passed from IDL to C and from C back to IDL. Sometimes this requires an initial call to the C code to return the array sizes which IDL will allocate, if the array sizes are not known to IDL beforehand.
  • Don't deallocate any arrays which were passed from IDL.
  • Don't pass strings, rather pass byte arrays. It is much simpler. Convert strings to byte arrays in IDL before or after the Call_External call.
  • Convert all output variables to the data type which C is expecting in the Call_External call.

Question: What is the effect of the /CDECL keyword to Call_External?

Answer: This controls the calling convention. If your C function is being called then you probably have this set correctly.

Question: Is it possible that the C program "forgets" something between the IDL Call_External calls?

Answer: Yes, it will forget anything which is not global or static.

Question: How can I return an array via Call_External or have I always to loop over calls returning scalars ?

Answer: Here is a simple example. It is C code which computes the Mandelbrot set, and is called from IDL. The argv[7] argument is a 2-D array.

   void mandelbrot(int argc, void *argv[])
   {
    int nr = *(int *) argv[0];
    int ni = *(int *) argv[1];
    double rstart = *(double *) argv[2];
    double istart = *(double *) argv[3];
    double dr = *(double *) argv[4];
    double di = *(double *) argv[5];
    int max_iter = *(int *) argv[6];
    int *result =  argv[7];
   int i, j, count;
    double real, imag, rz, iz, sz2, rz2, iz2;
       for (i=0; i<ni; i++) {
         imag = istart + i*di;
         for (j=0; j<nr; j++) {
              real = rstart + j*dr;
              rz = 0.;
              iz = 0.;
              sz2 = 0.;

              count = 0;
              while ((count < max_iter) && (sz2 < 4.0)) {
                   rz2 = rz * rz;
                   iz2 = iz * iz;
                   iz = 2.0 * rz * iz + imag;
                   rz = rz2 - iz2 + real;
                   sz2 = rz2 + iz2;
                   count++;
              }
              *result++ = count;
         }
       }
   }

Here is the IDL code which calls the C code:

   function mandelbrot1, xcenter, ycenter, radius, size, max_iter, xout, yout
   if (n_elements(size) eq 0) then size=100
   if (n_elements(max_iter) eq 0) then max_iter=255
   dx = double(radius)*2/size
   xstart = double(xcenter - radius)
   xstop = double(xcenter + radius)
   ystart = double(ycenter - radius)
   ystop = double(ycenter + radius)
   result = lonarr(size, size)
   xout = xstart + findgen(size)*dx
   yout = ystart + findgen(size)*dx
   s = call_external('mandelbrot.dll', 'mandelbrot', $
                      long(size), $
                      long(size), $
                      double(xstart), $
                      double(ystart), $
                      double(dx), $
                      double(dx), $
                      long(max_iter), $
                      result)
   return, result
   end

Question: Can I pass IDL structures to my C program using Call_External?

Answer: [From 4 APRIL 1996] The last time I looked, the means by which structures are passed was intentionally not documented, presumably so that RSI would be free to change it in the future.

However, I know by experience that IDL presently passes structures just like you would expect (i.e. it passes the address of the start of the structure). All structure elements except strings are contained in the structure itself (i.e. the structure contains the value, not a pointer). Strings are different: the structure contains either the descriptor or the address of the descriptor (I forget).

I routinely pass structures to Call_External, but I do so at my own risk, since it is not guaranteed to be done the same way in future versions of IDL.

I have found that the structures will contain padding to keep the members aligned on natural boundaries. The C compiler will normally do this on the structures in yourCall_External code as well, so it has not been a problem.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值