Solved

How to write a self-modifying executable

Posted on 1998-11-19
4
281 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
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 100 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now