You shouldn't be using GetLastError unless you know there is an error (i.e ReadProcessMemory returns false). WriteProcessMemory and ReadProcessMemory expect a buffer/pointer to be passed in, and casting the variable to LPCVOID doesn't turn it into a pointer, use the & sign instead (i.e &width). when you are using OpenProcess on the same process you are injected to you should use the handle from GetCurrentProcess and cast it to DWORD and use GetCurrentProcess when asked for a handle to read or write memory.