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

MessageBoxExW

関数
言語指定付きでメッセージボックスを表示する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// USER32.dll  (Unicode / -W)
#include <windows.h>

MESSAGEBOX_RESULT MessageBoxExW(
    HWND hWnd,   // optional
    LPCWSTR lpText,   // optional
    LPCWSTR lpCaption,   // optional
    MESSAGEBOX_STYLE uType,
    WORD wLanguageId
);

パラメーター

名前方向説明
hWndHWNDinoptional作成するメッセージボックスの所有者ウィンドウへのハンドル。このパラメーターが NULL の場合、メッセージボックスには所有者ウィンドウがありません。
lpTextLPCWSTRinoptional表示するメッセージ。
lpCaptionLPCWSTRinoptionalダイアログボックスのタイトル。このパラメーターが NULL の場合、既定のタイトル Error が使用されます。
uTypeMESSAGEBOX_STYLEinダイアログボックスの内容と動作。サポートされるフラグについては、MessageBox を参照してください。
wLanguageIdWORDin

メッセージボックスのボタンに表示されるテキストの言語。値として 0 を指定すると、ボタンテキストが既定のシステム言語で表示されます。このパラメーターが MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) の場合、呼び出し元スレッドに関連付けられた現在の言語が使用されます。

現在の言語以外の言語を指定するには、MAKELANGID マクロを使用してこのパラメーターを作成します。詳細については、MAKELANGID を参照してください。

戻り値の型: MESSAGEBOX_RESULT

公式ドキュメント

メッセージボックスを作成、表示、操作します。(Unicode)

戻り値

型: int

メッセージボックスに Cancel ボタンがある場合、ESC キーが押されるか Cancel ボタンが選択されると、この関数は IDCANCEL の値を返します。メッセージボックスに Cancel ボタンがない場合、ESC キーを押しても効果はありません。ただし、MB_OK ボタンが存在する場合は別です。MB_OK ボタンが表示されていてユーザーが ESC キーを押した場合、戻り値は IDOK になります。

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

関数が成功した場合、戻り値は次のメニュー項目の値のいずれかになります。

戻り値/値 説明
IDABORT
3
Abort ボタンが選択されました。
IDCANCEL
2
Cancel ボタンが選択されました。
IDCONTINUE
11
Continue ボタンが選択されました。
IDIGNORE
5
Ignore ボタンが選択されました。
IDNO
7
No ボタンが選択されました。
IDOK
1
OK ボタンが選択されました。
IDRETRY
4
Retry ボタンが選択されました。
IDTRYAGAIN
10
Try Again ボタンが選択されました。
IDYES
6
Yes ボタンが選択されました。

解説(Remarks)

システムモーダルのメッセージボックスを使用してシステムのメモリ不足を通知する場合、lpText および lpCaption パラメーターが指す文字列はリソースファイルから取得すべきではありません。リソースの読み込みに失敗する可能性があるためです。

ダイアログボックスが表示されている状態でメッセージボックスを作成する場合は、そのダイアログボックスへのハンドルを hWnd パラメーターとして使用してください。hWnd パラメーターには、ダイアログボックス内のコントロールなどの子ウィンドウを指定すべきではありません。

メモ

winuser.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして MessageBoxEx を定義しています。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在して使用すると、不整合が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規約 を参照してください。

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

各言語での呼び出し定義

// USER32.dll  (Unicode / -W)
#include <windows.h>

