问题描述
我正在研究 Excel XLL SDK,这是一个 C API,允许编译可在 Excel 中直接引用的 C DLL(称为 XLL),然后允许 Excel 直接调用 C 函数。 (当然,函数必须遵守某些规则等)
我想用 Fortran 重写这个 API。 API 使用少量基本 Windows 类型(在 Windows.h
包含的标头中定义),这些类型只是基本类型的 typedef
(几乎是代码混淆!):
-
WORD
是unsigned short
的 typedef
-
INT23
是signed int
=int
的 typedef
-
DWORD_PTR
是一个unsigned __int64
=long long
-
BYTE
是一个unsigned char
-
LPSTR
为空终止char*
这些类型转换为 Fortran 如下:
然后定义非常简单的 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.H
和 XLCALL.CPP
代码。>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)