I would like to write aprox. 20 bytes to my running program's exe.. at a known location, without changing any of the code.. just to save the prog's settings. I'd like to know if there's an EASY way of doing so.. If it's too complicated, then i'll just skip it. Thanks.
I've seen this done before! So I know it's possible but I don't have any examples or anything, but you can do it! See ya, Ben
I'd think it's pretty simple. In your code, just put something easily recognizable in front of those 20 bytes, like "StartSettings -->", then have your prog open your exec file and search for those bytes. I just can't help thinking there must be a better way, but hey, even BO2K uses this.
yeah, it' s quit easy to do but you have to compile your code with the code section in writeable mode (see in the linkers options). mov ecx,myCodeToCopyEnd-myCodeToCopyStart mov esi,offset myCodeToCopyStart mov edi,offset myCodeGoesHere repnz movsb ; the rest of your code myCodeGoesHere: db (myCodeToCopyEnd-myCodeToCopyStart) dup (0) ; your code will be added here myCodeToCopyStart: some code here some other code code what you want and some code myCodeToCopyEnd:
Windows uses some kinda paging sceme when loading exe's. So should it not be possible to tell windows to treat just one page (4k) of a file as if it was being edited not executed? So when you write to a certain part of the .data segment, it would be written to the file instead of somewhere in memory? This may seem a little far fetched but I don't like the idea of an executable that has to search itself for data it already knows the location of.
Link your code section as writable, then you can write the bytes to it. Its in the LINK.EXE options. Regards, email@example.com
Do what Hutch said, but be careful about the CPU 4KB instruction cache. If you write to code just ahead of where you are, the CPU won't pick up the change because it's referring to the original instruction stored in its cache. You can clear this cache with a call or jmp instruction, but I find it's easier just to write behind where I am (behind the current epi), then loop back. Or you can write to a blank area in the code section, then jump to it. You'll store the instruction bytes in a data section, then read them to the code section while executing your program. What you want to do is a special case of "self-modifying code".