문제

I need to use VirtualProtect, and my question is about the address of the region passed to the function. It says (on MSDN) "an address that describes the starting page", does it have to be the address of the beginning of the page or could it be any address in that page? In other words, should I first use VirtualQuery to determine the starting address of the page?

도움이 되었습니까?

해결책

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.

다른 팁

In either case, you wouldn't need VirtualQuery -- pages are always a multiple of PAGE_SIZE, which is normally 4 KiB. So you'd just need to round down your number to the nearest PAGE_SIZE.

But I don't believe you need to round down; I think any address will work. It might be worth double-checking though.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top