Is it possible to modify an executable file on runtime (I'm asking about Windows XP/Vista/7/Server)? I've just evaluated SmartUtils Portable Storage application. It can create so called "managed executable storage files" that modify them-self at runtime... Such storage file is like standard self-extracting archive (the data is apended to an executable module) but the main difference it that you are able to view and modify its content without the main program. How is it possible? I need similar functionality in my project (C++): I want to be able to create executable that can modify data attached to it.
-
1[Self modifying code](http://en.wikipedia.org/wiki/Self-modifying_code) is a good start – parapura rajkumar Dec 08 '11 at 21:49
-
1@parapurarajkumar: I don't think here we are talking about self-modifying code; probably it's just some kind of archive appended at the end of the real executable. – Matteo Italia Dec 08 '11 at 21:50
-
Welcome to StackOverflow. Please edit your question to specify exactly what functionality you need to implement. Expecting people to go research an external web site in order to figure out what you're asking is unreasonable. If for some reason the site is unavailable, it makes your question unmeaningful (and unsearchable for future readers). While you're at it, you perhaps should add a tag indicating which OS you're targeting; what's possible varies between them. Thanks. :) – Ken White Dec 08 '11 at 21:54
-
Also, you may want to see this [question](http://stackoverflow.com/questions/7288279/how-to-embed-a-file-into-an-executable) and the pages linked from there (see the list of `Linked Questions` on the right below the `tagged` list). – Ken White Dec 08 '11 at 22:00
-
Executables are files, you can modify them as you would any other file (though, depending on OS, not while they're running, you'd have to work on a copy). You can thank [John von Neumann](http://en.wikipedia.org/wiki/John_von_Neumann) and [Alan Turing](http://en.wikipedia.org/wiki/Alan_Turing). What kind of problem are you running into using the normal file manipulation functions? – derobert Dec 08 '11 at 22:03
-
http://msdn.microsoft.com/en-us/library/ms809762.aspx – Hans Passant Dec 08 '11 at 22:16
4 Answers
Yes - a common technique is to append data files at the end of an executable.
Typical scheme is to write a 0x00000000 integer to the end of the executable and then append each file followed by it's size in bytes.
Then when the executable needs to read the data it checks the last 4bytes in it's own file, uses that as the file length and copies that number of bytes form it's own file, it then checks the next 4 bytes as another length and copies that as a file , until it gets a length of 0000. If you also need to code the file names - that adds a little complexity but it's basically the same idea.

- 94,801
- 28
- 188
- 263
If all you're really asking is how SmartUtils Portable Storage does it's magic, then I would suggest that it is a self-executing zip archive. The EXE of the archive (just as WinZip or 7-Zip create) auto-extracts and executes your application exe from a temp folder, and gives you an API that boils down to ways to extract, manipulate, and then modify that original self-executing archive.
So Windows is never trying to modify a running .exe. Rather, your .exe (temp file extracted & run) is what is executing (and the libraries bound to it), which manipulates the source .exe (really a self-executing archive - possibly .zip).
The next time the user "runs" the modified "exe", again your .exe is extracted & run, and it can again manipulate the self-extracting .exe.
I hope that makes sense to you.
And this is just a best guess!

- 9,412
- 6
- 60
- 112
You can append a TOC pointer to an EXE (and probably a magic ID cookie) so you can verify that it is a TOC pointer, and then use that to back up to the start of each appended record.
As long as you don't mess up the file's header & main contents, it should still be loadable by the OS.
However, you sacrifice any signing your EXE had - and you probably have various permissions issues to contend with...
I have written tools for my development environment that opens a Windows EXE, extrapolates the resources in it, modifies various ones, and repackages the whole thing. We use this to mark a beta as release (so it modifies the version records).
You can do anything you want to an EXE file if you know the structure of it and rebuild it correctly.

- 9,412
- 6
- 60
- 112
-
I don't think you can get a writable handle to a file that's currently being executed. – Mooing Duck Dec 08 '11 at 22:37
-
Thats the problem! An executable file can not be modified using standard approach using CreateFile/WriteFile. But it seems that the application I've wrote about implements some kind of workaround... – Den Wronglr Dec 09 '11 at 07:56
-
If Windows won't let you write to an executing file (not that surprising), then you have to extract the resources & manipulate them elsewhere (or transfer execution to a different module, which opens & modifies the original). – Mordachai Dec 09 '11 at 15:12
Since this is tagged as Windows
, you might also consider "Alternate Data Streams". That allows you to treat a single file almost as a directory. You can add a stream called Program.EXE:ExtraData
to your program and write to that with the normal file functions.
Then again, your executable most likely will be in Program Files\
, which isn't writeable for normal (non-elevated) users.

- 173,980
- 10
- 155
- 350
-
Unfortunately, that's an NTFS only feature, and as I understand it, is not normally available on DVD/CD/USB keys, etc. There was something a while back that Raymond Chan wrote about indicating that the Windows Shell team stopped using alternate data streams because of its non-portability (and I think you can extrapolate that if the Windows Shell avoids it, it must be in fact quite limiting) – Mordachai Dec 09 '11 at 15:20
-
2Well, a self-modifying EXE on a CD or DVD is going to face rather unique challenges ;). As for the Windows Shell, you're probably refering to [Why are custom properties created on Windows 2000 lost...](http://blogs.msdn.com/b/oldnewthing/archive/2011/05/27/10168422.aspx) – MSalters Dec 09 '11 at 15:33
-
Thanks for adding the link. Yeah, that's what I was referring to. And the SmartUtils the OP refers to doesn't use streams, and hence doesn't suffer that limitation. Not sure what the OP's requirements are, but worth considering that limitation. ;) – Mordachai Dec 09 '11 at 15:35