Writing to an Incbin'ed file

BlitzMax Forums/BlitzMax Beginners Area/Writing to an Incbin'ed file

Gabriel(Posted 2005) [#1]
I know it *isn't* possible, but is there any inherent BMax or Windows reason that it *couldn't* be done? It would be nice to be able to put things into the exe while still being able to modify them.

As I understand it, the resource data tacked onto an exe is kept completely separate from the program data, so that shouldn't be an issue, right? I can see that writing to a resource in an exe would mean moving the existing subsequent resources as well, but this is doable too, surely? Is there something I'm missing?


Who was John Galt?(Posted 2005) [#2]
I tried writing data to a blitz exe (nothing to do with Blitzmax incbin, just using readbytes/writebytes.) I think there's an issue with re-writing the exe file you are running under Windows - the best I managed was to write a copy with the modified data included.


smilertoo(Posted 2005) [#3]
windows locks the file your running.


xlsior(Posted 2005) [#4]
windows locks the file your running


Which brings up the question: can you unlock the file through some API call or the other?
Data files can be opened without being locked after all...

(some data files will be locked for exclusive use while openened by a program, while others can be openend by multiple progrrams at the same time)


Hotcakes(Posted 2005) [#5]
For example, NotePad does not lock the file you are editing. But I think Sybixsus meant writing to the included data in memory, not actually altering the exe itself...

It should be possible. I guess the obvious work around for now is to create a copy of the data in memory and write to that, but that's not very efficient. And I guess writing to the resource attached to the exe after it's loaded would result in a DEP violation, at least on Windows...


BlitzSupport(Posted 2005) [#6]

But I think Sybixsus meant writing to the included data in memory, not actually altering the exe itself


If that's the case, IncbinPtr and IncbinLen should take care of it, and I think CreateStaticBank or CreateBankStream applied to IncbinPtr would allow reading/writing fairly easily... haven't tried it though.

EDIT: Have now...

Incbin "test.txt" ' Create a text file called test.txt with a simple line of text, eg. Hello World.

Graphics 640, 480, 0

' Create static bank from Incbin memory...

bank = CreateStaticBank (IncbinPtr ("test.txt"), IncbinLen ("test.txt"))

' Read through bank...

For ascii = 0 Until BankSize (bank)

	' Get byte...

	b:Byte = PeekByte (bank, ascii)

	' Add character to string...

	raw$ = raw$ + Chr (b)

	' Modify byte (add 1 to ASCII value)...

	PokeByte bank, ascii, b + 1
	
Next

Print ""
Print raw$
Print ""

' Read the (modified) Incbin'd file...

file = ReadFile ("incbin::test.txt")

If file
	While Not Eof (file)
		modded$ = modded$ + Chr (ReadByte (file))
	Wend
EndIf

Print modded$


Bear in mind if you're incbin'ing images, etc, you're dealing with the raw file format!


xlsior(Posted 2005) [#7]
but being able to write to update an incbinned text file or bank into the actual .EXE itself would have a major exe itself can also have some uses, e.g. storing game settings or configuration data without the need for external .ini files or registry access


Tom Darby(Posted 2005) [#8]
...bear in mind that self-modifying executables may raise alarms with antivirus software--but yeah, there are a number of things this could be useful for...