You can't write non-POD types to memory for the same reason that you can't write them to a file.
Consider the following:
1 2 3 4 5 6 7 8
|
std::string small = "foo";
std::string big = "A very long string that contains a lot more data. Logically"
" you would think this string would take more space in memory because"
" it contains a lot more characters. But take a look at this example and"
" maybe you'll see what I mean.";
cout << sizeof(small) << endl; // prints the size of the small string
cout << sizeof(big) << endl; // prints the size of the big string
|
You might be surprised to know that sizeof(small) and sizeof(big) are
EXACTLY THE SAME. They're both just giving you the size of the std::string class.
But how can this be? One is obviously larger than the other, right?
The thing is... the std::string does not actually contain the string data. It contains a
POINTER TO the string data. So when you attempt to write a string object to memory like you're doing... you're not actually writing the string, you're just writing an arbitrary pointer.
Clearly... this will not work.
What you need to do instead is get a pointer to the actual string data, and write THAT data to memory. You can get this with the c_str member function.
So instead of this:
WriteProcessMemory(phandle,(void*)(Base_address+offset3),&New_Name,sizeof(New_Name),0);
You can do this:
WriteProcessMemory(phandle,(void*)(Base_address+offset3),New_Name.c_str(),New_Name.length(),0);
Note the 2 key differences here:
1) using c_str() to get a pointer to the string data
2) using length() to get the length of the string, rather than sizeof() to get the size of the std::string object.
However... this is still problematic because the length of the string is variable... and therefore it will be impossible to read this string back from memory unless you first know the length of it.
Therefore, you have 2 options:
1) Write an integer value to memory first to indicate the length of the string.
or
2) Write an additional '\0' character after the string data to indicate the end of the string.
I highly recommend approach #1, as it not only makes the string easier to read back, but it also is more "solid" than approach #2 (#2 will fail, for example, if the string you want to write already contains null characters).