Home

VirtualProtect

VirtualProtect is a Windows API function that changes the protection on a region of virtual memory in the calling process. It is declared in Kernel32.dll. The function takes a pointer to the region's starting address (lpAddress), the size of the region in bytes (dwSize), a flag specifying the requested page protection (flNewProtect), and a pointer to a variable that receives the previous protection attributes (oldProtect). The protection flags can specify read, write, execute permissions, or no access, such as PAGE_READONLY, PAGE_READWRITE, PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, or PAGE_NOACCESS; there are also other attributes for guard pages or caching options depending on the platform.

On success, VirtualProtect changes the protection for all memory pages that intersect the specified region. The

It is also available as VirtualProtectEx to modify memory in another process's address space, requiring appropriate

region
must
be
within
the
process's
committed
virtual
memory;
attempting
to
change
protection
on
uncommitted
memory
or
an
invalid
address
fails
with
an
error
returned
by
GetLastError.
The
function
returns
nonzero
on
success
and
zero
on
failure.
privileges.
Typical
uses
include
implementing
dynamic
memory
protection
for
data,
preparing
code
pages
for
execution,
patching
or
hot-patching
code,
or
implementing
security
features
such
as
data
execution
prevention.
When
finished,
applications
should
restore
the
original
protection
as
needed.
Possible
error
codes
include
ERROR_INVALID_ADDRESS,
ERROR_ACCESS_DENIED,
or
ERROR_INVALID_PARAMETER.