Use HDF5 dll in Delphi application

I would like to work with HDF5 in my Delphi 10.2 Windows 10 64bit application.

I use hdf5.dll with the following declarations:

//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’;

The following code

procedure TForm1.Button1Click(Sender: TObject);
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;
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 -> OK

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


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

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

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

gives this output

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

H5Fopen seems to work but all functions which uses the file_id afterwards return -1 meaning that an error occurred.

What could be the problem?

Thank you!

Are you sure that PH5F_info_t resolves to H5F_info2_t?

Can you pass file_id instead of 0 to H5Fget_info2? (Are you confident that 0 gets cast to a 64-bit integer?)

The fact that H5Gcreate2 fails suggests that there is more going on here. (?)


You are right. The types have been incorrect. Now it works. Thank you very much.