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

RtlUnwindEx

関数
指定フレームまで例外処理のスタック巻き戻しを実行する。
DLLKERNEL32.dll呼出規約winapi

シグネチャ

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

void RtlUnwindEx(
    void* TargetFrame,   // optional
    void* TargetIp,   // optional
    EXCEPTION_RECORD* ExceptionRecord,   // optional
    void* ReturnValue,
    CONTEXT* ContextRecord,
    UNWIND_HISTORY_TABLE* HistoryTable   // optional
);

パラメーター

名前方向説明
TargetFramevoid*inoptionalアンワインドの対象となる呼び出しフレームへのポインター。このパラメーターが NULL の場合、関数は終了アンワインドを実行します。
TargetIpvoid*inoptionalアンワインドの継続アドレス。TargetFrameNULL の場合、このパラメーターは無視されます。
ExceptionRecordEXCEPTION_RECORD*inoptionalEXCEPTION_RECORD 構造体へのポインター。
ReturnValuevoid*in実行を継続する前に整数関数の戻り値レジスタに格納される値。
ContextRecordCONTEXT*inアンワインド操作中にコンテキストを格納する CONTEXT 構造体へのポインター。
HistoryTableUNWIND_HISTORY_TABLE*inoptionalアンワインド履歴テーブルへのポインター。この構造体はプロセッサ固有です。この構造体の定義については、Winternl.h を参照してください。

戻り値の型: void

公式ドキュメント

プロシージャ呼び出しフレームのアンワインドを開始します。

戻り値

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

解説(Remarks)

FRAME_POINTERS 構造体は次のように定義されます。

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

各言語での呼び出し定義

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

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

RtlUnwindEx = ctypes.windll.kernel32.RtlUnwindEx
RtlUnwindEx.restype = None
RtlUnwindEx.argtypes = [
    ctypes.POINTER(None),  # TargetFrame : void* optional
    ctypes.POINTER(None),  # TargetIp : void* optional
    ctypes.c_void_p,  # ExceptionRecord : EXCEPTION_RECORD* optional
    ctypes.POINTER(None),  # ReturnValue : void*
    ctypes.c_void_p,  # ContextRecord : CONTEXT*
    ctypes.c_void_p,  # HistoryTable : UNWIND_HISTORY_TABLE* optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procRtlUnwindEx = kernel32.NewProc("RtlUnwindEx")
)

