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

SetUnhandledExceptionFilter

関数
未処理例外を処理するトップレベルフィルタ関数を設定する。
DLLKERNEL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilter(
    LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter   // optional
);

パラメーター

名前方向説明
lpTopLevelExceptionFilterLPTOP_LEVEL_EXCEPTION_FILTERinoptional

トップレベル例外フィルター関数へのポインター。この関数は、UnhandledExceptionFilter 関数が制御を受け取り、かつプロセスがデバッグされていないときに常に呼び出されます。このパラメーターに NULL を指定すると、UnhandledExceptionFilter 内での既定の処理が行われます。

フィルター関数の構文は UnhandledExceptionFilter と似ています。LPEXCEPTION_POINTERS 型の単一のパラメーターを受け取り、WINAPI 呼び出し規約を持ち、LONG 型の値を返します。フィルター関数は次のいずれかの値を返す必要があります。

意味
EXCEPTION_EXECUTE_HANDLER
0x1
UnhandledExceptionFilter から戻り、関連付けられた例外ハンドラーを実行します。通常はプロセスの終了につながります。
EXCEPTION_CONTINUE_EXECUTION
0xffffffff
UnhandledExceptionFilter から戻り、例外が発生した地点から実行を続行します。フィルター関数は、LPEXCEPTION_POINTERS パラメーターを通じて渡された例外情報を変更することで、続行状態を自由に変更できることに注意してください。
EXCEPTION_CONTINUE_SEARCH
0x0
UnhandledExceptionFilter の通常の実行を続行します。これは、SetErrorMode フラグに従うこと、またはアプリケーションエラーのポップアップメッセージボックスを表示することを意味します。

戻り値の型: LPTOP_LEVEL_EXCEPTION_FILTER

公式ドキュメント

アプリケーションが、プロセス内の各スレッドのトップレベル例外ハンドラーを上書きできるようにします。

戻り値

SetUnhandledExceptionFilter 関数は、この関数によって設定された以前の例外フィルターのアドレスを返します。戻り値が NULL の場合は、現在のトップレベル例外ハンドラーが存在しないことを意味します。

解説(Remarks)

SetUnhandledExceptionFilter を発行すると、呼び出し元プロセス内のすべての既存スレッドおよび将来のすべてのスレッドについて、既存のトップレベル例外フィルターが置き換えられます。

lpTopLevelExceptionFilter で指定された例外ハンドラーは、障害を引き起こしたスレッドのコンテキストで実行されます。これは、無効なスタックなどの特定の例外から回復する例外ハンドラーの能力に影響を与える可能性があります。

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

各言語での呼び出し定義

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

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

SetUnhandledExceptionFilter = ctypes.windll.kernel32.SetUnhandledExceptionFilter
SetUnhandledExceptionFilter.restype = ctypes.c_void_p
SetUnhandledExceptionFilter.argtypes = [
    ctypes.c_void_p,  # lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetUnhandledExceptionFilter = Fiddle::Function.new(
  lib['SetUnhandledExceptionFilter'],
  [
    Fiddle::TYPE_VOIDP,  # lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn SetUnhandledExceptionFilter(
        lpTopLevelExceptionFilter: *const core::ffi::c_void  // LPTOP_LEVEL_EXCEPTION_FILTER optional
    ) -> *const core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll")]
public static extern IntPtr SetUnhandledExceptionFilter(IntPtr lpTopLevelExceptionFilter);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetUnhandledExceptionFilter' -Namespace Win32 -PassThru
# $api::SetUnhandledExceptionFilter(lpTopLevelExceptionFilter)
#uselib "KERNEL32.dll"
#func global SetUnhandledExceptionFilter "SetUnhandledExceptionFilter" sptr
; SetUnhandledExceptionFilter lpTopLevelExceptionFilter   ; 戻り値は stat
; lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global SetUnhandledExceptionFilter "SetUnhandledExceptionFilter" sptr
; res = SetUnhandledExceptionFilter(lpTopLevelExceptionFilter)
; lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional -> "sptr"
; LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
#uselib "KERNEL32.dll"
#cfunc global SetUnhandledExceptionFilter "SetUnhandledExceptionFilter" intptr
; res = SetUnhandledExceptionFilter(lpTopLevelExceptionFilter)
; lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetUnhandledExceptionFilter = kernel32.NewProc("SetUnhandledExceptionFilter")
)

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

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

typedef SetUnhandledExceptionFilterNative = Pointer<Void> Function(Pointer<Void>);
typedef SetUnhandledExceptionFilterDart = Pointer<Void> Function(Pointer<Void>);
final SetUnhandledExceptionFilter = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<SetUnhandledExceptionFilterNative, SetUnhandledExceptionFilterDart>('SetUnhandledExceptionFilter');
// lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetUnhandledExceptionFilter(
  lpTopLevelExceptionFilter: Pointer   // LPTOP_LEVEL_EXCEPTION_FILTER optional
): Pointer; stdcall;
  external 'KERNEL32.dll' name 'SetUnhandledExceptionFilter';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetUnhandledExceptionFilter"
  c_SetUnhandledExceptionFilter :: Ptr () -> IO (Ptr ())
-- lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setunhandledexceptionfilter =
  foreign "SetUnhandledExceptionFilter"
    ((ptr void) @-> returning (ptr void))
(* lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER 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 ("SetUnhandledExceptionFilter" set-unhandled-exception-filter :convention :stdcall) :pointer
  (lp-top-level-exception-filter :pointer))   ; LPTOP_LEVEL_EXCEPTION_FILTER optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetUnhandledExceptionFilter = Win32::API::More->new('KERNEL32',
    'LPVOID SetUnhandledExceptionFilter(LPVOID lpTopLevelExceptionFilter)');
# my $ret = $SetUnhandledExceptionFilter->Call($lpTopLevelExceptionFilter);
# lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

使用する型