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

RaiseException

関数
指定したコードとフラグでソフトウェア例外を発生させる。
DLLKERNEL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

void RaiseException(
    DWORD dwExceptionCode,
    DWORD dwExceptionFlags,
    DWORD nNumberOfArguments,
    const UINT_PTR* lpArguments   // optional
);

パラメーター

名前方向説明
dwExceptionCodeDWORDin

発生させる例外の、アプリケーション定義の例外コードです。例外ハンドラーのフィルター式および例外ハンドラー ブロックでは、GetExceptionCode 関数を使用してこの値を取得できます。

なお、システムはメッセージを表示する前に dwExceptionCode のビット 28 をクリアします。このビットは予約された例外ビットであり、システムが独自の目的で使用します。

dwExceptionFlagsDWORDin例外フラグです。継続可能な例外を示すゼロ、または継続不可能な例外を示す EXCEPTION_NONCONTINUABLE のいずれかを指定できます。継続不可能な例外の後に実行を継続しようとすると、EXCEPTION_NONCONTINUABLE_EXCEPTION 例外が発生します。
nNumberOfArgumentsDWORDinlpArguments 配列内の引数の数です。この値は EXCEPTION_MAXIMUM_PARAMETERS を超えてはなりません。lpArgumentsNULL の場合、このパラメーターは無視されます。
lpArgumentsUINT_PTR*inoptional引数の配列です。このパラメーターには NULL を指定できます。これらの引数には、例外ハンドラーのフィルター式に渡す必要のある任意のアプリケーション定義データを格納できます。

戻り値の型: void

公式ドキュメント

呼び出し元スレッドで例外を発生させます。

戻り値

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

解説(Remarks)

RaiseException 関数を使用すると、プロセスは構造化例外処理を使って、プライベートな、ソフトウェアによって生成されるアプリケーション定義の例外を処理できます。

例外を発生させると、例外ディスパッチャーは次の手順で例外ハンドラーを検索します。

  1. システムはまず、存在する場合はプロセスのデバッガーへの通知を試みます。
  2. プロセスがデバッグされていない場合、または関連付けられたデバッガーが例外を処理しない場合、システムは例外が発生したスレッドのスタック フレームを検索して、フレーム ベースの例外ハンドラーを見つけようとします。システムは最初に現在のスタック フレームを検索し、その後さかのぼって前のスタック フレームを順に調べます。
  3. フレーム ベースのハンドラーが見つからない場合、またはどのフレーム ベースのハンドラーも例外を処理しない場合、システムは 2 回目のプロセスのデバッガーへの通知を試みます。
  4. プロセスがデバッグされていない場合、または関連付けられたデバッガーが例外を処理しない場合、システムは例外の種類に基づいて既定の処理を行います。ほとんどの例外では、既定の動作は ExitProcess 関数を呼び出すことです。

dwExceptionCodedwExceptionFlagsnNumberOfArgumentslpArguments の各パラメーターで指定した値は、GetExceptionInformation 関数を呼び出すことで、フレーム ベースの例外ハンドラーのフィルター式内で取得できます。デバッガーは WaitForDebugEvent 関数を呼び出すことでこれらの値を取得できます。

例については、例外ハンドラーの使用を参照してください。

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

各言語での呼び出し定義

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

