Using HDF.PInvoke.1.10.612 I can successfully create a scalar fixed length string like this;
void CreateFixedLenString(string s)
{
long hf = H5F.create(@"d:\testfile.h5", H5F.ACC_TRUNC);
long hs = H5S.create(H5S.class_t.SCALAR);
long ht = H5T.copy(H5T.C_S1);
int err = H5T.set_size(ht, new IntPtr(s.Length + 1));
long ha = H5A.create(hf, "fixed_len_str", ht, hs);
try
{
unsafe
{
var bytes = Encoding.ASCII.GetBytes(s + '\0');
fixed (byte* b = bytes)
{
int result = H5A.write(ha, ht, new IntPtr(b));
result.Dump(); // LinqPad helper
}
}
}
catch (Exception ex)
{
ex.Dump();// LinqPad helper
}
finally
{
H5A.close(ha);
H5T.close(ht);
H5S.close(hs);
H5F.close(hf);
}
}
But if I try pretty much the same thing to create a variable length string I end up with an access violation:
void CreateVariableLenString(string s)
{
long hf = H5F.create(@"d:\testfile.h5", H5F.ACC_TRUNC);
long hs = H5S.create(H5S.class_t.SCALAR);
// The only difference
long ht = **H5T.create(H5T.class_t.STRING, H5T.VARIABLE);**
long ha = H5A.create(hf, "variable_len_str", ht, hs);
try
{
unsafe
{
var bytes = Encoding.ASCII.GetBytes(s + '\0');
fixed (byte* b = bytes)
{
int result = H5A.write(ha, ht, new IntPtr(b));
result.Dump(); // LinqPad helper
}
}
}
catch (Exception ex)
{
ex.Dump(); // LinqPad helper
}
finally
{
H5A.close(ha);
H5T.close(ht);
H5S.close(hs);
H5F.close(hf);
}
}
Any ideas what I’m doing wrong?