我可以检查内存块是否可读而不会引发C ++异常吗?

时间:2013-08-23 04:20:54

标签: c++ windows winapi memory-management

我需要以下C ++代码中的异常处理程序。说,我有以下代码块:

void myFunction(LPCTSTR pStr, int ncbNumCharsInStr)
{
    __try
    {
        //Do work with 'pStr'

    }
    __except(1)
    {
        //Catch all

        //But here I need to log `pStr` into event log
        //For that I don't want to raise another exception
        //if memory block of size `ncbNumCharsInStr` * sizeof(TCHAR)
        //pointed by 'pStr' is unreadable.
        if(memory_readable(pStr, ncbNumCharsInStr * sizeof(TCHAR)))
        {
            Log(L"Failed processing: %s", pStr);
        }
        else
        {
            Log(L"String at 0x%X, %d chars long is unreadable!", pStr, ncbNumCharsInStr);
        }
    }
}

有没有办法实施memory_readable

2 个答案:

答案 0 :(得分:3)

VirtualQuery功能可能会有所帮助。以下是快速了解如何使用它来实现memory_readable

bool memory_readable(void *ptr, size_t byteCount)
{
  MEMORY_BASIC_INFORMATION mbi;
  if (VirtualQuery(ptr, &mbi, sizeof(MEMORY_BASIC_INFORMATION)) == 0)
    return false;

  if (mbi.State != MEM_COMMIT)
    return false;

  if (mbi.Protect == PAGE_NOACCESS || mbi.Protect == PAGE_EXECUTE)
    return false;

  // This checks that the start of memory block is in the same "region" as the
  // end. If it isn't you "simplify" the problem into checking that the rest of 
  // the memory is readable.
  size_t blockOffset = (size_t)((char *)ptr - (char *)mbi.AllocationBase);
  size_t blockBytesPostPtr = mbi.RegionSize - blockOffset;

  if (blockBytesPostPtr < byteCount)
    return memory_readable((char *)ptr + blockBytesPostPtr,
                           byteCount - blockBytesPostPtr);

  return true;
}

注意:我的背景是C,所以虽然我怀疑有更好的选择,而不是在C ++中转换为char *,但我不确定它们是什么。

答案 1 :(得分:0)

您可以使用ReadProcessMemory功能。如果函数返回0,则该地址不可读,否则可读。

  

返回值

     

如果函数失败,则返回值为0(零)。扩展   错误信息,请致电GetLastError

     

如果请求的读取操作进入   进程中无法访问的区域。

     

如果函数成功,则返回值为非零。