Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › EndDialog

EndDialog

関数
モーダルダイアログを終了し結果値を返す。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL EndDialog(
    HWND hDlg,
    INT_PTR nResult
);

パラメーター

名前方向説明
hDlgHWNDin破棄するダイアログボックスへのハンドル。
nResultINT_PTRinダイアログボックスを作成した関数からアプリケーションに返される値。

戻り値の型: BOOL

公式ドキュメント

モーダルダイアログボックスを破棄し、システムにそのダイアログボックスに関するすべての処理を終了させます。

戻り値

型: BOOL

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

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

解説(Remarks)

DialogBoxDialogBoxParamDialogBoxIndirectDialogBoxIndirectParam の各関数によって作成されたダイアログボックスは、EndDialog 関数を使用して破棄する必要があります。アプリケーションはダイアログボックスプロシージャ内から EndDialog を呼び出します。この関数を他の目的に使用してはなりません。

ダイアログボックスプロシージャは、WM_INITDIALOG メッセージの処理中も含め、いつでも EndDialog を呼び出すことができます。WM_INITDIALOG の処理中にこの関数を呼び出した場合、ダイアログボックスは表示される前、かつ入力フォーカスが設定される前に破棄されます。

EndDialog はダイアログボックスを即座に破棄するわけではありません。代わりにフラグを設定し、ダイアログボックスプロシージャがシステムに制御を返せるようにします。システムはアプリケーションキューから次のメッセージを取得しようとする前に、このフラグを確認します。フラグが設定されている場合、システムはメッセージループを終了し、ダイアログボックスを破棄して、nResult の値を、ダイアログボックスを作成した関数の戻り値として使用します。

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

各言語での呼び出し定義

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

BOOL EndDialog(
    HWND hDlg,
    INT_PTR nResult
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool EndDialog(
    IntPtr hDlg,   // HWND
    IntPtr nResult   // INT_PTR
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function EndDialog(
    hDlg As IntPtr,   ' HWND
    nResult As IntPtr   ' INT_PTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hDlg : HWND
' nResult : INT_PTR
Declare PtrSafe Function EndDialog Lib "user32" ( _
    ByVal hDlg As LongPtr, _
    ByVal nResult As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

EndDialog = ctypes.windll.user32.EndDialog
EndDialog.restype = wintypes.BOOL
EndDialog.argtypes = [
    wintypes.HANDLE,  # hDlg : HWND
    ctypes.c_ssize_t,  # nResult : INT_PTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
EndDialog = Fiddle::Function.new(
  lib['EndDialog'],
  [
    Fiddle::TYPE_VOIDP,  # hDlg : HWND
    Fiddle::TYPE_INTPTR_T,  # nResult : INT_PTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn EndDialog(
        hDlg: *mut core::ffi::c_void,  // HWND
        nResult: isize  // INT_PTR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool EndDialog(IntPtr hDlg, IntPtr nResult);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_EndDialog' -Namespace Win32 -PassThru
# $api::EndDialog(hDlg, nResult)
#uselib "USER32.dll"
#func global EndDialog "EndDialog" sptr, sptr
; EndDialog hDlg, nResult   ; 戻り値は stat
; hDlg : HWND -> "sptr"
; nResult : INT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global EndDialog "EndDialog" sptr, sptr
; res = EndDialog(hDlg, nResult)
; hDlg : HWND -> "sptr"
; nResult : INT_PTR -> "sptr"
; BOOL EndDialog(HWND hDlg, INT_PTR nResult)
#uselib "USER32.dll"
#cfunc global EndDialog "EndDialog" intptr, intptr
; res = EndDialog(hDlg, nResult)
; hDlg : HWND -> "intptr"
; nResult : INT_PTR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procEndDialog = user32.NewProc("EndDialog")
)

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

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

typedef EndDialogNative = Int32 Function(Pointer<Void>, IntPtr);
typedef EndDialogDart = int Function(Pointer<Void>, int);
final EndDialog = DynamicLibrary.open('USER32.dll')
    .lookupFunction<EndDialogNative, EndDialogDart>('EndDialog');
// hDlg : HWND -> Pointer<Void>
// nResult : INT_PTR -> IntPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function EndDialog(
  hDlg: THandle;   // HWND
  nResult: NativeInt   // INT_PTR
): BOOL; stdcall;
  external 'USER32.dll' name 'EndDialog';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "EndDialog"
  c_EndDialog :: Ptr () -> CIntPtr -> IO CInt
-- hDlg : HWND -> Ptr ()
-- nResult : INT_PTR -> CIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let enddialog =
  foreign "EndDialog"
    ((ptr void) @-> intptr_t @-> returning int32_t)
(* hDlg : HWND -> (ptr void) *)
(* nResult : INT_PTR -> intptr_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("EndDialog" end-dialog :convention :stdcall) :int32
  (h-dlg :pointer)   ; HWND
  (n-result :int64))   ; INT_PTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $EndDialog = Win32::API::More->new('USER32',
    'BOOL EndDialog(HANDLE hDlg, LPARAM nResult)');
# my $ret = $EndDialog->Call($hDlg, $nResult);
# hDlg : HWND -> HANDLE
# nResult : INT_PTR -> LPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目