You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在Dymola中通过外部C调用32位DLL函数?

Dymola调用32位DLL函数的可行性及问题排查

问题背景

尝试在Dymola中调用32位DLL内的函数,编译函数成功,但执行时无报错提示却直接失败,仿真日志为空。已通过32位GCC编写独立C程序测试该函数,确认功能正常,现需确认Dymola是否支持此类调用。

命令行输出信息:

Must translate function Library.CopyDesign.
Compilation of function Library.CopyDesign successful.
Finished 
Process Execute function Library.CopyDesign stopped without any error indication.
Failed to execute command Library.CopyDesign.
Failed to expand Library.CopyDesign

对应Modelica代码:

function CopyDesign
input String I_RPath;
input String I_NPath;
input Integer I_Flags;
input Integer I_Serie;
input Integer I_Mode;
input String I_Typ;
input Integer I_CC;
input String I_Ref;
input Real I_Q;
input Real I_T0;
input Real I_TC;
input Real I_TS;
input Real I_TL;
input Real I_TN;
input Integer I_NET;
input Integer I_DS;
input Integer I_OV;
input Integer I_FI;
input Real I_FCF;
input Integer I_FCV;
input Integer I_FCOF;
input Integer I_FCMV;
input Integer I_OP;
input Real I_CR;

output String O_T1;
output Integer O_SzT1;

output String O_T2;
output Integer O_SzT2;

output TDesignData DesignData;

output Integer O_Hint1;
output Integer O_Hint2;

output String O_Err;
output Integer O_SzErr;

output Integer ReturnValue;

external"C" ReturnValue = CopyDesign(
I_RPath,
I_NPath,
I_Flags,
I_Serie,
I_Mode,
I_Typ,
I_CC,
I_Ref,
I_Q,
I_T0,
I_TC,
I_TS,
I_TL,
I_TN,
I_NET,
I_DS,
I_OV,
I_FI,
I_FCF,
I_FCV,
I_FCOF,
I_FCMV,
I_OP,
I_CR,
O_T1,
O_SzT1,
O_T2,
O_SzT2,
DesignData,
O_Hint1,
O_Hint2,
O_Err,
O_SzErr) annotation (LibraryDirectory="modelica://Library/",
Library="Hhk52");
end CopyDesign;

解答

1. Dymola调用32位DLL的可行性

Dymola支持调用32位DLL,但必须使用32位版本的Dymola。如果你的Dymola是64位版本,因架构不匹配,直接调用32位DLL会导致执行失败,这是此类问题最常见的原因。

2. 可能的其他问题点

  • 数据类型映射不匹配:Modelica与C语言的数据类型必须严格对应:
    • Modelica的String在C中对应const char*,但输出字符串需注意内存分配逻辑——要么由C函数提前分配足够内存,要么通过O_SzT1这类参数传递缓冲区大小,确保Dymola能正确接收结果。
    • 自定义类型TDesignData需保证Modelica中的定义与C结构体的内存布局(成员顺序、类型、对齐方式)完全一致。
  • 调用约定不一致:DLL中的C函数默认是cdecl调用约定,Modelica的external"C"默认匹配该约定,但如果DLL函数使用stdcall(如部分Windows API),需在声明中明确指定:external"C stdcall"。
  • DLL路径与依赖缺失:确认LibraryDirectory指向的路径正确,DLL文件存在;同时检查DLL依赖的其他32位库是否能被Dymola加载(可通过Dependency Walker工具排查)。
  • 输出参数处理错误:对于输出字符串,C函数需正确写入Dymola提供的缓冲区,且O_SzT1等参数需准确返回字符串长度;自定义结构体输出要确保内存被正确填充。

内容的提问来源于stack exchange,提问作者displayname

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:22:11