4

I have a Visual Studio 2008 C++ project for Windows CE 5 where I would like the currently running executable to modify itself.

Specifically, I would like to be able to read/write some data stored within the exe file itself. I do not need (or wish to) modify executable code.

In regular windows, I could use a string resource and the UpdateResource function, but that doesn't exist in WinCE.

CreateFile, unfortunately, fails because the file is already in use.

Does anybody have any other suggestions?

PaulH
  • 7,759
  • 8
  • 66
  • 143
  • 1
    Is this exactly needed or can you not store a flat file on disk? – sean Jul 24 '12 at 20:45
  • 3
    Sounds to me like a perceived requirement based on insufficient understanding, not an actual requirement. I could be wrong, but valid uses of this are few and far between. – Ed S. Jul 24 '12 at 20:53
  • The really hardcore solution would be to modify current memory dump (how much do you know about PE Header and sections?) and then dump it into file (or copy itself and modify...), but I doubt that there's any way how could you make this possible without killing one process and creating another (you can swap files by bat script executed on termination). – Vyktor Jul 24 '12 at 20:58
  • 1
    I doubt even `UpdateResource` would work on a currently running executable file. I agree with the others, your requirements are unrealistic. – Mark Ransom Jul 24 '12 at 21:18
  • 1
    The requirement is that the executable will generate some data that must persist for the lifetime of the installation of the program. The program is stored in NV memory and may outlast the registry. Also it may be run in a hostile environment where another program will attempt to delete or modify files at random (including my data file). My alternative (which looks more and more likely) is that I will have to keep a handle to that data file open so the the OS locks it and protects it from modification/deletion. – PaulH Jul 24 '12 at 21:41
  • I don't think there's a platform independent way to do this. If you know the path of the executable file and can open that file read/write, then there's no reason that what you want to do isn't possible -- just difficult. See: http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe – Benjamin Kay Jul 26 '12 at 18:34
  • Self-modifying programs are likely to look viral to AntiVirus software. I don't think it would be a good idea. You'd be better off using a data file — even in the hostile environment you describe. – Jonathan Leffler Aug 12 '12 at 05:15

1 Answers1

1

First, why do you need to do this? You should be able to do this with other methods.

I'm not particularly familiar with Windows-CE, but if you need to, you can probably copy the file, edit the copy, delete the first, and then run the other. That's an inefficient way, but if you only need to do it once or twice in the span of the program and speed isn't a concern, I guess you could do it:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char * argv[]) {
    // Check if this IS the copy:
    if (argv[0].find(argv[1]) != string::npos) {
        system("taskkill -IM myOLDfile.exe"); // Stop the old one running,
        system("del myOLDfile.exe"); // Then delete it.
    }

    ifstream myself(argv[0]); // argv[0] is the program itself
    string fullcode;
    string line;
    if (file.is_open()) {
        while (file.good()) {
            getline(myself, line);
            line.append("\n");
            fullcode.append(line);
        }
    }
    myself.close();
    // Do whatever you need to do to the code here.
    ofstream newcode("myNEWfile.exe");
    newcode.write(fullcode);
    newcode.close();
    system("myNEWfile.exe myNEWfile.exe"); // Starts new file. Also, not a typo.
}

Good luck on your project!

Cosine
  • 572
  • 4
  • 18