【问题标题】:use HDF5 dll in Delphi application在 Delphi 应用程序中使用 HDF5 dll
【发布时间】:2020-07-03 03:25:54
【问题描述】:

我想在我的 Delphi 10.2 Windows 10 64 位应用程序中使用 HDF5 (https://www.hdfgroup.org/)。

我使用带有以下声明的 hdf5.dll(用 C 编写):

const
  H5P_DEFAULT: Integer = 0;

  H5F_ACC_RDONLY: Integer = $0000;  //*absence of rdwr => rd-only */
  H5F_ACC_RDWR: Integer = $0001;    //*open for read and write    */
  H5F_ACC_TRUNC: Integer = $0002;   //*overwrite existing files   */
  H5F_ACC_EXCL: Integer = $0004;    //*fail if file already exists*/
  H5F_ACC_DEBUG: Integer = $0008;   //*print debug info      */
  H5F_ACC_CREAT: Integer = $0010;   //*create non-existing files  */

type
  hid_t = Integer;
  hsize_t = UINT64;
  hssize_t = INT64;
  size_t = Cardinal;
  herr_t = Integer;
  unsigned = Cardinal;

  H5_ih_info_t = record
   index_size: hsize_t;
   heap_size: hsize_t;
  end;

  Tsohm =record
    hdr_size: hsize_t;
    msgs_info: H5_ih_info_t;
  end;

  TH5F_info_t = record
    super_ext_size: hsize_t;
    sohm: Tsohm;
  end;
  PH5F_info_t= ^TH5F_info_t;

//initializes the HDF5 library
function H5open: herr_t; cdecl; external 'hdf5.dll';

//flushes all data to disk, closes all open HDF5 objects, and cleans up all memory used by the HDF5 library
function H5close: herr_t; cdecl; external 'hdf5.dll';

//retrieves the major, minor, and release numbers of the version of the HDF5 library
function H5get_libversion( var majnum: unsigned; var minnum: unsigned; var relnum:unsigned): herr_t;  cdecl; external 'hdf5.dll';

// Opens an existing HDF5 file.
function H5Fopen(filename: PAnsiChar; flags: unsigned; fapl_id: hid_t): hid_t; cdecl; external 'hdf5.dll';

//Terminates access to an HDF5 file.
function H5Fclose(file_id: hid_t): herr_t; cdecl; external 'hdf5.dll';

//Returns global information for a file
function H5Fget_info2(obj_id: hid_t; file_info: PH5F_info_t): herr_t; cdecl; external 'hdf5.dll';

//Creates a new simple dataspace and opens it for access.
function H5Screate_simple(rank: Integer; current_dims: Pointer; maximum_dims: Pointer): hid_t; cdecl; external 'hdf5.dll';

//Creates a new group and links it into the file.
function H5Gcreate2(loc_id: hid_t; aname: PAnsiChar;  lcpl_id: hid_t; gcpl_id: hid_t; gapl_id: hid_t): hid_t; cdecl;  external 'hdf5.dll';

以下代码

procedure TForm1.Button1Click(Sender: TObject);
var
  status: herr_t;
  majnum, minnum, relnum: unsigned;
  file_id: hid_t;
  group_id: hid_t;
  dataspace_id: hid_t;
  dataset_id: hid_t;
  dims: array[0..1] of hsize_t;
  file_info: TH5F_info_t;
  pfile_info: PH5F_info_t;
begin
  status := H5open;
  Memo1.Lines.Add('H5open ' + IntToStr(status)); //status=0 -> OK

  status := H5get_libversion(majnum, minnum, relnum);
  Memo1.Lines.Add('H5get_libversion ' + IntToStr(status)); //status=0 -> OK
  Memo1.Lines.Add(IntToStr(majnum)); //majnum=1
  Memo1.Lines.Add(IntToStr(minnum)); //minnum=10
  Memo1.Lines.Add(IntToStr(relnum)); //relnum=6
                                     //-> version 1.10.6.

  file_id := H5Fopen('example.h5', H5F_ACC_RDWR, H5P_DEFAULT);
  Memo1.Lines.Add('H5Fopen ' + IntToStr(file_id));  //file_id=0 ->

  pfile_info:=@file_info;
  status:=H5Fget_info2(0, pfile_info);
  Memo1.Lines.Add('H5Fget_info2 ' + IntToStr(status)); //status=-1 -> ERROR

  group_id:=H5Gcreate2(file_id, 'testgroup', H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  Memo1.Lines.Add('H5Gcreate2 ' + IntToStr(group_id)); //group_id=-1 -> ERROR

  dims[0]:=4;
  dims[1]:=6;

  dataspace_id := H5Screate_simple(2, @dims, nil);
  Memo1.Lines.Add('H5Screate_simple ' + IntToStr(dataspace_id)); //dataspace_id=2 -> OK

  status:=H5Fclose(file_id);
  Memo1.Lines.Add('H5Fclose ' + IntToStr(status)); //status=-1 -> ERROR

  status:=H5close;
  Memo1.Lines.Add('H5close ' + IntToStr(status)); //status=0 -> OK
end;

给出这个输出

H5open 0
H5get_libversion 0
1
10
6
H5Fopen 0
H5Fget_info2 -1
H5Gcreate2 -1
H5Screate_simple 2
H5Fclose -1
H5close 0

H5Fopen 似乎可以工作,但所有使用 file_id 的函数都返回 -1 表示发生了错误。

可能是什么问题?

【问题讨论】:

  • 没有看到更多代码,我们无法知道出了什么问题。例如,我们看不到任何类型声明。制作一个最小的 C 示例来执行此操作。然后在 Delphi 中编写相同的代码。然后找到第一个不同的函数调用。然后深入挖掘
  • 我添加了类型声明。
  • 不幸的是,我几乎没有 C 知识,也没有太多在 Delphi 中使用外部 DLL 的经验。所以我真的没有计划如何调试它。如果您能给我更多提示,我会很好。
  • 我的建议是学习足够的 C 语言来编写一个调用 DLL 的简单程序,并且足够能够准确地翻译头文件。人们总是期望在这样的任务上取得成功,而无需花时间学习所需的技能,这让我一直感到惊讶。当我第一次需要执行此类任务时,我就是这样做的。
  • 您对 H5FGetInfo2 的声明是错误的。在 C 声明中,file_info 是指向 H5F_info2_t 的指针。请到这里:portal.hdfgroup.org/display/HDF5/H5F_GET_INFO2 查看 H5F_info2_t 的声明。您可能还有其他不正确的声明,所以也请检查一下

标签: c delphi dll hdf5


【解决方案1】:

Dave Nottage 的评论就是答案。类型不正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多