/// <summary>
/// Allocate new memory block
/// </summary>
/// <param name="process">Process memory routines</param>
/// <param name="size">Block size</param>
/// <param name="desired">Desired base address of new block</param>
/// <param name="protection">Memory protection</param>
/// <returns>Memory block. If failed - returned block will be invalid</returns>
MemBlock MemBlock::Allocate( ProcessMemory& process, size_t size, ptr_t desired /*= 0*/, DWORD protection /*= PAGE_EXECUTE_READWRITE */ )
{
    ptr_t desired64 = desired;
    DWORD newProt = CastProtection( protection, process.core().DEP() );
    
    if (process.core().native()->VirualAllocExT( desired64, size, MEM_COMMIT, newProt ) != STATUS_SUCCESS)
    {
        desired64 = 0;
        if (process.core().native()->VirualAllocExT( desired64, size, MEM_COMMIT, newProt ) == STATUS_SUCCESS)
            LastNtStatus( STATUS_IMAGE_NOT_AT_BASE );
        else
            desired64 = 0;
    }

    return MemBlock( &process, desired64, size, protection );
}
Exemple #2
0
/// <summary>
/// Allocate new memory block
/// </summary>
/// <param name="process">Process memory routines</param>
/// <param name="size">Block size</param>
/// <param name="desired">Desired base address of new block</param>
/// <param name="protection">Memory protection</param>
/// <param name="own">false if caller will be responsible for block deallocation</param>
/// <returns>Memory block. If failed - returned block will be invalid</returns>
call_result_t<MemBlock> MemBlock::Allocate( ProcessMemory& process, size_t size, ptr_t desired /*= 0*/, DWORD protection /*= PAGE_EXECUTE_READWRITE */, bool own /*= true*/ )
{
    ptr_t desired64 = desired;
    DWORD newProt = CastProtection( protection, process.core().DEP() );
    
    NTSTATUS status = process.core().native()->VirtualAllocExT( desired64, size, MEM_COMMIT, newProt );
    if (!NT_SUCCESS( status ))
    {
        desired64 = 0;
        status = process.core().native()->VirtualAllocExT( desired64, size, MEM_COMMIT, newProt );
        if (NT_SUCCESS( status ))
            return call_result_t<MemBlock>( MemBlock( &process, desired64, size, protection, own ), STATUS_IMAGE_NOT_AT_BASE );
        else
            return status;
    }

    return MemBlock( &process, desired64, size, protection, own );
}