2

我需要使用 VirtualProtect,我的问题是关于传递给函数的区域地址。它说(在 MSDN 上)“描述起始页面的地址”,它必须是页面开头的地址还是该页面中的任何地址?也就是说,我应该先用VirtualQuery来确定页面的起始地址吗?

4

2 回答 2

3

You don't need to pass in the base address of the page. VirtualProtect will accept any address within the page. The description of the dwSize parameter makes that clear:

The region of affected pages includes all pages containing one or more bytes in the range from the lpAddress parameter to (lpAddress+dwSize). This means that a 2-byte range straddling a page boundary causes the protection attributes of both pages to be changed.

f you're able to have a two-byte range that straddles a page boundary, then it must be possible for lpAddress to be just one byte before the end of a page. Pages can't be just one byte long, so it's not at the start of a page.

于 2012-05-31T19:02:28.867 回答
1

无论哪种情况,您都不需要VirtualQuery- 页面始终是 的倍数PAGE_SIZE,通常为 4 KiB。因此,您只需要将您的数字四舍五入到最接近的PAGE_SIZE.

但我不认为你需要四舍五入;我认为任何地址都可以。不过,这可能值得仔细检查。

于 2012-05-31T18:11:20.073 回答