Jump to content
Sign in to follow this  

Using the CreateFileMapping function with an Offset

Recommended Posts

Yashieds most excellent WinAPIEx UDF is required for this example.

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

    CreateFileMapping function
    MapViewOfFile function
    File Mapping

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)
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

    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.

$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)

#region - Read file by _WinAPI_CreateFileMapping - End

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...