How can I have my program modify itself?

Example:

Size = 1mb ..Run program once.. size = ++1mb

On Second Run, check if itself is 1mb

Or something of the sort :S.

I need to be able to check if my program has ever been ran before..

So.. Is there any way to tell if it's ever been ran on that particular machine without making a text file or extra files?
My Idea was to make it modify itself and then check if it is modified. If so then it has been ran already. Or maybe have it update it's own resources internally and on the second run, check if that resource changed?

Any Ideas?

Edited 4 Years Ago by triumphost

The basic problem here is that Windows will not allow your program to modify itself, or else it will generate and access violation. So one solution to that might be for the program to load a copy of itself into a data area and then modify that. Then just save it back to disk. So then, you will just always be modifying the original copy that resides on the disk.

For example, in your program, you could have something like this:

int main()
{
   _asm

   {
      nop
      nop
      nop
      nop
      nop
      nop
   }

   ......
   ......
   ......

   return 0;
 }

There are 6 nop (no operation) assembly statements in a row. Each nop instruction is the number 90, so then there are six 90's in a row there. The nop instruction just swaps the AX register with itself, just to kill time. Once a copy of your program is loaded from disk into a data area in the current running program, just do a scan for those 6 nop instruction and then ++increment the last one so it reads 91. Then just subtract 90 from that last nop instruction to determine how many times previously the program has run. And of course, save the modified copy back to disk to over write the original.

But make sure that the compiler doesn't optimize away those nop statements. Maybe turn off the optimization for just that section of code.

Edited 4 Years Ago by MandrewP: Added content

Ahh that makes sense but how would I even modify the copy :S I can't think of "what" to do to the file or how to modify it.. I just want to check if it's been ran already :c

Edited 4 Years Ago by triumphost

Ooops - one mistake! If you increment the last nop instruction, then it is no longer a nop instruction, but will do something! So, you can't increment it unless it is bypassed. Maybe bypass all of those 90's with a goto statement (or something less EVIL)

    goto bypass;

     nop
     nop
     nop
     nop
     nop
     nop

    bypass:

All of this is just off the cuff stuff, so you have to hone it to your satisfaction.

Edited 4 Years Ago by MandrewP: Added content

This question has already been answered. Start a new discussion instead.