MESSAGEBOX_RESULT MessageBoxExW(
    HWND hWnd,   // optional
    LPCWSTR lpText,   // optional
    LPCWSTR lpCaption,   // optional
    MESSAGEBOX_STYLE uType,
    WORD wLanguageId
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int MessageBoxExW(
    IntPtr hWnd,   // HWND optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpText,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpCaption,   // LPCWSTR optional
    uint uType,   // MESSAGEBOX_STYLE
    ushort wLanguageId   // WORD
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function MessageBoxExW(
    hWnd As IntPtr,   ' HWND optional
    <MarshalAs(UnmanagedType.LPWStr)> lpText As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpCaption As String,   ' LPCWSTR optional
    uType As UInteger,   ' MESSAGEBOX_STYLE
    wLanguageId As UShort   ' WORD
) As Integer
End Function
' hWnd : HWND optional
' lpText : LPCWSTR optional
' lpCaption : LPCWSTR optional
' uType : MESSAGEBOX_STYLE
' wLanguageId : WORD
Declare PtrSafe Function MessageBoxExW Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal lpText As LongPtr, _
    ByVal lpCaption As LongPtr, _
    ByVal uType As Long, _
    ByVal wLanguageId As Integer) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

MessageBoxExW = ctypes.windll.user32.MessageBoxExW
MessageBoxExW.restype = ctypes.c_int
MessageBoxExW.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND optional
    wintypes.LPCWSTR,  # lpText : LPCWSTR optional
    wintypes.LPCWSTR,  # lpCaption : LPCWSTR optional
    wintypes.DWORD,  # uType : MESSAGEBOX_STYLE
    ctypes.c_ushort,  # wLanguageId : WORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
MessageBoxExW = Fiddle::Function.new(
  lib['MessageBoxExW'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND optional
    Fiddle::TYPE_VOIDP,  # lpText : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpCaption : LPCWSTR optional
    -Fiddle::TYPE_INT,  # uType : MESSAGEBOX_STYLE
    -Fiddle::TYPE_SHORT,  # wLanguageId : WORD
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn MessageBoxExW(
        hWnd: *mut core::ffi::c_void,  // HWND optional
        lpText: *const u16,  // LPCWSTR optional
        lpCaption: *const u16,  // LPCWSTR optional
        uType: u32,  // MESSAGEBOX_STYLE
        wLanguageId: u16  // WORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int MessageBoxExW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string lpText, [MarshalAs(UnmanagedType.LPWStr)] string lpCaption, uint uType, ushort wLanguageId);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_MessageBoxExW' -Namespace Win32 -PassThru
# $api::MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
#uselib "USER32.dll"
#func global MessageBoxExW "MessageBoxExW" wptr, wptr, wptr, wptr, wptr
; MessageBoxExW hWnd, lpText, lpCaption, uType, wLanguageId   ; 戻り値は stat
; hWnd : HWND optional -> "wptr"
; lpText : LPCWSTR optional -> "wptr"
; lpCaption : LPCWSTR optional -> "wptr"
; uType : MESSAGEBOX_STYLE -> "wptr"
; wLanguageId : WORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global MessageBoxExW "MessageBoxExW" sptr, wstr, wstr, int, int
; res = MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
; hWnd : HWND optional -> "sptr"
; lpText : LPCWSTR optional -> "wstr"
; lpCaption : LPCWSTR optional -> "wstr"
; uType : MESSAGEBOX_STYLE -> "int"
; wLanguageId : WORD -> "int"
; MESSAGEBOX_RESULT MessageBoxExW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, MESSAGEBOX_STYLE uType, WORD wLanguageId)
#uselib "USER32.dll"
#cfunc global MessageBoxExW "MessageBoxExW" intptr, wstr, wstr, int, int
; res = MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
; hWnd : HWND optional -> "intptr"
; lpText : LPCWSTR optional -> "wstr"
; lpCaption : LPCWSTR optional -> "wstr"
; uType : MESSAGEBOX_STYLE -> "int"
; wLanguageId : WORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procMessageBoxExW = user32.NewProc("MessageBoxExW")
)

// hWnd (HWND optional), lpText (LPCWSTR optional), lpCaption (LPCWSTR optional), uType (MESSAGEBOX_STYLE), wLanguageId (WORD)
r1, _, err := procMessageBoxExW.Call(
	uintptr(hWnd),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpText))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpCaption))),
	uintptr(uType),
	uintptr(wLanguageId),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // MESSAGEBOX_RESULT
