保护页异常-如何引发

Guard Page Exception - how to raise

本文关键字:何引发 异常 保护      更新时间:2023-10-16

使用我们的API的客户会得到一个保护页异常。他使用VirtualAlloc和VirtualProtect。

当我以他为榜样时,一切都很好。

我从Microsoft尝试了这个例子,但即使我已经在"Debug"下的异常菜单中打开了它,VisualStudio也不会抛出0x80000001异常。但这个例子清楚地表明:

第一次尝试锁定内存块失败,引发STATUS_GUARD_PAGE_VIOLATION异常。

我需要做什么才能获得该异常?

编辑:

客户这样做:

SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
DWORD dwPageSize = systemInfo.dwPageSize;
size_t size = width * height * sizeof(MyStruct); 
while(size % dwPageSize) 
{
    height--;
    size = width * height * sizeof(MyStruct); 
}
size_t dataSize = size + dwPageSize;
MyStruct * my_struct = (MyStruct*)VirtualAlloc(NULL, dataSize, MEM_COMMIT | MEM_RESERVE , PAGE_READWRITE);
if (!my_struct) return;
LPVOID beginGuard = (char*)my_struct + size;
DWORD oldProtection;
BOOL b = VirtualProtect(beginGuard, dwPageSize, PAGE_READWRITE | PAGE_GUARD, &oldProtection);
if(!b) MessageBox(NULL, "Can't set guard page", "", 0);
doSomething(); // some API function

在"doSomething()"中的某个位置,会抛出上述异常。但我不能帮助那个客户,因为我没有例外。

若要引发代码为0x80000001的异常,您需要尝试访问使用PAGE_GUARD标志分配和保护的内存。写一些类似的东西

MyStruct foo = my_struct[0];

并且将引发异常。

就MSDN中的示例而言,您可以在该帖子的评论中看到它的解释是不正确的,因为VirtualLock没有引发异常。