I'm trying to read a process memory using the following code:
void readdata(HANDLE phandle, LPCVOID paddress, SIZE_T datasize)
{
char *buff;
SIZE_T dataread;
BOOL b = FALSE;
buff = (char *) malloc (datasize);
b = ReadProcessMemory(phandle, paddress, (LPVOID)buff, datasize, &dataread);
if(!b)
{
printf("error reading memory, err = %d\n", GetLastError());
return;
}
printf("Data Read = %d\n", dataread);
printf("Len of actual buffer = %d\n", strlen(buff));
printf("Data = %s\n", buff);
free(buff);
return;
}
Now, phandle and paddress are known becuase I used WriteProcessMemory. I have the values from there. datasize is also known.
The function works ok, except for the following. ReadProcessMemory() returns dataread = 41 (which is correct, I passed 41 to datasize) but the actual length of the buff is 49. when I print buff i get my string + some garbage.
What am I doing wrong?
code is appreciated.
Thanks!