function MessageBoxExW(
  hWnd: THandle;   // HWND optional
  lpText: PWideChar;   // LPCWSTR optional
  lpCaption: PWideChar;   // LPCWSTR optional
  uType: DWORD;   // MESSAGEBOX_STYLE
  wLanguageId: Word   // WORD
): Integer; stdcall;
  external 'USER32.dll' name 'MessageBoxExW';
result := DllCall("USER32\MessageBoxExW"
    , "Ptr", hWnd   ; HWND optional
    , "WStr", lpText   ; LPCWSTR optional
    , "WStr", lpCaption   ; LPCWSTR optional
    , "UInt", uType   ; MESSAGEBOX_STYLE
    , "UShort", wLanguageId   ; WORD
    , "Int")   ; return: MESSAGEBOX_RESULT
●MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId) = DLL("USER32.dll", "int MessageBoxExW(void*, char*, char*, dword, int)")
# 呼び出し: MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
# hWnd : HWND optional -> "void*"
# lpText : LPCWSTR optional -> "char*"
# lpCaption : LPCWSTR optional -> "char*"
# uType : MESSAGEBOX_STYLE -> "dword"
# wLanguageId : WORD -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "user32" fn MessageBoxExW(
    hWnd: ?*anyopaque, // HWND optional
    lpText: [*c]const u16, // LPCWSTR optional
    lpCaption: [*c]const u16, // LPCWSTR optional
    uType: u32, // MESSAGEBOX_STYLE
    wLanguageId: u16 // WORD
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc MessageBoxExW(
    hWnd: pointer,  # HWND optional
    lpText: WideCString,  # LPCWSTR optional
    lpCaption: WideCString,  # LPCWSTR optional
    uType: uint32,  # MESSAGEBOX_STYLE
    wLanguageId: uint16  # WORD
): int32 {.importc: "MessageBoxExW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "user32");
extern(Windows)
int MessageBoxExW(
    void* hWnd,   // HWND optional
    const(wchar)* lpText,   // LPCWSTR optional
    const(wchar)* lpCaption,   // LPCWSTR optional
    uint uType,   // MESSAGEBOX_STYLE
    ushort wLanguageId   // WORD
);
ccall((:MessageBoxExW, "USER32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Cwstring, Cwstring, UInt32, UInt16),
      hWnd, lpText, lpCaption, uType, wLanguageId)
# hWnd : HWND optional -> Ptr{Cvoid}
# lpText : LPCWSTR optional -> Cwstring
# lpCaption : LPCWSTR optional -> Cwstring
# uType : MESSAGEBOX_STYLE -> UInt32
# wLanguageId : WORD -> UInt16
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t MessageBoxExW(
    void* hWnd,
    const uint16_t* lpText,
    const uint16_t* lpCaption,
    uint32_t uType,
    uint16_t wLanguageId);
]]
local user32 = ffi.load("user32")
-- user32.MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
-- hWnd : HWND optional
-- lpText : LPCWSTR optional
-- lpCaption : LPCWSTR optional
-- uType : MESSAGEBOX_STYLE
-- wLanguageId : WORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const MessageBoxExW = lib.func('__stdcall', 'MessageBoxExW', 'int32_t', ['void *', 'str16', 'str16', 'uint32_t', 'uint16_t']);
// MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
// hWnd : HWND optional -> 'void *'
// lpText : LPCWSTR optional -> 'str16'
// lpCaption : LPCWSTR optional -> 'str16'
// uType : MESSAGEBOX_STYLE -> 'uint32_t'
// wLanguageId : WORD -> 'uint16_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  MessageBoxExW: { parameters: ["pointer", "buffer", "buffer", "u32", "u16"], result: "i32" },
});
// lib.symbols.MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId)
// hWnd : HWND optional -> "pointer"
// lpText : LPCWSTR optional -> "buffer"
// lpCaption : LPCWSTR optional -> "buffer"
// uType : MESSAGEBOX_STYLE -> "u32"
// wLanguageId : WORD -> "u16"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t MessageBoxExW(
    void* hWnd,
    const uint16_t* lpText,
    const uint16_t* lpCaption,
    uint32_t uType,
    uint16_t wLanguageId);
