ホーム › System.Diagnostics.Debug › FormatMessageA
FormatMessageA
関数メッセージ文字列を整形して取得する(ANSI版)。
シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
DWORD FormatMessageA(
FORMAT_MESSAGE_OPTIONS dwFlags,
const void* lpSource, // optional
DWORD dwMessageId,
DWORD dwLanguageId,
LPSTR lpBuffer,
DWORD nSize,
CHAR** Arguments // optional
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| dwFlags | FORMAT_MESSAGE_OPTIONS | in |
| lpSource | void* | inoptional |
| dwMessageId | DWORD | in |
| dwLanguageId | DWORD | in |
| lpBuffer | LPSTR | out |
| nSize | DWORD | in |
| Arguments | CHAR** | inoptional |
戻り値の型: DWORD
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
DWORD FormatMessageA(
FORMAT_MESSAGE_OPTIONS dwFlags,
const void* lpSource, // optional
DWORD dwMessageId,
DWORD dwLanguageId,
LPSTR lpBuffer,
DWORD nSize,
CHAR** Arguments // optional
);[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern uint FormatMessageA(
uint dwFlags, // FORMAT_MESSAGE_OPTIONS
IntPtr lpSource, // void* optional
uint dwMessageId, // DWORD
uint dwLanguageId, // DWORD
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer, // LPSTR out
uint nSize, // DWORD
IntPtr Arguments // CHAR** optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FormatMessageA(
dwFlags As UInteger, ' FORMAT_MESSAGE_OPTIONS
lpSource As IntPtr, ' void* optional
dwMessageId As UInteger, ' DWORD
dwLanguageId As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPStr)> lpBuffer As System.Text.StringBuilder, ' LPSTR 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 : LPSTR out
' nSize : DWORD
' Arguments : CHAR** optional
Declare PtrSafe Function FormatMessageA Lib "kernel32" ( _
ByVal dwFlags As Long, _
ByVal lpSource As LongPtr, _
ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
ByVal Arguments As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
FormatMessageA = ctypes.windll.kernel32.FormatMessageA
FormatMessageA.restype = wintypes.DWORD
FormatMessageA.argtypes = [
wintypes.DWORD, # dwFlags : FORMAT_MESSAGE_OPTIONS
ctypes.POINTER(None), # lpSource : void* optional
wintypes.DWORD, # dwMessageId : DWORD
wintypes.DWORD, # dwLanguageId : DWORD
wintypes.LPSTR, # lpBuffer : LPSTR 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')
FormatMessageA = Fiddle::Function.new(
lib['FormatMessageA'],
[
-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 : LPSTR out
-Fiddle::TYPE_INT, # nSize : DWORD
Fiddle::TYPE_VOIDP, # Arguments : CHAR** optional
],
-Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn FormatMessageA(
dwFlags: u32, // FORMAT_MESSAGE_OPTIONS
lpSource: *const (), // void* optional
dwMessageId: u32, // DWORD
dwLanguageId: u32, // DWORD
lpBuffer: *mut u8, // LPSTR 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.Ansi, SetLastError = true)]
public static extern uint FormatMessageA(uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer, uint nSize, IntPtr Arguments);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FormatMessageA' -Namespace Win32 -PassThru
# $api::FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments)#uselib "KERNEL32.dll"
#func global FormatMessageA "FormatMessageA" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; FormatMessageA dwFlags, lpSource, dwMessageId, dwLanguageId, varptr(lpBuffer), nSize, varptr(Arguments) ; 戻り値は stat
; dwFlags : FORMAT_MESSAGE_OPTIONS -> "sptr"
; lpSource : void* optional -> "sptr"
; dwMessageId : DWORD -> "sptr"
; dwLanguageId : DWORD -> "sptr"
; lpBuffer : LPSTR out -> "sptr"
; nSize : DWORD -> "sptr"
; Arguments : CHAR** optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global FormatMessageA "FormatMessageA" int, sptr, int, int, var, int, var ; res = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments) ; dwFlags : FORMAT_MESSAGE_OPTIONS -> "int" ; lpSource : void* optional -> "sptr" ; dwMessageId : DWORD -> "int" ; dwLanguageId : DWORD -> "int" ; lpBuffer : LPSTR out -> "var" ; nSize : DWORD -> "int" ; Arguments : CHAR** optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global FormatMessageA "FormatMessageA" int, sptr, int, int, sptr, int, sptr ; res = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, varptr(lpBuffer), nSize, varptr(Arguments)) ; dwFlags : FORMAT_MESSAGE_OPTIONS -> "int" ; lpSource : void* optional -> "sptr" ; dwMessageId : DWORD -> "int" ; dwLanguageId : DWORD -> "int" ; lpBuffer : LPSTR out -> "sptr" ; nSize : DWORD -> "int" ; Arguments : CHAR** optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD FormatMessageA(FORMAT_MESSAGE_OPTIONS dwFlags, void* lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize, CHAR** Arguments) #uselib "KERNEL32.dll" #cfunc global FormatMessageA "FormatMessageA" int, intptr, int, int, var, int, var ; res = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments) ; dwFlags : FORMAT_MESSAGE_OPTIONS -> "int" ; lpSource : void* optional -> "intptr" ; dwMessageId : DWORD -> "int" ; dwLanguageId : DWORD -> "int" ; lpBuffer : LPSTR out -> "var" ; nSize : DWORD -> "int" ; Arguments : CHAR** optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD FormatMessageA(FORMAT_MESSAGE_OPTIONS dwFlags, void* lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize, CHAR** Arguments) #uselib "KERNEL32.dll" #cfunc global FormatMessageA "FormatMessageA" int, intptr, int, int, intptr, int, intptr ; res = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, varptr(lpBuffer), nSize, varptr(Arguments)) ; dwFlags : FORMAT_MESSAGE_OPTIONS -> "int" ; lpSource : void* optional -> "intptr" ; dwMessageId : DWORD -> "int" ; dwLanguageId : DWORD -> "int" ; lpBuffer : LPSTR out -> "intptr" ; nSize : DWORD -> "int" ; Arguments : CHAR** optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procFormatMessageA = kernel32.NewProc("FormatMessageA")
)
// dwFlags (FORMAT_MESSAGE_OPTIONS), lpSource (void* optional), dwMessageId (DWORD), dwLanguageId (DWORD), lpBuffer (LPSTR out), nSize (DWORD), Arguments (CHAR** optional)
r1, _, err := procFormatMessageA.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 // DWORDfunction FormatMessageA(
dwFlags: DWORD; // FORMAT_MESSAGE_OPTIONS
lpSource: Pointer; // void* optional
dwMessageId: DWORD; // DWORD
dwLanguageId: DWORD; // DWORD
lpBuffer: PAnsiChar; // LPSTR out
nSize: DWORD; // DWORD
Arguments: Pointer // CHAR** optional
): DWORD; stdcall;
external 'KERNEL32.dll' name 'FormatMessageA';result := DllCall("KERNEL32\FormatMessageA"
, "UInt", dwFlags ; FORMAT_MESSAGE_OPTIONS
, "Ptr", lpSource ; void* optional
, "UInt", dwMessageId ; DWORD
, "UInt", dwLanguageId ; DWORD
, "Ptr", lpBuffer ; LPSTR out
, "UInt", nSize ; DWORD
, "Ptr", Arguments ; CHAR** optional
, "UInt") ; return: DWORD●FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments) = DLL("KERNEL32.dll", "dword FormatMessageA(dword, void*, dword, dword, char*, dword, void*)")
# 呼び出し: FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments)
# dwFlags : FORMAT_MESSAGE_OPTIONS -> "dword"
# lpSource : void* optional -> "void*"
# dwMessageId : DWORD -> "dword"
# dwLanguageId : DWORD -> "dword"
# lpBuffer : LPSTR out -> "char*"
# nSize : DWORD -> "dword"
# Arguments : CHAR** optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。