// TargetFrame (void* optional), TargetIp (void* optional), ExceptionRecord (EXCEPTION_RECORD* optional), ReturnValue (void*), ContextRecord (CONTEXT*), HistoryTable (UNWIND_HISTORY_TABLE* optional)
r1, _, err := procRtlUnwindEx.Call(
	uintptr(TargetFrame),
	uintptr(TargetIp),
	uintptr(ExceptionRecord),
	uintptr(ReturnValue),
	uintptr(ContextRecord),
	uintptr(HistoryTable),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void
procedure RtlUnwindEx(
  TargetFrame: Pointer;   // void* optional
  TargetIp: Pointer;   // void* optional
  ExceptionRecord: Pointer;   // EXCEPTION_RECORD* optional
  ReturnValue: Pointer;   // void*
  ContextRecord: Pointer;   // CONTEXT*
  HistoryTable: Pointer   // UNWIND_HISTORY_TABLE* optional
); stdcall;
  external 'KERNEL32.dll' name 'RtlUnwindEx';
result := DllCall("KERNEL32\RtlUnwindEx"
    , "Ptr", TargetFrame   ; void* optional
    , "Ptr", TargetIp   ; void* optional
    , "Ptr", ExceptionRecord   ; EXCEPTION_RECORD* optional
    , "Ptr", ReturnValue   ; void*
    , "Ptr", ContextRecord   ; CONTEXT*
    , "Ptr", HistoryTable   ; UNWIND_HISTORY_TABLE* optional
    , "Int")   ; return: void
●RtlUnwindEx(TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable) = DLL("KERNEL32.dll", "int RtlUnwindEx(void*, void*, void*, void*, void*, void*)")
# 呼び出し: RtlUnwindEx(TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable)
# TargetFrame : void* optional -> "void*"
# TargetIp : void* optional -> "void*"
# ExceptionRecord : EXCEPTION_RECORD* optional -> "void*"
# ReturnValue : void* -> "void*"
# ContextRecord : CONTEXT* -> "void*"
# HistoryTable : UNWIND_HISTORY_TABLE* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn RtlUnwindEx(
    TargetFrame: ?*anyopaque, // void* optional
    TargetIp: ?*anyopaque, // void* optional
    ExceptionRecord: [*c]EXCEPTION_RECORD, // EXCEPTION_RECORD* optional
    ReturnValue: ?*anyopaque, // void*
    ContextRecord: [*c]CONTEXT, // CONTEXT*
    HistoryTable: [*c]UNWIND_HISTORY_TABLE // UNWIND_HISTORY_TABLE* optional
) callconv(std.os.windows.WINAPI) void;
proc RtlUnwindEx(
    TargetFrame: pointer,  # void* optional
    TargetIp: pointer,  # void* optional
    ExceptionRecord: ptr EXCEPTION_RECORD,  # EXCEPTION_RECORD* optional
    ReturnValue: pointer,  # void*
    ContextRecord: ptr CONTEXT,  # CONTEXT*
    HistoryTable: ptr UNWIND_HISTORY_TABLE  # UNWIND_HISTORY_TABLE* optional
) {.importc: "RtlUnwindEx", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
void RtlUnwindEx(
    void* TargetFrame,   // void* optional
    void* TargetIp,   // void* optional
    EXCEPTION_RECORD* ExceptionRecord,   // EXCEPTION_RECORD* optional
    void* ReturnValue,   // void*
    CONTEXT* ContextRecord,   // CONTEXT*
    UNWIND_HISTORY_TABLE* HistoryTable   // UNWIND_HISTORY_TABLE* optional
);
ccall((:RtlUnwindEx, "KERNEL32.dll"), stdcall, Cvoid,
      (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{EXCEPTION_RECORD}, Ptr{Cvoid}, Ptr{CONTEXT}, Ptr{UNWIND_HISTORY_TABLE}),
      TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable)
# TargetFrame : void* optional -> Ptr{Cvoid}
# TargetIp : void* optional -> Ptr{Cvoid}
# ExceptionRecord : EXCEPTION_RECORD* optional -> Ptr{EXCEPTION_RECORD}
# ReturnValue : void* -> Ptr{Cvoid}
# ContextRecord : CONTEXT* -> Ptr{CONTEXT}
# HistoryTable : UNWIND_HISTORY_TABLE* optional -> Ptr{UNWIND_HISTORY_TABLE}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void RtlUnwindEx(
    void* TargetFrame,
    void* TargetIp,
    void* ExceptionRecord,
    void* ReturnValue,
    void* ContextRecord,
    void* HistoryTable);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.RtlUnwindEx(TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable)
-- TargetFrame : void* optional
-- TargetIp : void* optional
-- ExceptionRecord : EXCEPTION_RECORD* optional
-- ReturnValue : void*
-- ContextRecord : CONTEXT*
-- HistoryTable : UNWIND_HISTORY_TABLE* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const RtlUnwindEx = lib.func('__stdcall', 'RtlUnwindEx', 'void', ['void *', 'void *', 'void *', 'void *', 'void *', 'void *']);
// RtlUnwindEx(TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable)
// TargetFrame : void* optional -> 'void *'
// TargetIp : void* optional -> 'void *'
// ExceptionRecord : EXCEPTION_RECORD* optional -> 'void *'
// ReturnValue : void* -> 'void *'
// ContextRecord : CONTEXT* -> 'void *'
// HistoryTable : UNWIND_HISTORY_TABLE* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  RtlUnwindEx: { parameters: ["pointer", "pointer", "pointer", "pointer", "pointer", "pointer"], result: "void" },
});
// lib.symbols.RtlUnwindEx(TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable)
// TargetFrame : void* optional -> "pointer"
// TargetIp : void* optional -> "pointer"
// ExceptionRecord : EXCEPTION_RECORD* optional -> "pointer"
// ReturnValue : void* -> "pointer"
// ContextRecord : CONTEXT* -> "pointer"
// HistoryTable : UNWIND_HISTORY_TABLE* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void RtlUnwindEx(
    void* TargetFrame,
    void* TargetIp,
    void* ExceptionRecord,
    void* ReturnValue,
    void* ContextRecord,
    void* HistoryTable);