C, "USER32.dll");
// $ffi->MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId);
// hWnd : HWND optional
// lpText : LPCWSTR optional
// lpCaption : LPCWSTR optional
// uType : MESSAGEBOX_STYLE
// wLanguageId : WORD
// 構造体/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, W32APIOptions.UNICODE_OPTIONS);
    int MessageBoxExW(
        Pointer hWnd,   // HWND optional
        WString lpText,   // LPCWSTR optional
        WString lpCaption,   // LPCWSTR optional
        int uType,   // MESSAGEBOX_STYLE
        short wLanguageId   // WORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("user32")]
lib LibUSER32
  fun MessageBoxExW = MessageBoxExW(
    hWnd : Void*,   # HWND optional
    lpText : UInt16*,   # LPCWSTR optional
    lpCaption : UInt16*,   # LPCWSTR optional
    uType : UInt32,   # MESSAGEBOX_STYLE
    wLanguageId : UInt16   # WORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef MessageBoxExWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Uint32, Uint16);
typedef MessageBoxExWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, int, int);
final MessageBoxExW = DynamicLibrary.open('USER32.dll')
    .lookupFunction<MessageBoxExWNative, MessageBoxExWDart>('MessageBoxExW');
// hWnd : HWND optional -> Pointer<Void>
// lpText : LPCWSTR optional -> Pointer<Utf16>
// lpCaption : LPCWSTR optional -> Pointer<Utf16>
// uType : MESSAGEBOX_STYLE -> Uint32
// wLanguageId : WORD -> Uint16
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MessageBoxExW(
  hWnd: THandle;   // HWND optional
  lpText: PWideChar;   // LPCWSTR optional
  lpCaption: PWideChar;   // LPCWSTR optional
  uType: DWORD;   // MESSAGEBOX_STYLE
  wLanguageId: Word   // WORD
): Integer; stdcall;
  external 'USER32.dll' name 'MessageBoxExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MessageBoxExW"
  c_MessageBoxExW :: Ptr () -> CWString -> CWString -> Word32 -> Word16 -> IO Int32
-- hWnd : HWND optional -> Ptr ()
-- lpText : LPCWSTR optional -> CWString
-- lpCaption : LPCWSTR optional -> CWString
-- uType : MESSAGEBOX_STYLE -> Word32
-- wLanguageId : WORD -> Word16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let messageboxexw =
  foreign "MessageBoxExW"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> uint16_t @-> returning int32_t)
(* hWnd : HWND optional -> (ptr void) *)
(* lpText : LPCWSTR optional -> (ptr uint16_t) *)
(* lpCaption : LPCWSTR optional -> (ptr uint16_t) *)
(* uType : MESSAGEBOX_STYLE -> uint32_t *)
(* wLanguageId : WORD -> uint16_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("MessageBoxExW" message-box-ex-w :convention :stdcall) :int32
  (h-wnd :pointer)   ; HWND optional
  (lp-text (:string :encoding :utf-16le))   ; LPCWSTR optional
  (lp-caption (:string :encoding :utf-16le))   ; LPCWSTR optional
  (u-type :uint32)   ; MESSAGEBOX_STYLE
  (w-language-id :uint16))   ; WORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MessageBoxExW = Win32::API::More->new('USER32',
    'int MessageBoxExW(HANDLE hWnd, LPCWSTR lpText, LPCWSTR lpCaption, DWORD uType, WORD wLanguageId)');
# my $ret = $MessageBoxExW->Call($hWnd, $lpText, $lpCaption, $uType, $wLanguageId);
# hWnd : HWND optional -> HANDLE
# lpText : LPCWSTR optional -> LPCWSTR
# lpCaption : LPCWSTR optional -> LPCWSTR
# uType : MESSAGEBOX_STYLE -> DWORD
# wLanguageId : WORD -> WORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
公式の関連項目
使用する型