Application write to self(Self modifying) - Possible?

OK, a while back I wrote a class out of boredom as a lightweight file stream from scratch. I then sprouted from that creating a self archive like class that would read and write data to itself. I never tested it.

Now I came to a project today that would use something like that and remember I already made it. I set up a demo to actually test it and find out that it can only open a read handle from CreateFile() not a writing one. I could make it so that it creates a copy of the executable on the fly and modifies that one then launches it, but that seems messy. Is it possible to write to the end of an executable as it's running?
LVL 13
Dagan HooverDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
in short, no

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
Dagan HooverDeveloperAuthor Commented:
Well, this is what I thought at first when researching the subject. I was sure I needed to use a workaround.

The thing is, a program I use constantly called OllyDbg, a debugger, does the exact thing I'm trying to do. I can open an EXE in the debugger, modify the data(in this case editing the address space of the executable) then save it while it's still running. Now I'm wondering if anyone knows if this is a trick of some sort, or if there is more to it.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Geert GOracle dbaCommented:
editing in memory ?
from OllyDbg site:
  Examines and modifies memory, sets breakpoints and pauses program on-the-fly
Does it actually modify the file on disk ?

saving the changes to a temp location
after the exe shuts down
write from the temp to the exe
Geert GOracle dbaCommented:
you could rename the exe when running, copy it to the old name
save changes to the new file
when it restarts you have the changes in the new exe
Dagan HooverDeveloperAuthor Commented:
Well I just did a test to where I edited an executable while it was running, saved it, and killed ollydbg. The original.exe kept the changes. And the program that was running WAS the original.
Geert GOracle dbaCommented:
after you save the changes with OllyDbg you could see which file is actually still running with procexp.exe

Dagan HooverDeveloperAuthor Commented:
I did use procexp. It was the same file. It was never terminated(Used Ollydbg to debug Ollydbg to debug a test application).
Geert GOracle dbaCommented:
beats me how he does it :)
maybe you could ask him for the source code to see how ...
Dagan HooverDeveloperAuthor Commented:
Well I went ahead and did just that. Although it struck me that with some users not being able to modify the Program Files directory I'll have to go a different route anyway, so I'll accept the "no" answer since that's what it is.

If I get a reply from Ollydbg's creator I'll paste here how it's done.

Thanks for the quick reply though.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.