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

WaitForDebugEventEx

関数
デバッグイベントの発生を待機する拡張版関数である。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows 10 以降

シグネチャ

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

BOOL WaitForDebugEventEx(
    DEBUG_EVENT* lpDebugEvent,
    DWORD dwMilliseconds
);

パラメーター

名前方向説明
lpDebugEventDEBUG_EVENT*outデバッグイベントに関する情報を受け取る DEBUG_EVENT 構造体へのポインターです。
dwMillisecondsDWORDinデバッグイベントを待機するミリ秒数です。このパラメーターが 0 の場合、関数はデバッグイベントの有無を調べてすぐに戻ります。パラメーターが INFINITE の場合、関数はデバッグイベントが発生するまで戻りません。

戻り値の型: BOOL

公式ドキュメント

デバッグ対象のプロセスでデバッグイベントが発生するのを待機します。(WaitForDebugEventEx)

戻り値

関数が成功した場合、戻り値は 0 以外になります。

関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

デバッグ対象のプロセスを作成したスレッドのみが WaitForDebugEventEx を呼び出すことができます。

CREATE_PROCESS_DEBUG_EVENT が発生すると、デバッガーアプリケーションは、デバッグ対象プロセスのイメージファイルへのハンドル、デバッグ対象プロセスへのハンドル、およびデバッグ対象プロセスの初期スレッドへのハンドルを DEBUG_EVENT 構造体で受け取ります。これらのハンドルが返されるメンバーは、u.CreateProcessInfo.hFile(イメージファイル)、u.CreateProcessInfo.hProcess(プロセス)、および u.CreateProcessInfo.hThread(初期スレッド)です。システムが以前に EXIT_PROCESS_DEBUG_EVENT デバッグイベントを報告していた場合、デバッガーが ContinueDebugEvent 関数を呼び出すと、システムはプロセスとスレッドへのハンドルを閉じます。デバッガーは、 CloseHandle 関数を呼び出してイメージファイルへのハンドルを閉じる必要があります。

同様に、CREATE_THREAD_DEBUG_EVENT が発生すると、デバッガーアプリケーションは、作成によってデバッグイベントが発生したスレッドへのハンドルを DEBUG_EVENT 構造体の u.CreateThread.hThread メンバーで受け取ります。システムが以前に EXIT_THREAD_DEBUG_EVENT デバッグイベントを報告していた場合、デバッガーが ContinueDebugEvent 関数を呼び出すと、システムはスレッドへのハンドルを閉じます。

LOAD_DLL_DEBUG_EVENT が発生すると、デバッガーアプリケーションは、読み込まれた DLL へのハンドルを DEBUG_EVENT 構造体の u.LoadDll.hFile メンバーで受け取ります。このハンドルは、デバッガーアプリケーションが CloseHandle 関数を呼び出して閉じる必要があります。

警告 WaitForDebugEventEx を呼び出すスレッドに 非同期プロシージャ呼び出し(APC)をキューに入れないでください。

例については、 Writing the Debugger's Main Loop を参照してください。

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

各言語での呼び出し定義

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

BOOL WaitForDebugEventEx(
    DEBUG_EVENT* lpDebugEvent,
    DWORD dwMilliseconds
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WaitForDebugEventEx(
    IntPtr lpDebugEvent,   // DEBUG_EVENT* out
    uint dwMilliseconds   // DWORD
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WaitForDebugEventEx(
    lpDebugEvent As IntPtr,   ' DEBUG_EVENT* out
    dwMilliseconds As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpDebugEvent : DEBUG_EVENT* out
' dwMilliseconds : DWORD
Declare PtrSafe Function WaitForDebugEventEx Lib "kernel32" ( _
    ByVal lpDebugEvent As LongPtr, _
    ByVal dwMilliseconds As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WaitForDebugEventEx = ctypes.windll.kernel32.WaitForDebugEventEx
WaitForDebugEventEx.restype = wintypes.BOOL
WaitForDebugEventEx.argtypes = [
    ctypes.c_void_p,  # lpDebugEvent : DEBUG_EVENT* out
    wintypes.DWORD,  # dwMilliseconds : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procWaitForDebugEventEx = kernel32.NewProc("WaitForDebugEventEx")
)

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

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

typedef WaitForDebugEventExNative = Int32 Function(Pointer<Void>, Uint32);
typedef WaitForDebugEventExDart = int Function(Pointer<Void>, int);
final WaitForDebugEventEx = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<WaitForDebugEventExNative, WaitForDebugEventExDart>('WaitForDebugEventEx');
// lpDebugEvent : DEBUG_EVENT* out -> Pointer<Void>
// dwMilliseconds : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WaitForDebugEventEx(
  lpDebugEvent: Pointer;   // DEBUG_EVENT* out
  dwMilliseconds: DWORD   // DWORD
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'WaitForDebugEventEx';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WaitForDebugEventEx"
  c_WaitForDebugEventEx :: Ptr () -> Word32 -> IO CInt
-- lpDebugEvent : DEBUG_EVENT* out -> Ptr ()
-- dwMilliseconds : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let waitfordebugeventex =
  foreign "WaitForDebugEventEx"
    ((ptr void) @-> uint32_t @-> returning int32_t)
(* lpDebugEvent : DEBUG_EVENT* out -> (ptr void) *)
(* dwMilliseconds : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("WaitForDebugEventEx" wait-for-debug-event-ex :convention :stdcall) :int32
  (lp-debug-event :pointer)   ; DEBUG_EVENT* out
  (dw-milliseconds :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WaitForDebugEventEx = Win32::API::More->new('KERNEL32',
    'BOOL WaitForDebugEventEx(LPVOID lpDebugEvent, DWORD dwMilliseconds)');
# my $ret = $WaitForDebugEventEx->Call($lpDebugEvent, $dwMilliseconds);
# lpDebugEvent : DEBUG_EVENT* out -> LPVOID
# dwMilliseconds : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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