void RaiseException(
    DWORD dwExceptionCode,
    DWORD dwExceptionFlags,
    DWORD nNumberOfArguments,
    const UINT_PTR* lpArguments   // optional
);
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern void RaiseException(
    uint dwExceptionCode,   // DWORD
    uint dwExceptionFlags,   // DWORD
    uint nNumberOfArguments,   // DWORD
    IntPtr lpArguments   // UINT_PTR* optional
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Sub RaiseException(
    dwExceptionCode As UInteger,   ' DWORD
    dwExceptionFlags As UInteger,   ' DWORD
    nNumberOfArguments As UInteger,   ' DWORD
    lpArguments As IntPtr   ' UINT_PTR* optional
)
End Sub
' dwExceptionCode : DWORD
' dwExceptionFlags : DWORD
' nNumberOfArguments : DWORD
' lpArguments : UINT_PTR* optional
Declare PtrSafe Sub RaiseException Lib "kernel32" ( _
    ByVal dwExceptionCode As Long, _
    ByVal dwExceptionFlags As Long, _
    ByVal nNumberOfArguments As Long, _
    ByVal lpArguments As LongPtr)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

RaiseException = ctypes.windll.kernel32.RaiseException
RaiseException.restype = None
RaiseException.argtypes = [
    wintypes.DWORD,  # dwExceptionCode : DWORD
    wintypes.DWORD,  # dwExceptionFlags : DWORD
    wintypes.DWORD,  # nNumberOfArguments : DWORD
    ctypes.POINTER(ctypes.c_size_t),  # lpArguments : UINT_PTR* optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
RaiseException = Fiddle::Function.new(
  lib['RaiseException'],
  [
    -Fiddle::TYPE_INT,  # dwExceptionCode : DWORD
    -Fiddle::TYPE_INT,  # dwExceptionFlags : DWORD
    -Fiddle::TYPE_INT,  # nNumberOfArguments : DWORD
    Fiddle::TYPE_VOIDP,  # lpArguments : UINT_PTR* optional
  ],
  Fiddle::TYPE_VOID)
#[link(name = "kernel32")]
extern "system" {
    fn RaiseException(
        dwExceptionCode: u32,  // DWORD
        dwExceptionFlags: u32,  // DWORD
        nNumberOfArguments: u32,  // DWORD
        lpArguments: *const usize  // UINT_PTR* optional
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll")]
public static extern void RaiseException(uint dwExceptionCode, uint dwExceptionFlags, uint nNumberOfArguments, IntPtr lpArguments);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_RaiseException' -Namespace Win32 -PassThru
# $api::RaiseException(dwExceptionCode, dwExceptionFlags, nNumberOfArguments, lpArguments)
#uselib "KERNEL32.dll"
#func global RaiseException "RaiseException" sptr, sptr, sptr, sptr
; RaiseException dwExceptionCode, dwExceptionFlags, nNumberOfArguments, varptr(lpArguments)
; dwExceptionCode : DWORD -> "sptr"
; dwExceptionFlags : DWORD -> "sptr"
; nNumberOfArguments : DWORD -> "sptr"
; lpArguments : UINT_PTR* optional -> "sptr"
出力引数:
#uselib "KERNEL32.dll"
#func global RaiseException "RaiseException" int, int, int, var
; RaiseException dwExceptionCode, dwExceptionFlags, nNumberOfArguments, lpArguments
; dwExceptionCode : DWORD -> "int"
; dwExceptionFlags : DWORD -> "int"
; nNumberOfArguments : DWORD -> "int"
; lpArguments : UINT_PTR* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; void RaiseException(DWORD dwExceptionCode, DWORD dwExceptionFlags, DWORD nNumberOfArguments, UINT_PTR* lpArguments)
#uselib "KERNEL32.dll"
#func global RaiseException "RaiseException" int, int, int, var
; RaiseException dwExceptionCode, dwExceptionFlags, nNumberOfArguments, lpArguments
; dwExceptionCode : DWORD -> "int"
; dwExceptionFlags : DWORD -> "int"
; nNumberOfArguments : DWORD -> "int"
; lpArguments : UINT_PTR* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procRaiseException = kernel32.NewProc("RaiseException")
)

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

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

typedef RaiseExceptionNative = Void Function(Uint32, Uint32, Uint32, Pointer<UintPtr>);
typedef RaiseExceptionDart = void Function(int, int, int, Pointer<UintPtr>);
final RaiseException = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<RaiseExceptionNative, RaiseExceptionDart>('RaiseException');
// dwExceptionCode : DWORD -> Uint32
// dwExceptionFlags : DWORD -> Uint32
// nNumberOfArguments : DWORD -> Uint32
// lpArguments : UINT_PTR* optional -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
procedure RaiseException(
  dwExceptionCode: DWORD;   // DWORD
  dwExceptionFlags: DWORD;   // DWORD
  nNumberOfArguments: DWORD;   // DWORD
  lpArguments: Pointer   // UINT_PTR* optional
); stdcall;
  external 'KERNEL32.dll' name 'RaiseException';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RaiseException"
  c_RaiseException :: Word32 -> Word32 -> Word32 -> Ptr CUIntPtr -> IO ()
-- dwExceptionCode : DWORD -> Word32
-- dwExceptionFlags : DWORD -> Word32
-- nNumberOfArguments : DWORD -> Word32
-- lpArguments : UINT_PTR* optional -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let raiseexception =
  foreign "RaiseException"
    (uint32_t @-> uint32_t @-> uint32_t @-> (ptr size_t) @-> returning void)
(* dwExceptionCode : DWORD -> uint32_t *)
(* dwExceptionFlags : DWORD -> uint32_t *)
(* nNumberOfArguments : DWORD -> uint32_t *)
(* lpArguments : UINT_PTR* optional -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("RaiseException" raise-exception :convention :stdcall) :void
  (dw-exception-code :uint32)   ; DWORD
  (dw-exception-flags :uint32)   ; DWORD
  (n-number-of-arguments :uint32)   ; DWORD
  (lp-arguments :pointer))   ; UINT_PTR* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RaiseException = Win32::API::More->new('KERNEL32',
    'void RaiseException(DWORD dwExceptionCode, DWORD dwExceptionFlags, DWORD nNumberOfArguments, LPVOID lpArguments)');
# my $ret = $RaiseException->Call($dwExceptionCode, $dwExceptionFlags, $nNumberOfArguments, $lpArguments);
# dwExceptionCode : DWORD -> DWORD
# dwExceptionFlags : DWORD -> DWORD
# nNumberOfArguments : DWORD -> DWORD
# lpArguments : UINT_PTR* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目