I took this as a challenge, and managed it. I'm sure there's better ways, but.. it work perfectly for me. Compile this code, put your program in the same directory with the correct name, then run it.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream in( "infile.exe", ios::in | ios::binary );
float toFind = 60.0f;
float toReplace = 50.0f;
if ( !in ) {
return 1;
}
union {
float f;
struct {
char bytes[ sizeof(float) ];
};
};
int counter = 0;
while ( in.read(bytes, sizeof(float)) ) {
if ( toFind == f ) {
in.seekg( ios_base::beg );
break;
}
counter += sizeof(float);
}
ofstream out( "fixed.exe", ios::out | ios::binary );
while ( counter-- ) {
out.put( in.get() );
}
out.write( (char*) &toReplace, sizeof(float) );
in.ignore( 4 );
char ch;
while ( in.get(ch) ) {
out.put( ch );
}
cout << "Done.\n";
cin.ignore();
}
Hope this helps.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
May I ask why you don't want to compile the sources? And as for the program I made, it only changes the first 60.0f found, it would have to be modified to do any more. How many variables do you have assigned with the value 60.0f in your application?
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Hello William,
Yes you are correct your code does change only the first one. My mistake.
The code was compiled on a different computer, in order to recompile it I need to setup all the proper libraries with the proper versions (the code was compiled quite a few years ago). I am getting the libraries, but I thought it might be quicker just to modify the binary file. After looking into the code I see that there are some other places where value of 60.0f is used (there are also 9 places where I see 60.0f in the binary file). I would be disappointed if the code optimization process resulted in 9 constants with the same type and value, hence I am afraid that changing the binary would not do. Though through the investigation I have found that constants are stored towards the end of the executable.
Thanks a lot.
Copy and paste the unmodified project somewhere, and play around with the binary a bit.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129