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

RtlRestoreContext

関数
指定したCPUコンテキストを復元して実行を再開する。
DLLKERNEL32.dll呼出規約cdecl

シグネチャ

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

void RtlRestoreContext(
    CONTEXT* ContextRecord,
    EXCEPTION_RECORD* ExceptionRecord   // optional
);

パラメーター

名前方向説明
ContextRecordCONTEXT*inCONTEXT 構造体へのポインターです。
ExceptionRecordEXCEPTION_RECORD*inoptional

EXCEPTION_RECORD 構造体へのポインターです。このパラメーターは省略可能であり、通常は NULL を指定します。

例外レコードは、主にロングジャンプおよび C++ の catch-throw のサポートで使用されます。ExceptionCode メンバーが STATUS_LONGJUMP の場合、ExceptionInformation メンバーにはジャンプバッファーへのポインターが格納されています。RtlRestoreContext は、コンテキストレコードを復元する前に、ジャンプバッファーから不揮発性の状態をコンテキストレコードへコピーします。

ExceptionCode メンバーが STATUS_UNWIND_CONSOLIDATE の場合、ExceptionInformation メンバーには、catch ハンドラーなどのコールバック関数へのポインターが格納されています。RtlRestoreContext は、コールバック関数を呼び出す前に、自身のフレームとコンテキストレコードで指定されたフレームとの間のコールフレームを統合します。これにより、コールバック関数内で発生する可能性のある例外処理から、それらのフレームが隠されます。これと通常のアンワインドとの違いは、スタック上のデータが依然として存在している点であり、スローオブジェクトなどのフレームデータも引き続き利用できます。コールバック関数は、コンテキストレコード内で更新する新しいプログラムカウンターを返し、その値が通常のコンテキスト復元で使用されます。

戻り値の型: void

公式ドキュメント

呼び出し元のコンテキストを、指定されたコンテキストレコードに復元します。

戻り値

この関数は値を返しません。

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

各言語での呼び出し定義

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

