Writing char to adress

10/28/2013 18:56 XxharCs#1
Hey, after a long time i started again with memory editing and i stucked at one point.

I dont wanna use WriteProcessMemory to write new chars/intīs and so on, to an address. (from a dll)

Normally with a number it works..
example:
Code:
DWORD BasePointer = 0x006FA108;
...
...
DWORD dwBasePointer = *(DWORD*)BasePointer;
*(DWORD*)(dwBasePointer) = 2;

but when i try to do the same, but with a char, it doesnīt work:
Code:
char* helVal= "Hello";
LPVOID dwBasePointer = (DWORD*)0x006FA108;

dwBasePointer = helVal;
am i forgetting something or what?
if this method doesnīt work (with chars), then i am going to work with WriteProcessMemory to write chars..

Thanks!
10/28/2013 18:58 kingdeking#2
memcpy, strcpy
10/28/2013 19:07 XxharCs#3
ah thanks, forgot that there is also a memcpy :)

can be closed
10/28/2013 19:14 MrSm!th#4
You don't understand pointers correctly, I think.


Code:
//x86 compatible only, haters gonna hate

DWORD *dwPtr = (DWORD *)0xDEADBEEF;   //writing 0xDEADBEEF into the variable dwPtr (or: at address of dwPtr) 
*dwPtr = 5;    //writing 5 at address 0xDEADBEEF

const char *cstrVal = "C-Style-String";

const char *strPtr = (const char *)0xDEADBEEF;   //writing 0xDEADBEEF into the variable strPtr (or: at address of strPtr)

strPtr = cstrVal;    //writing the address of "C-Style-String" into the variable strPtr (or: at the address of strPtr) - NOT writing the content ("C-Style-String") at the address saved in strPtr (0xDEADBEEF)
A C-Style string is simply a pointer to a char-Array or rather to the first element of that array. The pointer works exactly the same way an int* does.

Code:
char *strPtr = (char *)0xDEADBEEF; //now leaving out const to make the ptr writeable
*strPtr = *cstrVal; //writing the 'C', the first character of the string pointed to by cstrVal (because it is actually a pointer to that one char) at 0xDEADBEEF
You wouldn't think that
Code:
DWORD vals[] = { 1, 2, 3 };

dwPtr = vals;
//or
*dwPtr = *vals;
would copy the whole DWORD array either, would you?
Of course not. You need a loop for that. Luckily, there is already a well-optimized solution for that provided by the standard library: memcpy/strcpy/strcpy_s.


Btw.

Code:
char *helVal = "Hello";
Your compiler should complain about that line, because you are implicitly converting a const char* into a non-const char* there, which is not possible.


Anyway, #closed