Partager via


VirtualAllocEx (Compact 2013)

3/28/2014

This function reserves or commits a region of pages in the virtual address space of the process specified by the hProcess parameter. Memory allocated by VirtualAllocEx is initialized to zero.

Syntax

LPVOID VirtualAllocEx(
  HANDLE hProcess,
  LPVOID lpAddress,
  DWORD dwSize,
  DWORD flAllocationType,
  DWORD flProtect
);

Parameters

  • hProcess
    [in] Process identifier, or a process handle returned from a call to the OpenProcess function.
  • lpAddress
    [in] Long pointer to the specified starting address of the region to be allocated.

    If the memory is being reserved, the specified address is rounded down to the next 64-KB boundary.

    If the memory is reserved and is being committed, the address is rounded down to the next page boundary.

    To determine the size of a page on the host computer, use the GetSystemInfofunction.

    If this parameter is NULL, the system determines where to allocate the region.

  • dwSize
    [in] Specifies the size, in bytes, of the region. It is an error to set this parameter to zero.

    If the lpAddress parameter is NULL, this value is rounded up to the next page boundary.

    Otherwise, the allocated pages include all pages containing one or more bytes in the range from lpAddress to (lpAddress plus dwSize). This means that a 2-byte range straddling a page boundary causes both pages to be included in the allocated region.

  • flAllocationType
    [in] Specifies the type of allocation.

    For information about the accepted values for this parameter, see VirtualAlloc.

  • flProtect
    [in] Specifies the type of access protection.

    If the pages are being committed, you can specify any of the following flags, along with the PAGE_GUARD and PAGE_NOCACHE protection modifier flags.

    For information about the accepted values for this parameter, see VirtualAlloc.

Return Value

The base address of the allocated region of pages indicates success. NULL indicates failure. To get extended error information, call GetLastError.

Remarks

After reserving and committing a page in a client process with the following code:

LPBYTE p = (LPBYTE) VirtualAllocEx(hClientProc, NULL, 0x1000, MEM_COMMIT, PAGE_READWRITE);

You cannot simply write to *p with the following code:

*p = whatever;

You cannot write to *p directly because the address in the client process's virtual memory is not directly accessible. To read/write to the memory pointed by the pointer returned, use ReadProcessMemory and WriteProcessMemory. For example:

WriteProcessMemory(hClientProc, p, &whatever, 1, &cbReturned);

For information about this function, see VirtualAlloc.

Requirements

Header

winbase.h

Library

coredll.lib

See Also

Reference

Memory Management Functions
GetSystemInfo
VirtualFreeEx
VirtualAlloc
VirtualCopyEx
VirtualAllocCopyEx
OpenProcess