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

VirtualProtectFromApp

関数
UWPアプリ向けにコミット済みメモリの保護属性を変更する。
DLLapi-ms-win-core-memory-l1-1-3.dll呼出規約winapiSetLastErrorあり対応OSWindows 10 以降

シグネチャ

// api-ms-win-core-memory-l1-1-3.dll
#include <windows.h>

BOOL VirtualProtectFromApp(
    void* Address,
    UINT_PTR Size,
    DWORD NewProtection,
    DWORD* OldProtection
);

パラメーター

名前方向
Addressvoid*in
SizeUINT_PTRin
NewProtectionDWORDin
OldProtectionDWORD*out

戻り値の型: BOOL

各言語での呼び出し定義

// api-ms-win-core-memory-l1-1-3.dll
#include <windows.h>

BOOL VirtualProtectFromApp(
    void* Address,
    UINT_PTR Size,
    DWORD NewProtection,
    DWORD* OldProtection
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError = true, ExactSpelling = true)]
static extern bool VirtualProtectFromApp(
    IntPtr Address,   // void*
    UIntPtr Size,   // UINT_PTR
    uint NewProtection,   // DWORD
    out uint OldProtection   // DWORD* out
);
<DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function VirtualProtectFromApp(
    Address As IntPtr,   ' void*
    Size As UIntPtr,   ' UINT_PTR
    NewProtection As UInteger,   ' DWORD
    <Out> ByRef OldProtection As UInteger   ' DWORD* out
) As Boolean
End Function
' Address : void*
' Size : UINT_PTR
' NewProtection : DWORD
' OldProtection : DWORD* out
Declare PtrSafe Function VirtualProtectFromApp Lib "api-ms-win-core-memory-l1-1-3" ( _
    ByVal Address As LongPtr, _
    ByVal Size As LongPtr, _
    ByVal NewProtection As Long, _
    ByRef OldProtection As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

VirtualProtectFromApp = ctypes.windll.LoadLibrary("api-ms-win-core-memory-l1-1-3.dll").VirtualProtectFromApp
VirtualProtectFromApp.restype = wintypes.BOOL
VirtualProtectFromApp.argtypes = [
    ctypes.POINTER(None),  # Address : void*
    ctypes.c_size_t,  # Size : UINT_PTR
    wintypes.DWORD,  # NewProtection : DWORD
    ctypes.POINTER(wintypes.DWORD),  # OldProtection : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('api-ms-win-core-memory-l1-1-3.dll')
VirtualProtectFromApp = Fiddle::Function.new(
  lib['VirtualProtectFromApp'],
  [
    Fiddle::TYPE_VOIDP,  # Address : void*
    Fiddle::TYPE_UINTPTR_T,  # Size : UINT_PTR
    -Fiddle::TYPE_INT,  # NewProtection : DWORD
    Fiddle::TYPE_VOIDP,  # OldProtection : DWORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "api-ms-win-core-memory-l1-1-3")]
extern "system" {
    fn VirtualProtectFromApp(
        Address: *mut (),  // void*
        Size: usize,  // UINT_PTR
        NewProtection: u32,  // DWORD
        OldProtection: *mut u32  // DWORD* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError = true)]
public static extern bool VirtualProtectFromApp(IntPtr Address, UIntPtr Size, uint NewProtection, out uint OldProtection);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-memory-l1-1-3_VirtualProtectFromApp' -Namespace Win32 -PassThru
# $api::VirtualProtectFromApp(Address, Size, NewProtection, OldProtection)
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#func global VirtualProtectFromApp "VirtualProtectFromApp" sptr, sptr, sptr, sptr
; VirtualProtectFromApp Address, Size, NewProtection, varptr(OldProtection)   ; 戻り値は stat
; Address : void* -> "sptr"
; Size : UINT_PTR -> "sptr"
; NewProtection : DWORD -> "sptr"
; OldProtection : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#cfunc global VirtualProtectFromApp "VirtualProtectFromApp" sptr, sptr, int, var
; res = VirtualProtectFromApp(Address, Size, NewProtection, OldProtection)
; Address : void* -> "sptr"
; Size : UINT_PTR -> "sptr"
; NewProtection : DWORD -> "int"
; OldProtection : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL VirtualProtectFromApp(void* Address, UINT_PTR Size, DWORD NewProtection, DWORD* OldProtection)
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#cfunc global VirtualProtectFromApp "VirtualProtectFromApp" intptr, intptr, int, var
; res = VirtualProtectFromApp(Address, Size, NewProtection, OldProtection)
; Address : void* -> "intptr"
; Size : UINT_PTR -> "intptr"
; NewProtection : DWORD -> "int"
; OldProtection : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	api_ms_win_core_memory_l1_1_3 = windows.NewLazySystemDLL("api-ms-win-core-memory-l1-1-3.dll")
	procVirtualProtectFromApp = api_ms_win_core_memory_l1_1_3.NewProc("VirtualProtectFromApp")
)

// Address (void*), Size (UINT_PTR), NewProtection (DWORD), OldProtection (DWORD* out)
r1, _, err := procVirtualProtectFromApp.Call(
	uintptr(Address),
	uintptr(Size),
	uintptr(NewProtection),
	uintptr(OldProtection),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function VirtualProtectFromApp(
  Address: Pointer;   // void*
  Size: NativeUInt;   // UINT_PTR
  NewProtection: DWORD;   // DWORD
  OldProtection: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'api-ms-win-core-memory-l1-1-3.dll' name 'VirtualProtectFromApp';
result := DllCall("api-ms-win-core-memory-l1-1-3\VirtualProtectFromApp"
    , "Ptr", Address   ; void*
    , "UPtr", Size   ; UINT_PTR
    , "UInt", NewProtection   ; DWORD
    , "Ptr", OldProtection   ; DWORD* out
    , "Int")   ; return: BOOL
●VirtualProtectFromApp(Address, Size, NewProtection, OldProtection) = DLL("api-ms-win-core-memory-l1-1-3.dll", "bool VirtualProtectFromApp(void*, int, dword, void*)")
# 呼び出し: VirtualProtectFromApp(Address, Size, NewProtection, OldProtection)
# Address : void* -> "void*"
# Size : UINT_PTR -> "int"
# NewProtection : DWORD -> "dword"
# OldProtection : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。