Search code examples
winapiseh

Write to a memory buffer and check if it is full with a SEH exception on Win32 platform?


Is it possible to allocate a memory buffer of about 64KB size with Win32 API and have some kind of a "guard zone" (I do not know the correct term for that) after that buffer so C++ code will write to the buffer and throw a SEH exception if the buffer is full and it attempts to write to the "guard zone"?


Solution

  • Yes. Use VirtualAlloc. With VirutalProtect used in conjunction, you can even make this guard zone only memory page sized (4096 bytes, not whole 64KB).

    Note that for stack it is handled by OS, you can only control this by SetThreadStackGuarantee to set the amount of buffer before stack overflow, and _resetstkoflw to fix (restore) guard after stack overflow exception is hadnled.