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

MessageBoxW

関数
メッセージボックスを表示しボタン応答を返す(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

MESSAGEBOX_RESULT MessageBoxW(
    HWND hWnd,   // optional
    LPCWSTR lpText,   // optional
    LPCWSTR lpCaption,   // optional
    MESSAGEBOX_STYLE uType
);

パラメーター

名前方向
hWndHWNDinoptional
lpTextLPCWSTRinoptional
lpCaptionLPCWSTRinoptional
uTypeMESSAGEBOX_STYLEin

戻り値の型: MESSAGEBOX_RESULT

各言語での呼び出し定義

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

MESSAGEBOX_RESULT MessageBoxW(
    HWND hWnd,   // optional
    LPCWSTR lpText,   // optional
    LPCWSTR lpCaption,   // optional
    MESSAGEBOX_STYLE uType
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int MessageBoxW(
    IntPtr hWnd,   // HWND optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpText,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpCaption,   // LPCWSTR optional
    uint uType   // MESSAGEBOX_STYLE
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function MessageBoxW(
    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
) As Integer
End Function
' hWnd : HWND optional
' lpText : LPCWSTR optional
' lpCaption : LPCWSTR optional
' uType : MESSAGEBOX_STYLE
Declare PtrSafe Function MessageBoxW Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal lpText As LongPtr, _
    ByVal lpCaption As LongPtr, _
    ByVal uType As Long) 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

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

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procMessageBoxW = user32.NewProc("MessageBoxW")
)

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