警卫页面例外 - 如何筹集

时间:2015-12-01 08:03:51

标签: c++ visual-studio-2010 exception

使用我们的API的客户会获得一个防护页面例外。 他使用VirtualAlloc和VirtualProtect。

当我运行他的例子时,一切正常。

我尝试了Microsoft的this示例,但VisualStudio不会抛出0x80000001异常,即使我已在' Debug'下的异常菜单中将其打开。 但这个例子明确指出:

  

锁定内存块的第一次尝试失败,引发了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

在某些地方做某事()'提到的异常被抛出。但是我无法帮助那个客户,因为我没有得到那个例外。

1 个答案:

答案 0 :(得分:0)

要使用代码0x80000001引发异常,您需要尝试访问使用PAGE_GUARD标志分配和保护的内存。写点像

MyStruct foo = my_struct[0];

将引发异常。

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

相关问题