Jump to content

Recommended Posts

I have this simple code that writes a record at the end of a binary file (any):


 

type
  TRecTestData = record
    strText1: String[100];
    strText2: String[100];
    i64BigNum: Int64;
  end;
var
  fDatafile: File;
  intRecSize: Integer;
  recTestRec: TRecTestData;
  strTestFile: String;
begin
  strTestFile := 'FileIOData.exe';
  intRecSize := SizeOf(recTestRec);
  AssignFile(fDataFile, strTestFile);
  Reset(fDataFile, 1);
  Seek(fDataFile, FileSize(fDataFile));
  BlockWrite(fDatafile, recTestRec, intRecSize);
  CloseFile(fDataFile);

This works perfectly. But in my main project (+120,000 lines of code) it generates a I/O Error 103 at line BlockWrite.

I've tried anything, the code is the same.

Share this post


Link to post
29 minutes ago, Attila Kovacs said:

for sure is the code not the same

Same exact code, copy and paste.

16 minutes ago, David Heffernan said:

Even if you can succeed in writing to this file, what do you think the resulting exe will do? 

I then read the record data from the Exe itself. It works in an empty project I made.

I also found the solution. I'm missing this:

FileMode := fmOpenReadWrite;

So now it works everywhere! In the empty project I used it worked even without setting the FileMode, in my main project not because I set the FileMode to read-only previously but I wasn't aware of that.

Edited by Silver Black

Share this post


Link to post

Doesn't an exe contain a file hash? I'm pretty sure that there are better ways of solving your problem, whatever it is, than modifying an exe file. 

Share this post


Link to post
9 hours ago, David Heffernan said:

Doesn't an exe contain a file hash? I'm pretty sure that there are better ways of solving your problem, whatever it is, than modifying an exe file. 

The exe file is created by me (my program), so I just want to add additional data.

Share this post


Link to post
4 hours ago, Silver Black said:

The exe file is created by me (my program), so I just want to add additional data.

I think this isn't the way to do it. I'd expect to see you add a resource with the additional payload. 

Share this post


Link to post
On 10/17/2021 at 9:04 AM, David Heffernan said:

I think this isn't the way to do it. I'd expect to see you add a resource with the additional payload. 

In a resource you can add any type of data? Strings, integers, etc.? Why I cannot write on an arbitrary binary file if Delphi has the proper functions to do so (BlockWrite/Read and Streams)? There is a particular reason you say so?

Share this post


Link to post
30 minutes ago, Silver Black said:

In a resource you can add any type of data? Strings, integers, etc.?

Yes. 

 

31 minutes ago, Silver Black said:

Why I cannot write on an arbitrary binary file if Delphi has the proper functions to do so (BlockWrite/Read and Streams)?

This isn't really to do with Delphi. Writing to a file can be done in any language.

 

Certainly I can see that antimalware software is likely to object to such code.

 

I could image attaching a payload to an executable after building it, and I'd always do that using a resource. But I'd see a problem with an executable being modified in the field. That would make updates next to impossible. File versioning would be messed up. 

 

What data are you adding to the file anyway? Why are you doing this at all? And when you say arbitrary executable file, I presumably mean an executable file that you control. You can't really imagine doing this to executable files provided by arbitrary third parties? 

Share this post


Link to post
37 minutes ago, Lars Fosdal said:

Does the program itself append to its own .exe ?

Antivirus software may take offence. 

 

No, it's not the same executable that makes the changes. It's another application that creates that executables that modifies it at the end of the process.

Edited by Silver Black

Share this post


Link to post
17 minutes ago, David Heffernan said:

Certainly I can see that antimalware software is likely to object to such code.

 

I could image attaching a payload to an executable after building it, and I'd always do that using a resource. But I'd see a problem with an executable being modified in the field. That would make updates next to impossible. File versioning would be messed up. 

 

What data are you adding to the file anyway? Why are you doing this at all? And when you say arbitrary executable file, I presumably mean an executable file that you control. You can't really imagine doing this to executable files provided by arbitrary third parties? 

 

The custom-modified executable is not being modified in the field, it's only modified once after it's built by another app. After that it's not modified anymore.

Yes, I only do this to MY executable created by MY othe main app, it's not intended to modify any other executable. The attached data are user-related info and the executable is a zip-auto-extractor.

Share this post


Link to post

That all makes sense. However, I still think I would use a resource for this. I do something very similar myself. I my case the executable is a help browser, and the embedded data is zipped up webhelp content. There are windows API functions for updating resources. 

Share this post


Link to post
21 hours ago, David Heffernan said:

That all makes sense. However, I still think I would use a resource for this. I do something very similar myself. I my case the executable is a help browser, and the embedded data is zipped up webhelp content. There are windows API functions for updating resources. 

Ok, thank you, I'll give a look into that resource stuff. 🙂 Have a nice day!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×