Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c++ - Program that modifes string inside its exe

I looking for example of program, that modifies a string inside its exe.

I work with C++, Visual Studio under Windows.

I searched working examples in Windows, but I can't find any working code.

I need simple code, that will ask user for string:

string strTest = "";
(if strTest != "")
{
   cout << "Modified: " << strTest << endl;
}
cin >> strText;

And code should rewrite:

string strTest = "";

To string that typed user:

string strTest = "SomeStringFromUser"; 

How, in C++, do you modify a string (from string strTest = ""), to string, what a user typed? (for example to strTest = "foo")?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When an EXE is running on a Windows machine, the exe file is held open as a CreateFileMapping object with pages marked either as READONLY or COPY_ON_WRITE.

So when the exe writes to itself, the file is not modified. It just creates a new page backed by the swap file. But since the file is kept open, no-one else can open the EXE file and write to it either.

Other than hacking the page protection to turn off COPY_ON_WRITE - Which I'm not sure is even possible. The only way I can think to do this would be to write a little program that runs after your exe finishes and opens the .exe file and writes to it.

I've gotta believe that whatever you are trying to do, there is a better way to go about it.

--- Later ----

Ok, I get it now. you are looking to watermark your exe. Here's the thing, this is pretty easy to do in your installer before the exe starts. But once the .exe is running it's MUCH harder to do.

Here's what I would do.

  • declare a global string variable of the necessary size, say const char g_szWatermark[100] = "";
  • Build my exe, then look in the map file to find the address of the variable within its segment (remember about C++ name decoration)
  • parse the EXE header to find the where the segment begins in the exe.
  • add these two numbers to get the location of the string within the exe file, give this number to my installer
  • have the installer run a little program that asks the user for information and then writes it into the .exe. Note: you have do do this before the exe runs or it won't work!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...