WaitForDebugEvent
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL WaitForDebugEvent(
DEBUG_EVENT* lpDebugEvent,
DWORD dwMilliseconds
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpDebugEvent | DEBUG_EVENT* | out | デバッグイベントに関する情報を受け取る DEBUG_EVENT 構造体へのポインターです。 |
| dwMilliseconds | DWORD | in | デバッグイベントを待機するミリ秒数です。このパラメーターが 0 の場合、関数はデバッグイベントの有無を確認してすぐに戻ります。パラメーターが INFINITE の場合、関数はデバッグイベントが発生するまで戻りません。 |
戻り値の型: BOOL
公式ドキュメント
デバッグ対象のプロセスでデバッグイベントが発生するのを待機します。(WaitForDebugEvent)
戻り値
関数が成功した場合、戻り値は 0 以外の値です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
デバッグ対象のプロセスを作成したスレッドのみが WaitForDebugEvent を呼び出すことができます。
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 が発生すると、デバッガーアプリケーションは DEBUG_EVENT 構造体の u.LoadDll.hFile メンバーに、ロードされた DLL へのハンドルを受け取ります。このハンドルは、デバッガーアプリケーションが CloseHandle 関数を呼び出して閉じる必要があります。
例
例については、 Writing the Debugger's Main Loop を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL WaitForDebugEvent(
DEBUG_EVENT* lpDebugEvent,
DWORD dwMilliseconds
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WaitForDebugEvent(
IntPtr lpDebugEvent, // DEBUG_EVENT* out
uint dwMilliseconds // DWORD
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WaitForDebugEvent(
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 WaitForDebugEvent 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
WaitForDebugEvent = ctypes.windll.kernel32.WaitForDebugEvent
WaitForDebugEvent.restype = wintypes.BOOL
WaitForDebugEvent.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')
WaitForDebugEvent = Fiddle::Function.new(
lib['WaitForDebugEvent'],
[
Fiddle::TYPE_VOIDP, # lpDebugEvent : DEBUG_EVENT* out
-Fiddle::TYPE_INT, # dwMilliseconds : DWORD
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn WaitForDebugEvent(
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 WaitForDebugEvent(IntPtr lpDebugEvent, uint dwMilliseconds);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_WaitForDebugEvent' -Namespace Win32 -PassThru
# $api::WaitForDebugEvent(lpDebugEvent, dwMilliseconds)#uselib "KERNEL32.dll"
#func global WaitForDebugEvent "WaitForDebugEvent" sptr, sptr
; WaitForDebugEvent varptr(lpDebugEvent), dwMilliseconds ; 戻り値は stat
; lpDebugEvent : DEBUG_EVENT* out -> "sptr"
; dwMilliseconds : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global WaitForDebugEvent "WaitForDebugEvent" var, int ; res = WaitForDebugEvent(lpDebugEvent, dwMilliseconds) ; lpDebugEvent : DEBUG_EVENT* out -> "var" ; dwMilliseconds : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global WaitForDebugEvent "WaitForDebugEvent" sptr, int ; res = WaitForDebugEvent(varptr(lpDebugEvent), dwMilliseconds) ; lpDebugEvent : DEBUG_EVENT* out -> "sptr" ; dwMilliseconds : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL WaitForDebugEvent(DEBUG_EVENT* lpDebugEvent, DWORD dwMilliseconds) #uselib "KERNEL32.dll" #cfunc global WaitForDebugEvent "WaitForDebugEvent" var, int ; res = WaitForDebugEvent(lpDebugEvent, dwMilliseconds) ; lpDebugEvent : DEBUG_EVENT* out -> "var" ; dwMilliseconds : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL WaitForDebugEvent(DEBUG_EVENT* lpDebugEvent, DWORD dwMilliseconds) #uselib "KERNEL32.dll" #cfunc global WaitForDebugEvent "WaitForDebugEvent" intptr, int ; res = WaitForDebugEvent(varptr(lpDebugEvent), dwMilliseconds) ; lpDebugEvent : DEBUG_EVENT* out -> "intptr" ; dwMilliseconds : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procWaitForDebugEvent = kernel32.NewProc("WaitForDebugEvent")
)
// lpDebugEvent (DEBUG_EVENT* out), dwMilliseconds (DWORD)
r1, _, err := procWaitForDebugEvent.Call(
uintptr(lpDebugEvent),
uintptr(dwMilliseconds),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction WaitForDebugEvent(
lpDebugEvent: Pointer; // DEBUG_EVENT* out
dwMilliseconds: DWORD // DWORD
): BOOL; stdcall;
external 'KERNEL32.dll' name 'WaitForDebugEvent';result := DllCall("KERNEL32\WaitForDebugEvent"
, "Ptr", lpDebugEvent ; DEBUG_EVENT* out
, "UInt", dwMilliseconds ; DWORD
, "Int") ; return: BOOL●WaitForDebugEvent(lpDebugEvent, dwMilliseconds) = DLL("KERNEL32.dll", "bool WaitForDebugEvent(void*, dword)")
# 呼び出し: WaitForDebugEvent(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 WaitForDebugEvent(
lpDebugEvent: [*c]DEBUG_EVENT, // DEBUG_EVENT* out
dwMilliseconds: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc WaitForDebugEvent(
lpDebugEvent: ptr DEBUG_EVENT, # DEBUG_EVENT* out
dwMilliseconds: uint32 # DWORD
): int32 {.importc: "WaitForDebugEvent", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int WaitForDebugEvent(
DEBUG_EVENT* lpDebugEvent, // DEBUG_EVENT* out
uint dwMilliseconds // DWORD
);ccall((:WaitForDebugEvent, "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 WaitForDebugEvent(
void* lpDebugEvent,
uint32_t dwMilliseconds);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.WaitForDebugEvent(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 WaitForDebugEvent = lib.func('__stdcall', 'WaitForDebugEvent', 'int32_t', ['void *', 'uint32_t']);
// WaitForDebugEvent(lpDebugEvent, dwMilliseconds)
// lpDebugEvent : DEBUG_EVENT* out -> 'void *'
// dwMilliseconds : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
WaitForDebugEvent: { parameters: ["pointer", "u32"], result: "i32" },
});
// lib.symbols.WaitForDebugEvent(lpDebugEvent, dwMilliseconds)
// lpDebugEvent : DEBUG_EVENT* out -> "pointer"
// dwMilliseconds : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t WaitForDebugEvent(
void* lpDebugEvent,
uint32_t dwMilliseconds);
C, "KERNEL32.dll");
// $ffi->WaitForDebugEvent(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 WaitForDebugEvent(
Pointer lpDebugEvent, // DEBUG_EVENT* out
int dwMilliseconds // DWORD
);
}@[Link("kernel32")]
lib LibKERNEL32
fun WaitForDebugEvent = WaitForDebugEvent(
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 WaitForDebugEventNative = Int32 Function(Pointer<Void>, Uint32);
typedef WaitForDebugEventDart = int Function(Pointer<Void>, int);
final WaitForDebugEvent = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<WaitForDebugEventNative, WaitForDebugEventDart>('WaitForDebugEvent');
// lpDebugEvent : DEBUG_EVENT* out -> Pointer<Void>
// dwMilliseconds : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WaitForDebugEvent(
lpDebugEvent: Pointer; // DEBUG_EVENT* out
dwMilliseconds: DWORD // DWORD
): BOOL; stdcall;
external 'KERNEL32.dll' name 'WaitForDebugEvent';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WaitForDebugEvent"
c_WaitForDebugEvent :: Ptr () -> Word32 -> IO CInt
-- lpDebugEvent : DEBUG_EVENT* out -> Ptr ()
-- dwMilliseconds : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let waitfordebugevent =
foreign "WaitForDebugEvent"
((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 ("WaitForDebugEvent" wait-for-debug-event :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 $WaitForDebugEvent = Win32::API::More->new('KERNEL32',
'BOOL WaitForDebugEvent(LPVOID lpDebugEvent, DWORD dwMilliseconds)');
# my $ret = $WaitForDebugEvent->Call($lpDebugEvent, $dwMilliseconds);
# lpDebugEvent : DEBUG_EVENT* out -> LPVOID
# dwMilliseconds : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f WaitForDebugEventEx — デバッグイベントの発生を待機する拡張版関数である。
- f ContinueDebugEvent — デバッグイベントを報告したスレッドの実行を再開させる。
- f DebugActiveProcess — 実行中プロセスにデバッガとしてアタッチする。
- f DebugBreak — 現プロセスにブレークポイント例外を発生させる。
- f OutputDebugStringW — Unicode文字列をデバッガの出力に送信する。