String + ReadProcessMemory = Access Violation?

I'm trying to understand why this code gives me an access violation.
1
2
3
4
5
6
string GetZone(HANDLE phandle)
{
	string zoneName;
	ReadProcessMemory(phandle,(void*)ZONEADDR,&zoneName,sizeof(zoneName),0);
	return zoneName;
}


And this code does not.
1
2
3
4
5
6
string GetZone(HANDLE phandle)
{
	char zoneName[20];
	ReadProcessMemory(phandle,(void*)ZONEADDR,&zoneName,sizeof(zoneName),0);
	return zoneName;
}


Thanks in advance!
Last edited on
A string is a non-POD and contains pointer(s) internally. Any attempt to dereference a pointer read in such a way is bound to end in disaster, as the address refers to the address space of the other process.
And even that assumes that what you're trying to read is actually a std::string and not a C string.
Ty for your help!
Topic archived. No new replies allowed.