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?

Recommended Answers

All 3 Replies

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.