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

MessageBoxIndirectW

関数
構造体指定でカスタムメッセージボックスを表示する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

MESSAGEBOX_RESULT MessageBoxIndirectW(
    const MSGBOXPARAMSW* lpmbp
);

パラメーター

名前方向説明
lpmbpMSGBOXPARAMSW*inメッセージボックスの表示に使用する情報を格納した MSGBOXPARAMS 構造体へのポインターです。

戻り値の型: MESSAGEBOX_RESULT

公式ドキュメント

メッセージボックスを作成、表示、操作します。メッセージボックスには、アプリケーションが定義したメッセージテキストとタイトル、任意のアイコン、および定義済みのプッシュボタンの任意の組み合わせが含まれます。(Unicode)

戻り値

型: int

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

メッセージボックスに Cancel ボタンがある場合、ESC キーを押すか Cancel ボタンを選択すると、関数は IDCANCEL 値を返します。メッセージボックスに Cancel ボタンがない場合、ESC キーを押しても効果はありません。

メッセージボックスを作成するのに十分なメモリがない場合、戻り値は 0 です。

戻り値/値 説明
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)

システムのメモリが不足していることを示すためにシステムモーダルのメッセージボックスを使用する場合、リソースの読み込みに失敗する可能性があるため、MSGBOXPARAMS 構造体の lpszText および lpszCaption メンバーが指す文字列をリソースファイルから取得しないでください。

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

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 MessageBoxIndirectW(
    const MSGBOXPARAMSW* lpmbp
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int MessageBoxIndirectW(
    IntPtr lpmbp   // MSGBOXPARAMSW*
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function MessageBoxIndirectW(
    lpmbp As IntPtr   ' MSGBOXPARAMSW*
) As Integer
End Function
' lpmbp : MSGBOXPARAMSW*
Declare PtrSafe Function MessageBoxIndirectW Lib "user32" ( _
    ByVal lpmbp As LongPtr) 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

MessageBoxIndirectW = ctypes.windll.user32.MessageBoxIndirectW
MessageBoxIndirectW.restype = ctypes.c_int
MessageBoxIndirectW.argtypes = [
    ctypes.c_void_p,  # lpmbp : MSGBOXPARAMSW*
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
MessageBoxIndirectW = Fiddle::Function.new(
  lib['MessageBoxIndirectW'],
  [
    Fiddle::TYPE_VOIDP,  # lpmbp : MSGBOXPARAMSW*
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn MessageBoxIndirectW(
        lpmbp: *const MSGBOXPARAMSW  // MSGBOXPARAMSW*
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBoxIndirectW(IntPtr lpmbp);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_MessageBoxIndirectW' -Namespace Win32 -PassThru
# $api::MessageBoxIndirectW(lpmbp)
#uselib "USER32.dll"
#func global MessageBoxIndirectW "MessageBoxIndirectW" wptr
; MessageBoxIndirectW varptr(lpmbp)   ; 戻り値は stat
; lpmbp : MSGBOXPARAMSW* -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global MessageBoxIndirectW "MessageBoxIndirectW" var
; res = MessageBoxIndirectW(lpmbp)
; lpmbp : MSGBOXPARAMSW* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; MESSAGEBOX_RESULT MessageBoxIndirectW(MSGBOXPARAMSW* lpmbp)
#uselib "USER32.dll"
#cfunc global MessageBoxIndirectW "MessageBoxIndirectW" var
; res = MessageBoxIndirectW(lpmbp)
; lpmbp : MSGBOXPARAMSW* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procMessageBoxIndirectW = user32.NewProc("MessageBoxIndirectW")
)

// lpmbp (MSGBOXPARAMSW*)
r1, _, err := procMessageBoxIndirectW.Call(
	uintptr(lpmbp),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // MESSAGEBOX_RESULT
function MessageBoxIndirectW(
  lpmbp: Pointer   // MSGBOXPARAMSW*
): Integer; stdcall;
  external 'USER32.dll' name 'MessageBoxIndirectW';
result := DllCall("USER32\MessageBoxIndirectW"
    , "Ptr", lpmbp   ; MSGBOXPARAMSW*
    , "Int")   ; return: MESSAGEBOX_RESULT
●MessageBoxIndirectW(lpmbp) = DLL("USER32.dll", "int MessageBoxIndirectW(void*)")
# 呼び出し: MessageBoxIndirectW(lpmbp)
# lpmbp : MSGBOXPARAMSW* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "user32" fn MessageBoxIndirectW(
    lpmbp: [*c]MSGBOXPARAMSW // MSGBOXPARAMSW*
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc MessageBoxIndirectW(
    lpmbp: ptr MSGBOXPARAMSW  # MSGBOXPARAMSW*
): int32 {.importc: "MessageBoxIndirectW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "user32");
extern(Windows)
int MessageBoxIndirectW(
    MSGBOXPARAMSW* lpmbp   // MSGBOXPARAMSW*
);
ccall((:MessageBoxIndirectW, "USER32.dll"), stdcall, Int32,
      (Ptr{MSGBOXPARAMSW},),
      lpmbp)
# lpmbp : MSGBOXPARAMSW* -> Ptr{MSGBOXPARAMSW}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t MessageBoxIndirectW(
    void* lpmbp);
]]
local user32 = ffi.load("user32")
-- user32.MessageBoxIndirectW(lpmbp)
-- lpmbp : MSGBOXPARAMSW*
-- 構造体/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 MessageBoxIndirectW = lib.func('__stdcall', 'MessageBoxIndirectW', 'int32_t', ['void *']);
// MessageBoxIndirectW(lpmbp)
// lpmbp : MSGBOXPARAMSW* -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  MessageBoxIndirectW: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.MessageBoxIndirectW(lpmbp)
// lpmbp : MSGBOXPARAMSW* -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t MessageBoxIndirectW(
    void* lpmbp);
C, "USER32.dll");
// $ffi->MessageBoxIndirectW(lpmbp);
// lpmbp : MSGBOXPARAMSW*
// 構造体/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 MessageBoxIndirectW(
        Pointer lpmbp   // MSGBOXPARAMSW*
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("user32")]
lib LibUSER32
  fun MessageBoxIndirectW = MessageBoxIndirectW(
    lpmbp : MSGBOXPARAMSW*   # MSGBOXPARAMSW*
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef MessageBoxIndirectWNative = Int32 Function(Pointer<Void>);
typedef MessageBoxIndirectWDart = int Function(Pointer<Void>);
final MessageBoxIndirectW = DynamicLibrary.open('USER32.dll')
    .lookupFunction<MessageBoxIndirectWNative, MessageBoxIndirectWDart>('MessageBoxIndirectW');
// lpmbp : MSGBOXPARAMSW* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MessageBoxIndirectW(
  lpmbp: Pointer   // MSGBOXPARAMSW*
): Integer; stdcall;
  external 'USER32.dll' name 'MessageBoxIndirectW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MessageBoxIndirectW"
  c_MessageBoxIndirectW :: Ptr () -> IO Int32
-- lpmbp : MSGBOXPARAMSW* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

(cffi:defcfun ("MessageBoxIndirectW" message-box-indirect-w :convention :stdcall) :int32
  (lpmbp :pointer))   ; MSGBOXPARAMSW*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MessageBoxIndirectW = Win32::API::More->new('USER32',
    'int MessageBoxIndirectW(LPVOID lpmbp)');
# my $ret = $MessageBoxIndirectW->Call($lpmbp);
# lpmbp : MSGBOXPARAMSW* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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