Win32 API 日本語リファレンス
ホームSystem.Diagnostics.Debug › FlushInstructionCache

FlushInstructionCache

関数
指定プロセスの命令キャッシュをフラッシュする。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL FlushInstructionCache(
    HANDLE hProcess,
    const void* lpBaseAddress,   // optional
    UINT_PTR dwSize
);

パラメーター

名前方向説明
hProcessHANDLEin命令キャッシュをフラッシュする対象プロセスのハンドル。
lpBaseAddressvoid*inoptionalフラッシュする領域の先頭へのポインター。このパラメーターには NULL を指定できます。
dwSizeUINT_PTRinlpBaseAddress パラメーターが NULL でない場合の、フラッシュする領域のサイズ(バイト単位)。

戻り値の型: BOOL

公式ドキュメント

指定したプロセスの命令キャッシュをフラッシュします。

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

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

解説(Remarks)

メモリ内でコードを生成または変更するアプリケーションは、 FlushInstructionCache を呼び出す必要があります。CPU は変更を検出できず、キャッシュした古いコードを実行する場合があります。

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

各言語での呼び出し定義

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

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

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

lib = Fiddle.dlopen('KERNEL32.dll')
FlushInstructionCache = Fiddle::Function.new(
  lib['FlushInstructionCache'],
  [
    Fiddle::TYPE_VOIDP,  # hProcess : HANDLE
    Fiddle::TYPE_VOIDP,  # lpBaseAddress : void* optional
    Fiddle::TYPE_UINTPTR_T,  # dwSize : UINT_PTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn FlushInstructionCache(
        hProcess: *mut core::ffi::c_void,  // HANDLE
        lpBaseAddress: *const (),  // void* optional
        dwSize: 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 FlushInstructionCache(IntPtr hProcess, IntPtr lpBaseAddress, UIntPtr dwSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FlushInstructionCache' -Namespace Win32 -PassThru
# $api::FlushInstructionCache(hProcess, lpBaseAddress, dwSize)
#uselib "KERNEL32.dll"
#func global FlushInstructionCache "FlushInstructionCache" sptr, sptr, sptr
; FlushInstructionCache hProcess, lpBaseAddress, dwSize   ; 戻り値は stat
; hProcess : HANDLE -> "sptr"
; lpBaseAddress : void* optional -> "sptr"
; dwSize : UINT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global FlushInstructionCache "FlushInstructionCache" sptr, sptr, sptr
; res = FlushInstructionCache(hProcess, lpBaseAddress, dwSize)
; hProcess : HANDLE -> "sptr"
; lpBaseAddress : void* optional -> "sptr"
; dwSize : UINT_PTR -> "sptr"
; BOOL FlushInstructionCache(HANDLE hProcess, void* lpBaseAddress, UINT_PTR dwSize)
#uselib "KERNEL32.dll"
#cfunc global FlushInstructionCache "FlushInstructionCache" intptr, intptr, intptr
; res = FlushInstructionCache(hProcess, lpBaseAddress, dwSize)
; hProcess : HANDLE -> "intptr"
; lpBaseAddress : void* optional -> "intptr"
; dwSize : UINT_PTR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procFlushInstructionCache = kernel32.NewProc("FlushInstructionCache")
)

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

extern "kernel32" fn FlushInstructionCache(
    hProcess: ?*anyopaque, // HANDLE
    lpBaseAddress: ?*anyopaque, // void* optional
    dwSize: usize // UINT_PTR
) callconv(std.os.windows.WINAPI) i32;
proc FlushInstructionCache(
    hProcess: pointer,  # HANDLE
    lpBaseAddress: pointer,  # void* optional
    dwSize: uint  # UINT_PTR
): int32 {.importc: "FlushInstructionCache", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int FlushInstructionCache(
    void* hProcess,   // HANDLE
    void* lpBaseAddress,   // void* optional
    size_t dwSize   // UINT_PTR
);
ccall((:FlushInstructionCache, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t),
      hProcess, lpBaseAddress, dwSize)
# hProcess : HANDLE -> Ptr{Cvoid}
# lpBaseAddress : void* optional -> Ptr{Cvoid}
# dwSize : UINT_PTR -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t FlushInstructionCache(
    void* hProcess,
    void* lpBaseAddress,
    uintptr_t dwSize);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.FlushInstructionCache(hProcess, lpBaseAddress, dwSize)
-- hProcess : HANDLE
-- lpBaseAddress : void* optional
-- dwSize : UINT_PTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const FlushInstructionCache = lib.func('__stdcall', 'FlushInstructionCache', 'int32_t', ['void *', 'void *', 'uintptr_t']);
// FlushInstructionCache(hProcess, lpBaseAddress, dwSize)
// hProcess : HANDLE -> 'void *'
// lpBaseAddress : void* optional -> 'void *'
// dwSize : UINT_PTR -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  FlushInstructionCache: { parameters: ["pointer", "pointer", "usize"], result: "i32" },
});
// lib.symbols.FlushInstructionCache(hProcess, lpBaseAddress, dwSize)
// hProcess : HANDLE -> "pointer"
// lpBaseAddress : void* optional -> "pointer"
// dwSize : UINT_PTR -> "usize"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t FlushInstructionCache(
    void* hProcess,
    void* lpBaseAddress,
    size_t dwSize);
C, "KERNEL32.dll");
// $ffi->FlushInstructionCache(hProcess, lpBaseAddress, dwSize);
// hProcess : HANDLE
// lpBaseAddress : void* optional
// dwSize : 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 FlushInstructionCache(
        Pointer hProcess,   // HANDLE
        Pointer lpBaseAddress,   // void* optional
        long dwSize   // UINT_PTR
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun FlushInstructionCache = FlushInstructionCache(
    hProcess : Void*,   # HANDLE
    lpBaseAddress : Void*,   # void* optional
    dwSize : 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 FlushInstructionCacheNative = Int32 Function(Pointer<Void>, Pointer<Void>, UintPtr);
typedef FlushInstructionCacheDart = int Function(Pointer<Void>, Pointer<Void>, int);
final FlushInstructionCache = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<FlushInstructionCacheNative, FlushInstructionCacheDart>('FlushInstructionCache');
// hProcess : HANDLE -> Pointer<Void>
// lpBaseAddress : void* optional -> Pointer<Void>
// dwSize : UINT_PTR -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function FlushInstructionCache(
  hProcess: THandle;   // HANDLE
  lpBaseAddress: Pointer;   // void* optional
  dwSize: NativeUInt   // UINT_PTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'FlushInstructionCache';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let flushinstructioncache =
  foreign "FlushInstructionCache"
    ((ptr void) @-> (ptr void) @-> size_t @-> returning int32_t)
(* hProcess : HANDLE -> (ptr void) *)
(* lpBaseAddress : void* optional -> (ptr void) *)
(* dwSize : 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 ("FlushInstructionCache" flush-instruction-cache :convention :stdcall) :int32
  (h-process :pointer)   ; HANDLE
  (lp-base-address :pointer)   ; void* optional
  (dw-size :uint64))   ; UINT_PTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $FlushInstructionCache = Win32::API::More->new('KERNEL32',
    'BOOL FlushInstructionCache(HANDLE hProcess, LPVOID lpBaseAddress, WPARAM dwSize)');
# my $ret = $FlushInstructionCache->Call($hProcess, $lpBaseAddress, $dwSize);
# hProcess : HANDLE -> HANDLE
# lpBaseAddress : void* optional -> LPVOID
# dwSize : UINT_PTR -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。