Function Reference


_WinAPI_UnmapViewOfFile

Unmaps a mapped view of a file from the calling process's address space

#include <WinAPIFiles.au3>
_WinAPI_UnmapViewOfFile ( $pAddress )

Parameters

$pAddress A pointer to the base address of the mapped view of a file that is to be unmapped.
This value must be identical to the value returned by a previous call to the _WinAPI_MapViewOfFile() function.

Return Value

Success: True
Failure: False

Remarks

Unmapping a mapped view of a file invalidates the range occupied by the view in the address space of the process and makes the range available for other allocations.
It removes the working set entry for each unmapped virtual page that was part of the working set of the process and reduces the working set size of the process.
It also decrements the share count of the corresponding physical page.

Although an application may close the file handle used to create a file mapping object,
the system holds the corresponding file open until the last view of the file is unmapped.
Files for which the last view has not yet been unmapped are held open with no sharing restrictions.

To minimize the risk of data loss in the event of a power failure or a system crash,
you should explicitly flush modified pages using the _WinAPI_FlushViewOfFile() function.

Related

_WinAPI_FlushViewOfFile, _WinAPI_MapViewOfFile

See Also

Search UnmapViewOfFile in MSDN Library.

Example

#NoTrayIcon

#include <MsgBoxConstants.au3>
#include <WinAPI.au3>
#include <WinAPIFiles.au3>

Opt('WinWaitDelay', 0)

Global Const $g_sTitle = '_WinAPI_MapViewOfFile' & ChrW(160)

If Not $CmdLine[0] Then
    If WinExists($g_sTitle) Then
        Exit
    EndIf
    For $i = 1 To 2
        If Not @Compiled Then
            Run(@AutoItExe & ' "' & @ScriptFullPath & '" /' & $i)
        Else
            Run(@AutoItExe & ' /' & $i)
        EndIf
        Sleep(500)
    Next
    Exit
EndIf

Opt('TrayIconHide', 0)

Switch $CmdLine[1]
    Case '/1'
        _Sender()
    Case '/2'
        _Receiver()
    Case Else
        Exit
EndSwitch

Func _Receiver()
    Local $hMapping = _WinAPI_OpenFileMapping('MyFileMapping')
    If Not $hMapping Then Return

    Local $pAddress = _WinAPI_MapViewOfFile($hMapping)
    Local $tData = DllStructCreate('wchar[1024]', $pAddress)
    Local $sText
    While WinWait($g_sTitle, '', 1)
        Sleep(200)
        $sText = DllStructGetData($tData, 1)
        DllStructSetData($tData, 1, '')
        If $sText Then MsgBox(BitOR($MB_ICONINFORMATION, $MB_SYSTEMMODAL), $g_sTitle & " (receiver)", "                                               " & @CRLF & $sText)
    WEnd
    _WinAPI_UnmapViewOfFile($pAddress)
    _WinAPI_CloseHandle($hMapping)
EndFunc   ;==>_Receiver

Func _Sender()
    Local $hMapping = _WinAPI_CreateFileMapping(-1, 2048, 'MyFileMapping')
    If Not $hMapping Or @extended Then
        MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Unable to create file mapping (@extended=' & @extended & ').')
        Return
    EndIf

    Local $pAddress = _WinAPI_MapViewOfFile($hMapping)
    Local $tData = DllStructCreate('wchar[1024]', $pAddress)
    Local $sText
    While WinWaitClose($g_sTitle)
        $sText = StringStripWS(InputBox($g_sTitle & " (sender)", 'Type some text message.', '', '', -1, 171), 3)
        If Not $sText Then
            ExitLoop
        EndIf
        DllStructSetData($tData, 1, $sText)
        If Not WinWait($g_sTitle, '', 1) Then
            ExitLoop
        EndIf
    WEnd
    _WinAPI_UnmapViewOfFile($pAddress)
    _WinAPI_CloseHandle($hMapping)
EndFunc   ;==>_Sender