| | |
Change constant in a exe compiled with Visual Studio C++ 2005
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
Regards, Alexander. http://sjcomp.com
•
•
Join Date: Mar 2008
Posts: 1,494
Reputation:
Solved Threads: 123
2
#2 Oct 7th, 2009
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. Hope this helps.
C++ Syntax (Toggle Plain Text)
#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(); }
Last edited by William Hemsworth; Oct 7th, 2009 at 7:33 pm.
I need pageviews! most fun profile ever :)
0
#3 Oct 7th, 2009
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.
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.
Regards, Alexander. http://sjcomp.com
•
•
Join Date: Mar 2008
Posts: 1,494
Reputation:
Solved Threads: 123
0
#4 Oct 8th, 2009
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.
I need pageviews! most fun profile ever :)
0
#5 Oct 8th, 2009
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.
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.
Regards, Alexander. http://sjcomp.com
•
•
Join Date: Mar 2008
Posts: 1,494
Reputation:
Solved Threads: 123
0
#6 Oct 8th, 2009
•
•
•
•
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.
I need pageviews! most fun profile ever :)
![]() |
Similar Threads
- How to Set up pdcurses with Visual Studio 2005 (C++)
- How Do I change Visual Studio 2005 Startup (C)
- How to make Visual Studio 2005 C++ MSI? (C++)
- Visual Studio 2005 problem (C)
- Visual Studio 2005 + Symbian programming (C)
- The Visual Studio® 2005 Standard Edition (C++)
- Visual Studio 2005 & POSIX threads (C)
- Visual Studio 2005 Launch Poll (C#)
Other Threads in the C++ Forum
- Previous Thread: Please help me check my code to convert Infix to Postfix
- Next Thread: Question about Dev-C++ compiler
Views: 253 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






