Win32 API 日本語リファレンス
ホームSystem.Memory › FlushViewOfFile

FlushViewOfFile

関数
マップされたビューの変更内容をディスク上のファイルへ書き込む。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll
#include <windows.h>

BOOL FlushViewOfFile(
    const void* lpBaseAddress,
    UINT_PTR dwNumberOfBytesToFlush
);

パラメーター

名前方向説明
lpBaseAddressvoid*inマップされたファイルのディスク表現にフラッシュするバイト範囲のベースアドレスへのポインターです。
dwNumberOfBytesToFlushUINT_PTRinフラッシュするバイト数です。dwNumberOfBytesToFlush が 0 の場合、ベースアドレスからマッピングの末尾までがフラッシュされます。

戻り値の型: BOOL

公式ドキュメント

ファイルのマップされたビュー内の指定したバイト範囲をディスクに書き込みます。

戻り値

関数が成功すると、戻り値は 0 以外になります。

関数が失敗すると、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

マップされたビューの範囲をフラッシュすると、その範囲内のダーティページのディスクへの書き込みが開始されます。ダーティページとは、ファイルビューがマップされてから内容が変更されたページのことです。FlushViewOfFile 関数はファイルのメタデータをフラッシュせず、また、変更が基盤となるハードウェアのディスクキャッシュからフラッシュされて物理的にディスクへ書き込まれるまで待機して戻ることもありません。すべてのダーティページに加えてファイルのメタデータをフラッシュし、それらが物理的にディスクへ書き込まれることを保証するには、FlushViewOfFile を呼び出した後に FlushFileBuffers 関数を呼び出します。

メモリマップトファイルをネットワーク経由でフラッシュする場合、 FlushViewOfFile はデータがローカルコンピューターから書き込まれたことは保証しますが、そのデータがリモートコンピューター上に存在することは保証しません。サーバーはリモート側でデータをキャッシュする場合があります。したがって、 FlushViewOfFile はデータが物理的にディスクへ書き込まれる前に戻ることがあります。

マップされたビューを通じてファイルを変更する場合、最終変更タイムスタンプが自動的に更新されないことがあります。必要に応じて、呼び出し元は SetFileTime を使用してタイムスタンプを設定してください。

Windows Server 2012 では、この関数は次のテクノロジでサポートされています。

テクノロジ サポート
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 トランスペアレントフェールオーバー (TFO) はい
SMB 3.0 とスケールアウトファイル共有 (SO) はい
クラスター共有ボリュームファイルシステム (CsvFS) はい
回復性ファイルシステム (ReFS) はい

CsvFs が一時停止されている場合、この呼び出しはロック競合を示すエラーで失敗することがあります。

例については、 Reading and Writing From a File View を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

