unsigned char* Data::getAddress(unsigned char* address)
{
strcpy((char*)address, (char*)this->_address);
return (unsigned char*)address;
}
int main()
{
Data d;
d.makealinkedlisthere();
while (d)
{
unsigned char address[256];
printf("0x%08x \r\n",d.getAddress(address));
d = d.getNext();
}
return 0;
}
它返回前两个(它们是相同的,它应该是不同的[可以从调试器中告诉] ...)然后崩溃。
它只是制作一个链表。受保护的成员Data* _next
......它们的链条。
unsigned char *来自Windows函数VirtualQueryEx
它返回的MEMORY_BASIC_INFORMATION
数据结构的一部分。
this->_address = (unsigned char*)meminfo->BaseAddress; // casted from void*
它是无效*,但我看到它在其他代码中转换为unsigned char *。在调试器中,我可以看到它表示为十六进制数字。
D1: + _address 0x7ffd5000 <Bad Ptr> unsigned char *
D1->_next:+ _address 0x7f6f0000 "áå•ú`©" unsigned char *
D1->_next->_next+ _address 0x7ffb0000 " " unsigned char *
答案 0 :(得分:2)
MEMORY_BASIC_INFORMATION.Base地址是页面区域的位置而不是字符串,并且没有保证它将被终止。 在MSDN网站上,您可以看到MEMORY_BASIC_INFORMATION structure
typedef struct _MEMORY_BASIC_INFORMATION {
PVOID BaseAddress;
PVOID AllocationBase;
DWORD AllocationProtect;
SIZE_T RegionSize;
DWORD State;
DWORD Protect;
DWORD Type;
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
要复制数据,您应使用大小= 255的memcpy
。
回顾您的代码,前提是没有其他问题,请将其更改为
PVOID Data::getAddress(PVOID address,size) {
memcpy((address, (void *)this->_address, size);
address[size]=NULL;
return address;
}
int main() {
Data d;
d.makealinkedlisthere();
while (d) {
unsigned char address[256];
printf("Address: 0x%08x \n",d.getAddress((PVOID)address),sizeof(address));
printf("Data: %s\n",(LPSTR)d.getAddress((PVOID)address),sizeof(address));
d = d.getNext();
}
return 0;
}
应该有效
请注意,存储RegionSize
并在memcpy
之前进行边界检查是安全的。