?
Solved

How to write a self-modifying executable

Posted on 1998-11-19
4
Medium Priority
?
289 Views
Last Modified: 2008-03-04
Is it possible to write an executable which is able to change itself at runtime? Specifically, I'd like to store some customization data in the executable file itself, so that if that exe is passed to someone else, the data can be retrieved.

I'm thinking that it might be possible to modify the exe's resources somehow, but have no idea if that would work as a scheme for persistence.

The essential point to this problem is that the information MUST be stored in the executable, not in the registry or in a separate file.

If the executable cannot modify itself while it is running, can it spawn another process and close itself? Could the other process then go and make the necessary changes to the exe? Just a thought...

Thanks for your help,
Brett
0
Comment
Question by:circuit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1178206
you can use BeginUpdateResource, ...,  for modifying resource but this function works only on NT 3.51+.

You can spwan another process, close the parent and then in this new process to modify resource.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178207
In windows, unlike DOS,  executables are opened read-only.  This makes this sort of thing nearly impossible to do.  There are ways around it, like using another process to do the writing, but did you ever stop to consider why they have the executable opened read-only?  The reason is that this sort of design tends to be dissasterious on a multi-processing system like windows.  You may be able to circumvent window's protections against writing to an executable, but they you run into the problems that windows is trying to prevent.  For example, what if you have two copies of your program running?  When one is closed so that another process can write to it, the EXE will still be open read only.  You won't be able to write to it.  What happens then?

I recommend you rethink your design.  Is it really that important that this information be written to the EXE?
0
 

Author Comment

by:circuit
ID: 1178208
Sorry, it is most important that I am able to perform this task on windows 95/98.  Thanks for your suggestion though.  

In response to nietod: I don't expect users to run two copies of my executable.  If they try, I can have the second one simply bring the first to top.  And yes, as I said in my post, modifying the executable is cornerstone to the idea of my project.

Thanks,
Brett
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1178209
If you must do it

(a) make a 2nd executable do the writing back to the 1st, while the 1st isn't running
(b) if you don't want to distribute 2 exe's, the 1st exe spits out the 2nd automatically.

e.g.
Program A wants to write to itself, therefore it does
(a) Program A finds where it's running (e.g. GetModuleFileName)
(b) Program A writes out program B to the same directory
(c) Program A kicks off program B ***
(d) Program A ends ***
(e) Program B waits till A has ended
(f) Program B writes to A's disk image
(g) Program B runs A
(h) program B ends ***
(i) Program A waits till B has finished ***
(j) Program A deletes program B disk image

The steps marked *** occur in parallel (c & d, AND,  h & i) hence the "wait" steps to make sure the last program terminated before modifying/deleting it's disk image.

A tip to make spit out stuff easier, is to embed binary resources into your EXE's, you can simply insert them in Visual Studio, or manually include files into your resource files.

Program A would include the binary image of a compiled program B as one of it's resources.

Program A could include additional binary resources (e.g. some data you can easily find)

Program B can then seek to this data, and write to it when working on A's disk image.



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question