Win32 API 日本語リファレンス
ホームSystem.Console › ReadConsoleInputW

ReadConsoleInputW

関数
コンソール入力バッファからイベントレコードを読み取る(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり

シグネチャ

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

BOOL ReadConsoleInputW(
    HANDLE hConsoleInput,
    INPUT_RECORD* lpBuffer,
    DWORD nLength,
    DWORD* lpNumberOfEventsRead
);

パラメーター

名前方向
hConsoleInputHANDLEin
lpBufferINPUT_RECORD*out
nLengthDWORDin
lpNumberOfEventsReadDWORD*out

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL ReadConsoleInputW(
    HANDLE hConsoleInput,
    INPUT_RECORD* lpBuffer,
    DWORD nLength,
    DWORD* lpNumberOfEventsRead
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool ReadConsoleInputW(
    IntPtr hConsoleInput,   // HANDLE
    IntPtr lpBuffer,   // INPUT_RECORD* out
    uint nLength,   // DWORD
    out uint lpNumberOfEventsRead   // DWORD* out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ReadConsoleInputW(
    hConsoleInput As IntPtr,   ' HANDLE
    lpBuffer As IntPtr,   ' INPUT_RECORD* out
    nLength As UInteger,   ' DWORD
    <Out> ByRef lpNumberOfEventsRead As UInteger   ' DWORD* out
) As Boolean
End Function
' hConsoleInput : HANDLE
' lpBuffer : INPUT_RECORD* out
' nLength : DWORD
' lpNumberOfEventsRead : DWORD* out
Declare PtrSafe Function ReadConsoleInputW Lib "kernel32" ( _
    ByVal hConsoleInput As LongPtr, _
    ByVal lpBuffer As LongPtr, _
    ByVal nLength As Long, _
    ByRef lpNumberOfEventsRead 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

ReadConsoleInputW = ctypes.windll.kernel32.ReadConsoleInputW
ReadConsoleInputW.restype = wintypes.BOOL
ReadConsoleInputW.argtypes = [
    wintypes.HANDLE,  # hConsoleInput : HANDLE
    ctypes.c_void_p,  # lpBuffer : INPUT_RECORD* out
    wintypes.DWORD,  # nLength : DWORD
    ctypes.POINTER(wintypes.DWORD),  # lpNumberOfEventsRead : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
ReadConsoleInputW = Fiddle::Function.new(
  lib['ReadConsoleInputW'],
  [
    Fiddle::TYPE_VOIDP,  # hConsoleInput : HANDLE
    Fiddle::TYPE_VOIDP,  # lpBuffer : INPUT_RECORD* out
    -Fiddle::TYPE_INT,  # nLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpNumberOfEventsRead : DWORD* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn ReadConsoleInputW(
        hConsoleInput: *mut core::ffi::c_void,  // HANDLE
        lpBuffer: *mut INPUT_RECORD,  // INPUT_RECORD* out
        nLength: u32,  // DWORD
        lpNumberOfEventsRead: *mut u32  // DWORD* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ReadConsoleInputW(IntPtr hConsoleInput, IntPtr lpBuffer, uint nLength, out uint lpNumberOfEventsRead);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_ReadConsoleInputW' -Namespace Win32 -PassThru
# $api::ReadConsoleInputW(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead)
#uselib "KERNEL32.dll"
#func global ReadConsoleInputW "ReadConsoleInputW" wptr, wptr, wptr, wptr
; ReadConsoleInputW hConsoleInput, varptr(lpBuffer), nLength, varptr(lpNumberOfEventsRead)   ; 戻り値は stat
; hConsoleInput : HANDLE -> "wptr"
; lpBuffer : INPUT_RECORD* out -> "wptr"
; nLength : DWORD -> "wptr"
; lpNumberOfEventsRead : DWORD* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global ReadConsoleInputW "ReadConsoleInputW" sptr, var, int, var
; res = ReadConsoleInputW(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead)
; hConsoleInput : HANDLE -> "sptr"
; lpBuffer : INPUT_RECORD* out -> "var"
; nLength : DWORD -> "int"
; lpNumberOfEventsRead : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL ReadConsoleInputW(HANDLE hConsoleInput, INPUT_RECORD* lpBuffer, DWORD nLength, DWORD* lpNumberOfEventsRead)
#uselib "KERNEL32.dll"
#cfunc global ReadConsoleInputW "ReadConsoleInputW" intptr, var, int, var
; res = ReadConsoleInputW(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead)
; hConsoleInput : HANDLE -> "intptr"
; lpBuffer : INPUT_RECORD* out -> "var"
; nLength : DWORD -> "int"
; lpNumberOfEventsRead : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procReadConsoleInputW = kernel32.NewProc("ReadConsoleInputW")
)

// hConsoleInput (HANDLE), lpBuffer (INPUT_RECORD* out), nLength (DWORD), lpNumberOfEventsRead (DWORD* out)
r1, _, err := procReadConsoleInputW.Call(
	uintptr(hConsoleInput),
	uintptr(lpBuffer),
	uintptr(nLength),
	uintptr(lpNumberOfEventsRead),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function ReadConsoleInputW(
  hConsoleInput: THandle;   // HANDLE
  lpBuffer: Pointer;   // INPUT_RECORD* out
  nLength: DWORD;   // DWORD
  lpNumberOfEventsRead: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'ReadConsoleInputW';
result := DllCall("KERNEL32\ReadConsoleInputW"
    , "Ptr", hConsoleInput   ; HANDLE
    , "Ptr", lpBuffer   ; INPUT_RECORD* out
    , "UInt", nLength   ; DWORD
    , "Ptr", lpNumberOfEventsRead   ; DWORD* out
    , "Int")   ; return: BOOL
●ReadConsoleInputW(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead) = DLL("KERNEL32.dll", "bool ReadConsoleInputW(void*, void*, dword, void*)")
# 呼び出し: ReadConsoleInputW(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead)
# hConsoleInput : HANDLE -> "void*"
# lpBuffer : INPUT_RECORD* out -> "void*"
# nLength : DWORD -> "dword"
# lpNumberOfEventsRead : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。