void RtlRestoreContext(
    CONTEXT* ContextRecord,
    EXCEPTION_RECORD* ExceptionRecord   // optional
);
[DllImport("KERNEL32.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
static extern void RtlRestoreContext(
    IntPtr ContextRecord,   // CONTEXT*
    IntPtr ExceptionRecord   // EXCEPTION_RECORD* optional
);
<DllImport("KERNEL32.dll", ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl)>
Public Shared Sub RtlRestoreContext(
    ContextRecord As IntPtr,   ' CONTEXT*
    ExceptionRecord As IntPtr   ' EXCEPTION_RECORD* optional
)
End Sub
' ContextRecord : CONTEXT*
' ExceptionRecord : EXCEPTION_RECORD* optional
Declare PtrSafe Sub RtlRestoreContext Lib "kernel32" ( _
    ByVal ContextRecord As LongPtr, _
    ByVal ExceptionRecord As LongPtr)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

RtlRestoreContext = ctypes.cdll.kernel32.RtlRestoreContext
RtlRestoreContext.restype = None
RtlRestoreContext.argtypes = [
    ctypes.c_void_p,  # ContextRecord : CONTEXT*
    ctypes.c_void_p,  # ExceptionRecord : EXCEPTION_RECORD* optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
RtlRestoreContext = Fiddle::Function.new(
  lib['RtlRestoreContext'],
  [
    Fiddle::TYPE_VOIDP,  # ContextRecord : CONTEXT*
    Fiddle::TYPE_VOIDP,  # ExceptionRecord : EXCEPTION_RECORD* optional
  ],
  Fiddle::TYPE_VOID, Fiddle::Function::CDECL)
#[link(name = "kernel32")]
extern "C" {
    fn RtlRestoreContext(
        ContextRecord: *mut CONTEXT,  // CONTEXT*
        ExceptionRecord: *mut EXCEPTION_RECORD  // EXCEPTION_RECORD* optional
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void RtlRestoreContext(IntPtr ContextRecord, IntPtr ExceptionRecord);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_RtlRestoreContext' -Namespace Win32 -PassThru
# $api::RtlRestoreContext(ContextRecord, ExceptionRecord)
#uselib "KERNEL32.dll"
#func global RtlRestoreContext "RtlRestoreContext" sptr, sptr
; RtlRestoreContext varptr(ContextRecord), varptr(ExceptionRecord)
; ContextRecord : CONTEXT* -> "sptr"
; ExceptionRecord : EXCEPTION_RECORD* optional -> "sptr"
出力引数:
#uselib "KERNEL32.dll"
#func global RtlRestoreContext "RtlRestoreContext" var, var
; RtlRestoreContext ContextRecord, ExceptionRecord
; ContextRecord : CONTEXT* -> "var"
; ExceptionRecord : EXCEPTION_RECORD* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; void RtlRestoreContext(CONTEXT* ContextRecord, EXCEPTION_RECORD* ExceptionRecord)
#uselib "KERNEL32.dll"
#func global RtlRestoreContext "RtlRestoreContext" var, var
; RtlRestoreContext ContextRecord, ExceptionRecord
; ContextRecord : CONTEXT* -> "var"
; ExceptionRecord : EXCEPTION_RECORD* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procRtlRestoreContext = kernel32.NewProc("RtlRestoreContext")
)

// ContextRecord (CONTEXT*), ExceptionRecord (EXCEPTION_RECORD* optional)
r1, _, err := procRtlRestoreContext.Call(
	uintptr(ContextRecord),
	uintptr(ExceptionRecord),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void
procedure RtlRestoreContext(
  ContextRecord: Pointer;   // CONTEXT*
  ExceptionRecord: Pointer   // EXCEPTION_RECORD* optional
); cdecl;
  external 'KERNEL32.dll' name 'RtlRestoreContext';
result := DllCall("KERNEL32\RtlRestoreContext"
    , "Ptr", ContextRecord   ; CONTEXT*
    , "Ptr", ExceptionRecord   ; EXCEPTION_RECORD* optional
    , "Cdecl Int")   ; return: void
●RtlRestoreContext(ContextRecord, ExceptionRecord) = DLL("KERNEL32.dll", "int RtlRestoreContext(void*, void*)")
# 呼び出し: RtlRestoreContext(ContextRecord, ExceptionRecord)
# ContextRecord : CONTEXT* -> "void*"
# ExceptionRecord : EXCEPTION_RECORD* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※cdecl関数。DLL()宣言はstdcall前提。cdeclは EXEC_PTR(`cdecl`,…) を使用。
const std = @import("std");

extern "kernel32" fn RtlRestoreContext(
    ContextRecord: [*c]CONTEXT, // CONTEXT*
    ExceptionRecord: [*c]EXCEPTION_RECORD // EXCEPTION_RECORD* optional
) callconv(.c) void;
proc RtlRestoreContext(
    ContextRecord: ptr CONTEXT,  # CONTEXT*
    ExceptionRecord: ptr EXCEPTION_RECORD  # EXCEPTION_RECORD* optional
) {.importc: "RtlRestoreContext", cdecl, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(C)
void RtlRestoreContext(
    CONTEXT* ContextRecord,   // CONTEXT*
    EXCEPTION_RECORD* ExceptionRecord   // EXCEPTION_RECORD* optional
);
ccall((:RtlRestoreContext, "KERNEL32.dll"), Cvoid,
      (Ptr{CONTEXT}, Ptr{EXCEPTION_RECORD}),
      ContextRecord, ExceptionRecord)
# ContextRecord : CONTEXT* -> Ptr{CONTEXT}
# ExceptionRecord : EXCEPTION_RECORD* optional -> Ptr{EXCEPTION_RECORD}
local ffi = require("ffi")
ffi.cdef[[
void RtlRestoreContext(
    void* ContextRecord,
    void* ExceptionRecord);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.RtlRestoreContext(ContextRecord, ExceptionRecord)
-- ContextRecord : CONTEXT*
-- ExceptionRecord : EXCEPTION_RECORD* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const RtlRestoreContext = lib.func('__cdecl', 'RtlRestoreContext', 'void', ['void *', 'void *']);
// RtlRestoreContext(ContextRecord, ExceptionRecord)
// ContextRecord : CONTEXT* -> 'void *'
// ExceptionRecord : EXCEPTION_RECORD* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  RtlRestoreContext: { parameters: ["pointer", "pointer"], result: "void" },
});
// lib.symbols.RtlRestoreContext(ContextRecord, ExceptionRecord)
// ContextRecord : CONTEXT* -> "pointer"
// ExceptionRecord : EXCEPTION_RECORD* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void RtlRestoreContext(
    void* ContextRecord,
    void* ExceptionRecord);
C, "KERNEL32.dll");
// $ffi->RtlRestoreContext(ContextRecord, ExceptionRecord);
// ContextRecord : CONTEXT*
// ExceptionRecord : EXCEPTION_RECORD* optional
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
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 Library {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    void RtlRestoreContext(
        Pointer ContextRecord,   // CONTEXT*
        Pointer ExceptionRecord   // EXCEPTION_RECORD* optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun RtlRestoreContext = RtlRestoreContext(
    ContextRecord : CONTEXT*,   # CONTEXT*
    ExceptionRecord : EXCEPTION_RECORD*   # EXCEPTION_RECORD* optional
  ) : Void
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef RtlRestoreContextNative = Void Function(Pointer<Void>, Pointer<Void>);
typedef RtlRestoreContextDart = void Function(Pointer<Void>, Pointer<Void>);
final RtlRestoreContext = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<RtlRestoreContextNative, RtlRestoreContextDart>('RtlRestoreContext');
// ContextRecord : CONTEXT* -> Pointer<Void>
// ExceptionRecord : EXCEPTION_RECORD* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
procedure RtlRestoreContext(
  ContextRecord: Pointer;   // CONTEXT*
  ExceptionRecord: Pointer   // EXCEPTION_RECORD* optional
); cdecl;
  external 'KERNEL32.dll' name 'RtlRestoreContext';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import ccall safe "RtlRestoreContext"
  c_RtlRestoreContext :: Ptr () -> Ptr () -> IO ()
-- ContextRecord : CONTEXT* -> Ptr ()
-- ExceptionRecord : EXCEPTION_RECORD* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let rtlrestorecontext =
  foreign "RtlRestoreContext"
    ((ptr void) @-> (ptr void) @-> returning void)
(* ContextRecord : CONTEXT* -> (ptr void) *)
(* ExceptionRecord : EXCEPTION_RECORD* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("RtlRestoreContext" rtl-restore-context :convention :cdecl) :void
  (context-record :pointer)   ; CONTEXT*
  (exception-record :pointer))   ; EXCEPTION_RECORD* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RtlRestoreContext = Win32::API::More->new('KERNEL32',
    'void RtlRestoreContext(LPVOID ContextRecord, LPVOID ExceptionRecord)');
# my $ret = $RtlRestoreContext->Call($ContextRecord, $ExceptionRecord);
# ContextRecord : CONTEXT* -> LPVOID
# ExceptionRecord : EXCEPTION_RECORD* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型