简体   繁体   English

从LPVOID减去

[英]Subtracting from LPVOID

Not very prof. 不是很教授。 with C++. 使用C ++。 The code I've been working on is : https://msdn.microsoft.com/en-us/library/windows/desktop/ee175819(v=vs.85).aspx 我一直在处理的代码是: https : //msdn.microsoft.com/zh-cn/library/windows/desktop/ee175819(v=vs.85).aspx

My problem resides in this area : 我的问题出在这方面:

_tprintf(TEXT("  Data portion begins at: %#p\n  Size: %d bytes\n") \
             TEXT("  Overhead: %d bytes\n  Region index: %d\n\n"),
             Entry.lpData,
             Entry.cbData,
             Entry.cbOverhead,
             Entry.iRegionIndex);
}

The problem I'm facing is, The Entry.lpData is the address of data portion of heap block. 我面临的问题是, Entry.lpData是堆块的数据部分的地址。 I want to read 8 byte before Entry.lpData address. 我想在Entry.lpData地址之前读取8个字节。 So when I'm simply subtracting 8 from Entry.lpData and trying to read bytes, I'm getting the error 所以当我简单地从Entry.lpData中减去8并尝试读取字节时,我得到了错误

hexDump(entry.lpData - 8, 8);


heapwalk.cpp(119): error C2036: 'PVOID' : unknown size

Pointers to void are pointers to anything , so it makes no sense to perform pointer arithmetic on them directly. 指向void的指针是指向任何东西的指针,因此直接对它们执行指针算术没有意义。 In this case, since you know you need an 8 byte offset, you will simply cast it to a char* first. 在这种情况下,由于您知道需要8个字节的偏移量,因此只需将其首先转换为char* In the general case you would know what sort of data it actually points to and cast it to a pointer of that type. 在一般情况下,您会知道它实际指向哪种数据并将其强制转换为该类型的指针。

char *p = static_cast<char*>(entry.lpData) - 8;

This works because char* is an exception to the strict aliasing rule . 之所以char*是因为char*严格别名规则的例外。 Don't try this with arbitrary types. 请勿尝试使用任意类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM