944,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 517
  • C++ RSS
Oct 7th, 2009
0

Change constant in a exe compiled with Visual Studio C++ 2005

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sjcomp is offline Offline
32 posts
since Feb 2008
Oct 7th, 2009
2
Re: Change constant in a exe compiled with Visual Studio C++ 2005
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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main() {
  6. ifstream in( "infile.exe", ios::in | ios::binary );
  7. float toFind = 60.0f;
  8. float toReplace = 50.0f;
  9.  
  10. if ( !in ) {
  11. return 1;
  12. }
  13.  
  14. union {
  15. float f;
  16. struct {
  17. char bytes[ sizeof(float) ];
  18. };
  19. };
  20.  
  21. int counter = 0;
  22. while ( in.read(bytes, sizeof(float)) ) {
  23. if ( toFind == f ) {
  24. in.seekg( ios_base::beg );
  25. break;
  26. }
  27. counter += sizeof(float);
  28. }
  29.  
  30. ofstream out( "fixed.exe", ios::out | ios::binary );
  31.  
  32. while ( counter-- ) {
  33. out.put( in.get() );
  34. }
  35.  
  36. out.write( (char*) &toReplace, sizeof(float) );
  37.  
  38. in.ignore( 4 );
  39. char ch;
  40. while ( in.get(ch) ) {
  41. out.put( ch );
  42. }
  43.  
  44. cout << "Done.\n";
  45. cin.ignore();
  46. }
Hope this helps.
Last edited by William Hemsworth; Oct 7th, 2009 at 7:33 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Oct 7th, 2009
0
Re: Change constant in a exe compiled with Visual Studio C++ 2005
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sjcomp is offline Offline
32 posts
since Feb 2008
Oct 8th, 2009
0
Re: Change constant in a exe compiled with Visual Studio C++ 2005
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?
Last edited by William Hemsworth; Oct 8th, 2009 at 11:59 am.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Oct 8th, 2009
0
Re: Change constant in a exe compiled with Visual Studio C++ 2005
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sjcomp is offline Offline
32 posts
since Feb 2008
Oct 8th, 2009
0
Re: Change constant in a exe compiled with Visual Studio C++ 2005
Click to Expand / Collapse  Quote originally posted by sjcomp ...
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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please help me check my code to convert Infix to Postfix
Next Thread in C++ Forum Timeline: Question about Dev-C++ compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC