Для моего приложения мне нужно создать DLL из Delphi (точнее, код, совместимый с Delphi, написанный в Lazarus IDE, скомпилированный с помощью бесплатного pascal под Linux), используя stdcall.
При использовании этой DLL (например, в Matlab или около того), конечно, нужна метаинформация для передачи аргументов - часто реализуемая с помощью файла заголовка. Я ищу инструмент, который работает с исходным кодом delphi. Что-то вроде h2pas
-reverse.
Мои исследования не дали результатов. На мой взгляд, такого инструмента нет, я бы хотел найти таблицу или другую информацию, как типы данных Delphi / Pascal сопоставляются с типами C и как работать с записями.
Как создать C-Header для Delphi / Free Pascal / Lazarus DLL - типы данных
Ответы (1)
Я использовал приведенную ниже конструкцию для создания файлов заголовков, совместимых с компилятором C-режима Visual C ++ 6 из кода Delphi 5, когда в Delphi был переключатель -JPH (см. Примечания ниже).
Обратите внимание, что я не использовал это с Delphi 5, но с тех пор переключатель был расширен:
Где-то в этой строке к команде dcc32
был добавлен переключатель JPHNE -строчный компилятор:
-JPHNE = Generate C++ .obj file, .hpp file, in namespace, export all
У Руди Велтуиса есть хорошая статья, в которой используется переключатель JPHNE.
Он определенно не поддерживает все типы, и вам понадобится немного HPPEMIT и EXTERNALSYM.
Я загрузил свое преобразование HPP из Delphi 5 в Visual C ++ 6 из того времени в BitBucket.
Он генерирует файлы .hpp для импорта библиотеки DLL, написанной на Delphi.
Примечания из эпохи 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}