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.
Posted on 2000-12-17 22:19:00 by your mama
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
Posted on 2000-12-18 00:47:00 by cyberben
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.
Posted on 2000-12-18 08:04:00 by Qweerdy
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:
Posted on 2000-12-18 12:54:00 by roy
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.
Posted on 2000-12-19 01:09:00 by Satrukaan
Link your code section as writable, then you can write the bytes to it. Its in the LINK.EXE options. Regards, hutch@pbq.com.au
Posted on 2000-12-19 04:57:00 by hutch--
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".
Posted on 2000-12-19 12:37:00 by A Mason