C, "KERNEL32.dll");
// $ffi->RtlUnwindEx(TargetFrame, TargetIp, ExceptionRecord, ReturnValue, ContextRecord, HistoryTable);
// TargetFrame : void* optional
// TargetIp : void* optional
// ExceptionRecord : EXCEPTION_RECORD* optional
// ReturnValue : void*
// ContextRecord : CONTEXT*
// HistoryTable : UNWIND_HISTORY_TABLE* optional
// 構造体/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);
    void RtlUnwindEx(
        Pointer TargetFrame,   // void* optional
        Pointer TargetIp,   // void* optional
        Pointer ExceptionRecord,   // EXCEPTION_RECORD* optional
        Pointer ReturnValue,   // void*
        Pointer ContextRecord,   // CONTEXT*
        Pointer HistoryTable   // UNWIND_HISTORY_TABLE* optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun RtlUnwindEx = RtlUnwindEx(
    TargetFrame : Void*,   # void* optional
    TargetIp : Void*,   # void* optional
    ExceptionRecord : EXCEPTION_RECORD*,   # EXCEPTION_RECORD* optional
    ReturnValue : Void*,   # void*
    ContextRecord : CONTEXT*,   # CONTEXT*
    HistoryTable : UNWIND_HISTORY_TABLE*   # UNWIND_HISTORY_TABLE* optional
  ) : Void
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

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

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

let rtlunwindex =
  foreign "RtlUnwindEx"
    ((ptr void) @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> returning void)
(* TargetFrame : void* optional -> (ptr void) *)
(* TargetIp : void* optional -> (ptr void) *)
(* ExceptionRecord : EXCEPTION_RECORD* optional -> (ptr void) *)
(* ReturnValue : void* -> (ptr void) *)
(* ContextRecord : CONTEXT* -> (ptr void) *)
(* HistoryTable : UNWIND_HISTORY_TABLE* 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 ("RtlUnwindEx" rtl-unwind-ex :convention :stdcall) :void
  (target-frame :pointer)   ; void* optional
  (target-ip :pointer)   ; void* optional
  (exception-record :pointer)   ; EXCEPTION_RECORD* optional
  (return-value :pointer)   ; void*
  (context-record :pointer)   ; CONTEXT*
  (history-table :pointer))   ; UNWIND_HISTORY_TABLE* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RtlUnwindEx = Win32::API::More->new('KERNEL32',
    'void RtlUnwindEx(LPVOID TargetFrame, LPVOID TargetIp, LPVOID ExceptionRecord, LPVOID ReturnValue, LPVOID ContextRecord, LPVOID HistoryTable)');
# my $ret = $RtlUnwindEx->Call($TargetFrame, $TargetIp, $ExceptionRecord, $ReturnValue, $ContextRecord, $HistoryTable);
# TargetFrame : void* optional -> LPVOID
# TargetIp : void* optional -> LPVOID
# ExceptionRecord : EXCEPTION_RECORD* optional -> LPVOID
# ReturnValue : void* -> LPVOID
# ContextRecord : CONTEXT* -> LPVOID
# HistoryTable : UNWIND_HISTORY_TABLE* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
使用する型