将具有包含指针的结构和包含指针的联合的 C 代码转换为 Fortran

问题描述

我正在研究 Excel XLL SDK,这是一个 C API,允许编译可在 Excel 中直接引用的 C DLL(称为 XLL),然后允许 Excel 直接调用 C 函数。 (当然,函数必须遵守某些规则等)

我想用 Fortran 重写这个 API。 API 使用少量基本 Windows 类型(在 Windows.h 包含的标头中定义),这些类型只是基本类型的 typedef(几乎是代码混淆!):

  • WORDunsigned short
  • 的 typedef
  • INT23signed int = int
  • 的 typedef
  • DWORD_PTR一个 unsigned __int64 = long long
  • BYTE一个 unsigned char
  • LPSTR 为空终止 char*

这些类型转换为 Fortran 如下:

  • logical*2
  • integer*4
  • integer*8
  • logical*1
  • 单个 character 的数组(尾随 \0 处理留给用户

然后定义非常简单的 struct,例如:

typedef struct xlmref12
{
    WORD count;
    XLREF12 reftbl[1];                  /* actually reftbl[count] */
} XLMREF12,*LPXLMREF12;

这已经提出了如何将 Fortran 的 C 指针转换为 Fortran 的问题。 (答案,据我所知:C_PTR (from ISO_C_BINDING)。)

然后,定义了更复杂的结构,依赖于包含指针的联合,例如:

typedef struct xloper 
{
    union 
    {
        double num;                 /* xltypeNum */
        LPSTR str;                  /* xltypestr */
#ifdef __cplusplus
        WORD xbool;                 /* xltypeBool */
#else   
        WORD bool;                  /* xltypeBool */
#endif  
        WORD err;                   /* xltypeErr */
        short int w;                    /* xltypeInt */
        struct 
        {
            WORD count;             /* always = 1 */
            XLREF ref;
        } sref;                     /* xltypeSRef */
        struct 
        {
            XLMREF *lpmref;
            IDSHEET idSheet;
        } mref;                     /* xltypeRef */
        struct 
        {
            struct xloper *lparray;
            WORD rows;
            WORD columns;
        } array;                    /* xltypeMulti */
        struct 
        {
            union
            {
                short int level;        /* xlflowRestart */
                short int tbctrl;       /* xlflowPause */
                IDSHEET idSheet;        /* xlflowGoto */
            } valflow;
            WORD rw;                /* xlflowGoto */
            BYTE col;               /* xlflowGoto */
            BYTE xlflow;
        } flow;                     /* xltypeFlow */
        struct
        {
            union
            {
                BYTE *lpbData;          /* data passed to XL */
                HANDLE hdata;           /* data returned from XL */
            } h;
            long cbData;
        } bigdata;                  /* xltypeBigData */
    } val;
    WORD xltype;
} XLOPER,*LPXLOPER;

我们在 Fortran 中有结构,很好,union(带映射)也有,但问题是:union 不能包含任何可分配的东西,也不能包含指针。

我该如何规避这个问题?

备注:事实上,我想在 Fortran 中重新编码(如果可能的话)头 XLCALL.H 和 Excel XLL SDK 的源 XLCALL.CPP 完全能够设计 Fortran XLL 完全在 Fortran 中。 (我可以用 C 包装 Fortran 代码并使用 Excel XLL SDK,但这不是我的任务......)为了完整起见,我在下面给出了 XLCALL.HXLCALL.CPP 代码。>

XLCALL.H

XLCALL.CPP

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)