BOOL FlushViewOfFile(
    const void* lpBaseAddress,
    UINT_PTR dwNumberOfBytesToFlush
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FlushViewOfFile(
    IntPtr lpBaseAddress,   // void*
    UIntPtr dwNumberOfBytesToFlush   // UINT_PTR
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FlushViewOfFile(
    lpBaseAddress As IntPtr,   ' void*
    dwNumberOfBytesToFlush As UIntPtr   ' UINT_PTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpBaseAddress : void*
' dwNumberOfBytesToFlush : UINT_PTR
Declare PtrSafe Function FlushViewOfFile Lib "kernel32" ( _
    ByVal lpBaseAddress As LongPtr, _
    ByVal dwNumberOfBytesToFlush As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

FlushViewOfFile = ctypes.windll.kernel32.FlushViewOfFile
FlushViewOfFile.restype = wintypes.BOOL
FlushViewOfFile.argtypes = [
    ctypes.POINTER(None),  # lpBaseAddress : void*
    ctypes.c_size_t,  # dwNumberOfBytesToFlush : UINT_PTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
FlushViewOfFile = Fiddle::Function.new(
  lib['FlushViewOfFile'],
  [
    Fiddle::TYPE_VOIDP,  # lpBaseAddress : void*
    Fiddle::TYPE_UINTPTR_T,  # dwNumberOfBytesToFlush : UINT_PTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn FlushViewOfFile(
        lpBaseAddress: *const (),  // void*
        dwNumberOfBytesToFlush: usize  // UINT_PTR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool FlushViewOfFile(IntPtr lpBaseAddress, UIntPtr dwNumberOfBytesToFlush);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FlushViewOfFile' -Namespace Win32 -PassThru
# $api::FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
#uselib "KERNEL32.dll"
#func global FlushViewOfFile "FlushViewOfFile" sptr, sptr
; FlushViewOfFile lpBaseAddress, dwNumberOfBytesToFlush   ; 戻り値は stat
; lpBaseAddress : void* -> "sptr"
; dwNumberOfBytesToFlush : UINT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global FlushViewOfFile "FlushViewOfFile" sptr, sptr
; res = FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
; lpBaseAddress : void* -> "sptr"
; dwNumberOfBytesToFlush : UINT_PTR -> "sptr"
; BOOL FlushViewOfFile(void* lpBaseAddress, UINT_PTR dwNumberOfBytesToFlush)
#uselib "KERNEL32.dll"
#cfunc global FlushViewOfFile "FlushViewOfFile" intptr, intptr
; res = FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
; lpBaseAddress : void* -> "intptr"
; dwNumberOfBytesToFlush : UINT_PTR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procFlushViewOfFile = kernel32.NewProc("FlushViewOfFile")
)

// lpBaseAddress (void*), dwNumberOfBytesToFlush (UINT_PTR)
r1, _, err := procFlushViewOfFile.Call(
	uintptr(lpBaseAddress),
	uintptr(dwNumberOfBytesToFlush),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function FlushViewOfFile(
  lpBaseAddress: Pointer;   // void*
  dwNumberOfBytesToFlush: NativeUInt   // UINT_PTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'FlushViewOfFile';
result := DllCall("KERNEL32\FlushViewOfFile"
    , "Ptr", lpBaseAddress   ; void*
    , "UPtr", dwNumberOfBytesToFlush   ; UINT_PTR
    , "Int")   ; return: BOOL
●FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush) = DLL("KERNEL32.dll", "bool FlushViewOfFile(void*, int)")
# 呼び出し: FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
# lpBaseAddress : void* -> "void*"
# dwNumberOfBytesToFlush : UINT_PTR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn FlushViewOfFile(
    lpBaseAddress: ?*anyopaque, // void*
    dwNumberOfBytesToFlush: usize // UINT_PTR
) callconv(std.os.windows.WINAPI) i32;
proc FlushViewOfFile(
    lpBaseAddress: pointer,  # void*
    dwNumberOfBytesToFlush: uint  # UINT_PTR
): int32 {.importc: "FlushViewOfFile", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int FlushViewOfFile(
    void* lpBaseAddress,   // void*
    size_t dwNumberOfBytesToFlush   // UINT_PTR
);
ccall((:FlushViewOfFile, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Csize_t),
      lpBaseAddress, dwNumberOfBytesToFlush)
# lpBaseAddress : void* -> Ptr{Cvoid}
# dwNumberOfBytesToFlush : UINT_PTR -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t FlushViewOfFile(
    void* lpBaseAddress,
    uintptr_t dwNumberOfBytesToFlush);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
-- lpBaseAddress : void*
-- dwNumberOfBytesToFlush : UINT_PTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const FlushViewOfFile = lib.func('__stdcall', 'FlushViewOfFile', 'int32_t', ['void *', 'uintptr_t']);
// FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
// lpBaseAddress : void* -> 'void *'
// dwNumberOfBytesToFlush : UINT_PTR -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  FlushViewOfFile: { parameters: ["pointer", "usize"], result: "i32" },
});
// lib.symbols.FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush)
// lpBaseAddress : void* -> "pointer"
// dwNumberOfBytesToFlush : UINT_PTR -> "usize"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t FlushViewOfFile(
    void* lpBaseAddress,
    size_t dwNumberOfBytesToFlush);
C, "KERNEL32.dll");
// $ffi->FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush);
// lpBaseAddress : void*
// dwNumberOfBytesToFlush : UINT_PTR
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    boolean FlushViewOfFile(
        Pointer lpBaseAddress,   // void*
        long dwNumberOfBytesToFlush   // UINT_PTR
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun FlushViewOfFile = FlushViewOfFile(
    lpBaseAddress : Void*,   # void*
    dwNumberOfBytesToFlush : LibC::SizeT   # UINT_PTR
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef FlushViewOfFileNative = Int32 Function(Pointer<Void>, UintPtr);
typedef FlushViewOfFileDart = int Function(Pointer<Void>, int);
final FlushViewOfFile = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<FlushViewOfFileNative, FlushViewOfFileDart>('FlushViewOfFile');
// lpBaseAddress : void* -> Pointer<Void>
// dwNumberOfBytesToFlush : UINT_PTR -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function FlushViewOfFile(
  lpBaseAddress: Pointer;   // void*
  dwNumberOfBytesToFlush: NativeUInt   // UINT_PTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'FlushViewOfFile';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "FlushViewOfFile"
  c_FlushViewOfFile :: Ptr () -> CUIntPtr -> IO CInt
-- lpBaseAddress : void* -> Ptr ()
-- dwNumberOfBytesToFlush : UINT_PTR -> CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let flushviewoffile =
  foreign "FlushViewOfFile"
    ((ptr void) @-> size_t @-> returning int32_t)
(* lpBaseAddress : void* -> (ptr void) *)
(* dwNumberOfBytesToFlush : UINT_PTR -> size_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("FlushViewOfFile" flush-view-of-file :convention :stdcall) :int32
  (lp-base-address :pointer)   ; void*
  (dw-number-of-bytes-to-flush :uint64))   ; UINT_PTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $FlushViewOfFile = Win32::API::More->new('KERNEL32',
    'BOOL FlushViewOfFile(LPVOID lpBaseAddress, WPARAM dwNumberOfBytesToFlush)');
# my $ret = $FlushViewOfFile->Call($lpBaseAddress, $dwNumberOfBytesToFlush);
# lpBaseAddress : void* -> LPVOID
# dwNumberOfBytesToFlush : UINT_PTR -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目