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

UnhandledExceptionFilter

関数
未処理例外に対する既定のシステムフィルタを呼び出す。
DLLKERNEL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

INT UnhandledExceptionFilter(
    EXCEPTION_POINTERS* ExceptionInfo
);

パラメーター

名前方向説明
ExceptionInfoEXCEPTION_POINTERS*in例外の説明と例外発生時のプロセッサコンテキストを指定する EXCEPTION_POINTERS 構造体へのポインターです。このポインターは、 GetExceptionInformation 関数の呼び出しによる戻り値です。

戻り値の型: INT

公式ドキュメント

プロセスがデバッグ中の場合に、処理されない例外をデバッガーに渡す、アプリケーション定義の関数です。

戻り値

この関数は次のいずれかの値を返します。

戻りコード/値 説明
EXCEPTION_CONTINUE_SEARCH
0x0
プロセスがデバッグ中であるため、例外は(2 回目の機会として)アプリケーションのデバッガーに渡される必要があります。
EXCEPTION_EXECUTE_HANDLER
0x1
以前の SetErrorMode の呼び出しで SEM_NOGPFAULTERRORBOX フラグが指定されていた場合、アプリケーションエラーのメッセージボックスは表示されません。この関数は例外ハンドラーに制御を返し、ハンドラーは適切な処理を自由に行えます。

解説(Remarks)

プロセスがデバッグされていない場合、この関数は現在のエラーモードに応じて アプリケーションエラー のメッセージボックスを表示します。既定の動作ではダイアログボックスを表示しますが、 SetErrorMode 関数の呼び出しで SEM_NOGPFAULTERRORBOX を指定することで無効化できます。

システムは、プロセスおよびスレッドの作成中に発生する例外を処理するために、内部で UnhandledExceptionFilter を使用します。

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

各言語での呼び出し定義

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

INT UnhandledExceptionFilter(
    EXCEPTION_POINTERS* ExceptionInfo
);
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern int UnhandledExceptionFilter(
    IntPtr ExceptionInfo   // EXCEPTION_POINTERS*
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function UnhandledExceptionFilter(
    ExceptionInfo As IntPtr   ' EXCEPTION_POINTERS*
) As Integer
End Function
' ExceptionInfo : EXCEPTION_POINTERS*
Declare PtrSafe Function UnhandledExceptionFilter Lib "kernel32" ( _
    ByVal ExceptionInfo As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

UnhandledExceptionFilter = ctypes.windll.kernel32.UnhandledExceptionFilter
UnhandledExceptionFilter.restype = ctypes.c_int
UnhandledExceptionFilter.argtypes = [
    ctypes.c_void_p,  # ExceptionInfo : EXCEPTION_POINTERS*
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
UnhandledExceptionFilter = Fiddle::Function.new(
  lib['UnhandledExceptionFilter'],
  [
    Fiddle::TYPE_VOIDP,  # ExceptionInfo : EXCEPTION_POINTERS*
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn UnhandledExceptionFilter(
        ExceptionInfo: *mut EXCEPTION_POINTERS  // EXCEPTION_POINTERS*
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll")]
public static extern int UnhandledExceptionFilter(IntPtr ExceptionInfo);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_UnhandledExceptionFilter' -Namespace Win32 -PassThru
# $api::UnhandledExceptionFilter(ExceptionInfo)
#uselib "KERNEL32.dll"
#func global UnhandledExceptionFilter "UnhandledExceptionFilter" sptr
; UnhandledExceptionFilter varptr(ExceptionInfo)   ; 戻り値は stat
; ExceptionInfo : EXCEPTION_POINTERS* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global UnhandledExceptionFilter "UnhandledExceptionFilter" var
; res = UnhandledExceptionFilter(ExceptionInfo)
; ExceptionInfo : EXCEPTION_POINTERS* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT UnhandledExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo)
#uselib "KERNEL32.dll"
#cfunc global UnhandledExceptionFilter "UnhandledExceptionFilter" var
; res = UnhandledExceptionFilter(ExceptionInfo)
; ExceptionInfo : EXCEPTION_POINTERS* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procUnhandledExceptionFilter = kernel32.NewProc("UnhandledExceptionFilter")
)

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

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

typedef UnhandledExceptionFilterNative = Int32 Function(Pointer<Void>);
typedef UnhandledExceptionFilterDart = int Function(Pointer<Void>);
final UnhandledExceptionFilter = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<UnhandledExceptionFilterNative, UnhandledExceptionFilterDart>('UnhandledExceptionFilter');
// ExceptionInfo : EXCEPTION_POINTERS* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function UnhandledExceptionFilter(
  ExceptionInfo: Pointer   // EXCEPTION_POINTERS*
): Integer; stdcall;
  external 'KERNEL32.dll' name 'UnhandledExceptionFilter';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "UnhandledExceptionFilter"
  c_UnhandledExceptionFilter :: Ptr () -> IO Int32
-- ExceptionInfo : EXCEPTION_POINTERS* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

(cffi:defcfun ("UnhandledExceptionFilter" unhandled-exception-filter :convention :stdcall) :int32
  (exception-info :pointer))   ; EXCEPTION_POINTERS*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $UnhandledExceptionFilter = Win32::API::More->new('KERNEL32',
    'int UnhandledExceptionFilter(LPVOID ExceptionInfo)');
# my $ret = $UnhandledExceptionFilter->Call($ExceptionInfo);
# ExceptionInfo : EXCEPTION_POINTERS* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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