Hello,

This is not a c++ question per se, but I do not see a better forum to ask it. I have a binary executable (compiled with Visual Studio C++ 2005) and the source code for this file. I need to change a constant, but for various reason I do not want to compile sources. I know the value and the type of the constant float 60.0f. The constant itself is mentioned multiple times in the code.

What are my options? Any suggestion would be welcome.

Theoretically speaking, I guess, I should be able to search through the binary to locate four bytes which contain 60.0f. But I think constants are stored in some particular place in the executable file.

Thanks.

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.

commented: Nice job, works! +0

Thanks William,

You even created a program to do that, nice. But I am sure that I can not change all 60.0f values in the program, though. Hence, replace all in the hex editor did not seem like a way to go.

Thanks anyway.

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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.