gpt4 book ai didi

c - 如何为 Delphi/Free Pascal/Lazarus DLL 创建 C-Header - 数据类型

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:52 33 4
gpt4 key购买 nike

对于我的应用程序,我需要使用 stdcall 从 Delphi 创建一个 DLL(更准确地说是在 Lazarus IDE 中编写的 Delphi 兼容代码,由 linux 下的免费 pascal 编译)。
当使用该 DLL(例如在 Matlab 中)时,当然需要用于传递参数的元信息 - 通常通过头文件实现。我正在寻找一种工具来处理在 delphi 源代码上运行的工具。类似于 h2pas-reverse.
我的研究没有结果。正如我所想,没有这样的工具,我想找到一个表或其他信息,Delphi/Pascal 数据类型如何映射到 C 类型以及如何使用记录。

最佳答案

当 Delphi 具有 -JPH 开关时,我使用以下构造从 Delphi 5 代码生成与 Visual C++ 6 的 C 模式编译器兼容的头文件(参见下面的注释)。

请注意,自从 Delphi 5 以来我就没有使用过它,但是此开关已被扩展:

沿线的某个地方,JPHNE switch已添加到 dcc32 命令行编译器中:

  -JPHNE = Generate C++ .obj file, .hpp file, in namespace, export all

Rudy Velthuis 有一个 nice article using the JPHNE switch .

它当然不能处理所有类型,您将需要相当多的 HPPEMITEXTERNALSYM指令。

我上传了我的 Delphi 5 to Visual C++ 6 HPP conversion from back then到 BitBucket。

它生成 .hpp 文件以导入用 Delphi 编写的 DLL。

Delphi 5 时代的笔记:

{ Visual C++ 6 does not like nested structs/unions the way that BC++ can handle them.
Visual C++ 6 requires the "typedef" to be present AND the typename AFTER the struct definition.
You will see this when defining TConversationId, TReturnKey and other types that depend on nested structs/unions

The trick is to perform these steps each and every time this unit changes:
- Generate this unit once with all the EXTERNALSYM disabled.
- Then the DCC32 -JPH will generate the basic structs for them.
- Copy all the nested struct and union definitions to this file
- Embed the definitions with (*$HPPEMIT '' *)
- Append the typename at the end of the struct definition
- Enable all the EXTERNALSYM again
- Regenerate the HPP files by using DCC32 -JPH
To make this process easier, we have introduced two new conditional defines:
- BCB - disable EXTERNALSYM, disable HPPEMIT
- VC6 - enable EXTERNALSYM, enable HPPEMIT

A similar thing is with these constructions that VC6 does not like those either:
- short strings (BCB defines them as "SmallString<##>" which VC6 does not like).
- short integers (BCB defines them as "Shortint" so we have included an HPPEMIT for that)
}

{$ifdef win32}
{ important! Makes sure that the all enumerated types fit in exactly one byte each! }
{$Z1}

{ force the C/C++ HPP header files to have the C/C++ compiler pack structure elements on byte boundaries }
{$ifdef BCB}
{$HPPEMIT '#pragma option push -a1' }
{$endif BCB}
{$endif win32}

关于c - 如何为 Delphi/Free Pascal/Lazarus DLL 创建 C-Header - 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734103/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com