1

I have a Dll which I inject into my target process. This Dll changes some variables at give memory addresses. Normaly I do it like:

  int* variable = (int*)0x????????;
  *variable = 1;

Now I want to do the same thing with a string. Reading the string works.

string* variable = (string*) 0x????????;

But changing the string crashes the process.

*variable = "hello world"; //crash

How can I fix this?

nice
  • 17
  • 6

2 Answers2

0

You have not shown how you guarantee that the memory at the address 0x???????? is allocated properly or that the string object is initialized.

If the pointer does not point to allocated memory, or if the string is not initialized when you copy-assign it, then you get undefined behaviour. Possible undefined behaviour include:

But changing the string crashes the process.

To fix it, allocate and initialize a string object and use the address of that string.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

You cannot use std::string or any other non POD data type this way. What you need to do instead:

 char* variable = (char*) 0x????????;
 strcpy( variable, "hello world" );

or other, maybe MS specific functions to manipulate C strings.

Community
  • 1
  • 1
Slava
  • 43,454
  • 1
  • 47
  • 90