Reading memory into a string causes program crash?

Im trying to read a memory address that contains text.

However when i do
if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&datakey.race1,sizeof(datakey.race1),0)){
            datakey.race1[] = "Non";

My program just crashes on startup

The array in my struct is

char race1[3];

I have no idea what im doing here since i rarely use "char". But pretty much im just trying to get the data from that memory address into a string variable. (The actual address is different from all zero's)
Last edited on
You are trying to read memory address zero, and that's illegal. See for proper usage of the ReadProcessMemory() function.
#1 This

My program just crashes on startup

makes is sound like your program crashes immediately after launch. Before it could have gotten to your code. I take it this is not the case?

#2 If you want to set a char buffer to "Non" then you need

memcpy(datakey.race1, "Non", 3);


datakey.race1[0] = 'N';
datakey.race1[1] = 'o';
datakey.race1[2] = 'n';

Note: that if you want to display "Non" as a C-string, you need to add space for a null terminator. And then you can use strcpy() instead of memcpy()

#3 Is the string you're trying to copy null terminated?


PS changing the example address to something other than zero might space some confusion? or use a variable (e.g. mem_addr?)
Last edited on
If "datakey.race1" is an array then you shouldn't have to pass it by reference. This function only needs a pointer that is cast to void, not a pointer to a pointer.

Isn't it the address-of operator in this context, rather than a reference?
@ andywestken: Yeah, it would mean the OP is passing the pointer to the pointer of the beginning of the array. Did I write that wrong? It's one of those things I have trouble putting into words. The OP still needs to delete it right?
Last edited on
Write it wrong? No.

I don't think delete is needed here. ReadProcessMemory will copy into the memory provided (the 3 bytes in the race1 array) if the & is lost (or &datakey.race1[0] is used)

But I think you're right that the address is needed rather than the address of the address.
Last edited on
Topic archived. No new replies allowed.