Win32 API 日本語リファレンス
ホームSystem.Diagnostics.Debug › FormatMessageW

FormatMessageW

関数
メッセージ文字列を整形して取得する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD FormatMessageW(
    FORMAT_MESSAGE_OPTIONS dwFlags,
    const void* lpSource,   // optional
    DWORD dwMessageId,
    DWORD dwLanguageId,
    LPWSTR lpBuffer,
    DWORD nSize,
    CHAR** Arguments   // optional
);

パラメーター

名前方向
dwFlagsFORMAT_MESSAGE_OPTIONSin
lpSourcevoid*inoptional
dwMessageIdDWORDin
dwLanguageIdDWORDin
lpBufferLPWSTRout
nSizeDWORDin
ArgumentsCHAR**inoptional

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD FormatMessageW(
    FORMAT_MESSAGE_OPTIONS dwFlags,
    const void* lpSource,   // optional
    DWORD dwMessageId,
    DWORD dwLanguageId,
    LPWSTR lpBuffer,
    DWORD nSize,
    CHAR** Arguments   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint FormatMessageW(
    uint dwFlags,   // FORMAT_MESSAGE_OPTIONS
    IntPtr lpSource,   // void* optional
    uint dwMessageId,   // DWORD
    uint dwLanguageId,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer,   // LPWSTR out
    uint nSize,   // DWORD
    IntPtr Arguments   // CHAR** optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FormatMessageW(
    dwFlags As UInteger,   ' FORMAT_MESSAGE_OPTIONS
    lpSource As IntPtr,   ' void* optional
    dwMessageId As UInteger,   ' DWORD
    dwLanguageId As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder,   ' LPWSTR out
    nSize As UInteger,   ' DWORD
    Arguments As IntPtr   ' CHAR** optional
) As UInteger
End Function
' dwFlags : FORMAT_MESSAGE_OPTIONS
' lpSource : void* optional
' dwMessageId : DWORD
' dwLanguageId : DWORD
' lpBuffer : LPWSTR out
' nSize : DWORD
' Arguments : CHAR** optional
Declare PtrSafe Function FormatMessageW Lib "kernel32" ( _
    ByVal dwFlags As Long, _
    ByVal lpSource As LongPtr, _
    ByVal dwMessageId As Long, _
    ByVal dwLanguageId As Long, _
    ByVal lpBuffer As LongPtr, _
    ByVal nSize As Long, _
    ByVal Arguments 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

FormatMessageW = ctypes.windll.kernel32.FormatMessageW
FormatMessageW.restype = wintypes.DWORD
FormatMessageW.argtypes = [
    wintypes.DWORD,  # dwFlags : FORMAT_MESSAGE_OPTIONS
    ctypes.POINTER(None),  # lpSource : void* optional
    wintypes.DWORD,  # dwMessageId : DWORD
    wintypes.DWORD,  # dwLanguageId : DWORD
    wintypes.LPWSTR,  # lpBuffer : LPWSTR out
    wintypes.DWORD,  # nSize : DWORD
    ctypes.c_void_p,  # Arguments : CHAR** optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
FormatMessageW = Fiddle::Function.new(
  lib['FormatMessageW'],
  [
    -Fiddle::TYPE_INT,  # dwFlags : FORMAT_MESSAGE_OPTIONS
    Fiddle::TYPE_VOIDP,  # lpSource : void* optional
    -Fiddle::TYPE_INT,  # dwMessageId : DWORD
    -Fiddle::TYPE_INT,  # dwLanguageId : DWORD
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPWSTR out
    -Fiddle::TYPE_INT,  # nSize : DWORD
    Fiddle::TYPE_VOIDP,  # Arguments : CHAR** optional
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn FormatMessageW(
        dwFlags: u32,  // FORMAT_MESSAGE_OPTIONS
        lpSource: *const (),  // void* optional
        dwMessageId: u32,  // DWORD
        dwLanguageId: u32,  // DWORD
        lpBuffer: *mut u16,  // LPWSTR out
        nSize: u32,  // DWORD
        Arguments: *mut *mut i8  // CHAR** optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint FormatMessageW(uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, uint nSize, IntPtr Arguments);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FormatMessageW' -Namespace Win32 -PassThru
# $api::FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments)
#uselib "KERNEL32.dll"
#func global FormatMessageW "FormatMessageW" wptr, wptr, wptr, wptr, wptr, wptr, wptr
; FormatMessageW dwFlags, lpSource, dwMessageId, dwLanguageId, varptr(lpBuffer), nSize, varptr(Arguments)   ; 戻り値は stat
; dwFlags : FORMAT_MESSAGE_OPTIONS -> "wptr"
; lpSource : void* optional -> "wptr"
; dwMessageId : DWORD -> "wptr"
; dwLanguageId : DWORD -> "wptr"
; lpBuffer : LPWSTR out -> "wptr"
; nSize : DWORD -> "wptr"
; Arguments : CHAR** optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global FormatMessageW "FormatMessageW" int, sptr, int, int, var, int, var
; res = FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments)
; dwFlags : FORMAT_MESSAGE_OPTIONS -> "int"
; lpSource : void* optional -> "sptr"
; dwMessageId : DWORD -> "int"
; dwLanguageId : DWORD -> "int"
; lpBuffer : LPWSTR out -> "var"
; nSize : DWORD -> "int"
; Arguments : CHAR** optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD FormatMessageW(FORMAT_MESSAGE_OPTIONS dwFlags, void* lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, CHAR** Arguments)
#uselib "KERNEL32.dll"
#cfunc global FormatMessageW "FormatMessageW" int, intptr, int, int, var, int, var
; res = FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments)
; dwFlags : FORMAT_MESSAGE_OPTIONS -> "int"
; lpSource : void* optional -> "intptr"
; dwMessageId : DWORD -> "int"
; dwLanguageId : DWORD -> "int"
; lpBuffer : LPWSTR out -> "var"
; nSize : DWORD -> "int"
; Arguments : CHAR** optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procFormatMessageW = kernel32.NewProc("FormatMessageW")
)

// dwFlags (FORMAT_MESSAGE_OPTIONS), lpSource (void* optional), dwMessageId (DWORD), dwLanguageId (DWORD), lpBuffer (LPWSTR out), nSize (DWORD), Arguments (CHAR** optional)
r1, _, err := procFormatMessageW.Call(
	uintptr(dwFlags),
	uintptr(lpSource),
	uintptr(dwMessageId),
	uintptr(dwLanguageId),
	uintptr(lpBuffer),
	uintptr(nSize),
	uintptr(Arguments),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function FormatMessageW(
  dwFlags: DWORD;   // FORMAT_MESSAGE_OPTIONS
  lpSource: Pointer;   // void* optional
  dwMessageId: DWORD;   // DWORD
  dwLanguageId: DWORD;   // DWORD
  lpBuffer: PWideChar;   // LPWSTR out
  nSize: DWORD;   // DWORD
  Arguments: Pointer   // CHAR** optional
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'FormatMessageW';
result := DllCall("KERNEL32\FormatMessageW"
    , "UInt", dwFlags   ; FORMAT_MESSAGE_OPTIONS
    , "Ptr", lpSource   ; void* optional
    , "UInt", dwMessageId   ; DWORD
    , "UInt", dwLanguageId   ; DWORD
    , "Ptr", lpBuffer   ; LPWSTR out
    , "UInt", nSize   ; DWORD
    , "Ptr", Arguments   ; CHAR** optional
    , "UInt")   ; return: DWORD
●FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments) = DLL("KERNEL32.dll", "dword FormatMessageW(dword, void*, dword, dword, char*, dword, void*)")
# 呼び出し: FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments)
# dwFlags : FORMAT_MESSAGE_OPTIONS -> "dword"
# lpSource : void* optional -> "void*"
# dwMessageId : DWORD -> "dword"
# dwLanguageId : DWORD -> "dword"
# lpBuffer : LPWSTR out -> "char*"
# nSize : DWORD -> "dword"
# Arguments : CHAR** optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。