Read a file header into a record ?

104 views
Skip to first unread message

m. e.l.

unread,
Apr 29, 2025, 4:41:01 AM4/29/25
to innosetup
Hi all,

I have a
TMyFileheader = Record
  ...
end;

And I would like to read the header of the file into it:

procedure GetMyFileHeader(const Filename: string);
var FileHeader: TMyFileHeader;
       FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(Filename, fmOpenRead or fmShareDenyNone);
  try
    if FileStream.Read(FileHeader, SizeOf(FileHeader)) = SizeOf(FileHeader)  then begin
      ...
    end;
  finally
    FileStream.Free;
  end;

But the TFileStream.Read method only accepts a string (ReadBuffer as well)... !
How can I read the file header into the record ?
Or, if I read it into a string, how to copy the string content into the FileHeader record  ?

Thanks for any help !

Mel

Bill Stewart

unread,
Apr 29, 2025, 10:22:57 AM4/29/25
to innosetup
Are you sure this is the solution you need to pursue? It might help if you describe the overall problem you're trying to solve.

m. e.l.

unread,
Apr 29, 2025, 3:13:05 PM4/29/25
to innosetup
If I could read that file header, I could perform different actions, which depend on which version has the file I want to read. And this is to be found in the file header.
This would be the simplest solution.
If it is not possible, then I will look for another way.

Gavin Lambert

unread,
Apr 29, 2025, 11:26:49 PM4/29/25
to innosetup
On Wednesday, April 30, 2025 at 7:13:05 AM UTC+12 m. e.l. wrote:
If I could read that file header, I could perform different actions, which depend on which version has the file I want to read. And this is to be found in the file header.
This would be the simplest solution.

Once you get to a certain level of complexity, it's usually easier to package your code into an EXE or DLL that's called from the installer rather than trying to translate it into direct [Code]. 

Bill Stewart

unread,
Apr 30, 2025, 10:05:31 AM4/30/25
to innosetup
On Tuesday, April 29, 2025 at 1:13:05 PM UTC-6 m. e.l. wrote:

If I could read that file header, I could perform different actions, which depend on which version has the file I want to read. And this is to be found in the file header.
This would be the simplest solution.
If it is not possible, then I will look for another way.

To me this sounds like a possible XY problem[1].

This is why I was asking what is the actual problem you're trying to solve. (I wasn't asking how you think it might be possible to solve it.)


m. e.l.

unread,
May 1, 2025, 7:14:53 AM5/1/25
to innosetup
:) no it is not an XY problem. Just believe me.

One way or the other, I need to get the file version of some files.
Reading the file header was in my eyes the simplest solution.
As it seems Inno cannot do that, I will find another way.
Writing a dll just for that task is a possiblity.

Bill Stewart

unread,
May 1, 2025, 9:19:58 AM5/1/25
to innosetup
On Thursday, May 1, 2025 at 5:14:53 AM UTC-6 m. e.l. wrote:

:) no it is not an XY problem. Just believe me.

One way or the other, I need to get the file version of some files.
Reading the file header was in my eyes the simplest solution.

Inno Setup has a number of functions related to file versions. See the documentation:


Scroll down to the section "File Version functions". Here what's there as I write this:

function GetVersionNumbers(const Filename: String; var VersionMS, VersionLS: Cardinal): Boolean;
function GetVersionComponents(const Filename: String; var Major, Minor, Revision, Build: Word): Boolean;
function GetVersionNumbersString(const Filename: String; var Version: String): Boolean;
function GetPackedVersion(const Filename: String; var Version: Int64): Boolean;

function PackVersionNumbers(const VersionMS, VersionLS: Cardinal): Int64;
function PackVersionComponents(const Major, Minor, Revision, Build: Word): Int64;

function ComparePackedVersion(const Version1, Version2: Int64): Integer;
function SamePackedVersion(const Version1, Version2: Int64): Boolean;

procedure UnpackVersionNumbers(const Version: Int64; var VersionMS, VersionLS: Cardinal);
procedure UnpackVersionComponents(const Version: Int64; var Major, Minor, Revision, Build: Word);

function VersionToStr(const Version: Int64): String;
function StrToVersion(const Version: String; var Version: Int64): Boolean;

m. e.l.

unread,
May 2, 2025, 6:52:37 AM5/2/25
to innosetup
Thanks. I am aware about those functions.
They are working for exes or dlls, but not for proprietary data files, which is my case.

Eivind Bakkestuen

unread,
May 2, 2025, 9:39:19 PM5/2/25
to inno...@googlegroups.com
The inno setup script isn't really intended for low level file manipulation. Make a DLL. :)

--
You received this message because you are subscribed to the Google Groups "innosetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to innosetup+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/innosetup/84e78ae9-e6c3-4689-a268-2c590c481e37n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages