Jump to content

Using the CreateFileMapping function with an Offset


KaFu
 Share

Recommended Posts

Yashieds most excellent WinAPIEx UDF is required for this example.

#include <APIConstants.au3>
#include <WinAPIEx.au3>

#cs
    http://www.catch22.net/tuts/memory-techniques-part-1
    
    CreateFileMapping function
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537(v=vs.85).aspx
    MapViewOfFile function
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761(v=vs.85).aspx
    File Mapping
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa366556(v=vs.85).aspx
#ce


Global $nBytes
$sFile = @SystemDir & "shell32.dll"

$iBytesToRead = 32
$iOffset = 235222

#region - Read file by _WinAPI_CreateFileEx() - Start

$tBuffer = DllStructCreate("byte[" & $iBytesToRead & "]")
$hFile = _WinAPI_CreateFileEx($sFile, 3, 0x80000000, 7, 0x08000000)
_WinAPI_SetFilePointerEx($hFile, $iOffset) ; use for files > 2 GB !!!
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $iBytesToRead, $nBytes)
_WinAPI_CloseHandle($hFile)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
#region - Read file by _WinAPI_CreateFileEx() - End

$tBuffer = 0
ConsoleWrite(@CRLF & "**********************" & @CRLF & @CRLF)

#region - Read file by _WinAPI_CreateFileMapping - Start

#cs
    The combination of the high and low offsets must specify an offset within the file mapping. They must also match the memory allocation granularity of the system.
    That is, the offset must be a multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function,
    which fills in the members of a SYSTEM_INFO structure.
#ce

$a_GetSystemInfo = _WinAPI_GetSystemInfo()
$i_Global_MemoryAllocationGranularity = $a_GetSystemInfo[7]

$iOffset_Slice = Round(Floor($iOffset / $i_Global_MemoryAllocationGranularity)) * $i_Global_MemoryAllocationGranularity
ConsoleWrite("$iOffset_Slice " & @TAB & @TAB & $iOffset_Slice & @CRLF)
$iOffset_Slice_Size = $iOffset - $iOffset_Slice
ConsoleWrite("$iOffset_Slice_Size " & @TAB & $iOffset_Slice_Size & @CRLF)

$hFile = _WinAPI_CreateFileEx($sFile, 3, 0x80000000, 7, 0x08000000)
$hMapping = _WinAPI_CreateFileMapping($hFile, 0, '', $PAGE_READONLY)
$pAddress = _WinAPI_MapViewOfFile($hMapping, $iOffset_Slice, $iOffset_Slice_Size + $iBytesToRead, $FILE_MAP_READ)
$tBuffer = DllStructCreate('byte[' & $iBytesToRead & ']', $pAddress + $iOffset_Slice_Size)
ConsoleWrite(DllStructGetData($tBuffer, 1) & @CRLF)
_WinAPI_UnmapViewOfFile($pAddress)
_WinAPI_CloseHandle($hMapping)
_WinAPI_CloseHandle($hFile)

#region - Read file by _WinAPI_CreateFileMapping - End
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...