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

ReadConsoleOutputW

関数
コンソール画面バッファから文字と属性の矩形ブロックを読み取る(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり

シグネチャ

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

BOOL ReadConsoleOutputW(
    HANDLE hConsoleOutput,
    CHAR_INFO* lpBuffer,
    COORD dwBufferSize,
    COORD dwBufferCoord,
    SMALL_RECT* lpReadRegion
);

パラメーター

名前方向説明
hConsoleOutputHANDLEin対象のコンソール画面バッファのハンドル。
lpBufferCHAR_INFO*out読み取った文字と属性を受け取るCHAR_INFO配列へのポインタ。
dwBufferSizeCOORDinlpBufferの寸法(列数・行数)を示すCOORD値。
dwBufferCoordCOORDinlpBuffer内で格納を開始するセルの座標を示すCOORD値。
lpReadRegionSMALL_RECT*inout画面上の読み取り対象範囲を示すSMALL_RECT構造体へのポインタ。実際の範囲が返る。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL ReadConsoleOutputW(
    HANDLE hConsoleOutput,
    CHAR_INFO* lpBuffer,
    COORD dwBufferSize,
    COORD dwBufferCoord,
    SMALL_RECT* lpReadRegion
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool ReadConsoleOutputW(
    IntPtr hConsoleOutput,   // HANDLE
    IntPtr lpBuffer,   // CHAR_INFO* out
    COORD dwBufferSize,   // COORD
    COORD dwBufferCoord,   // COORD
    IntPtr lpReadRegion   // SMALL_RECT* in/out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ReadConsoleOutputW(
    hConsoleOutput As IntPtr,   ' HANDLE
    lpBuffer As IntPtr,   ' CHAR_INFO* out
    dwBufferSize As COORD,   ' COORD
    dwBufferCoord As COORD,   ' COORD
    lpReadRegion As IntPtr   ' SMALL_RECT* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hConsoleOutput : HANDLE
' lpBuffer : CHAR_INFO* out
' dwBufferSize : COORD
' dwBufferCoord : COORD
' lpReadRegion : SMALL_RECT* in/out
Declare PtrSafe Function ReadConsoleOutputW Lib "kernel32" ( _
    ByVal hConsoleOutput As LongPtr, _
    ByVal lpBuffer As LongPtr, _
    ByVal dwBufferSize As LongPtr, _
    ByVal dwBufferCoord As LongPtr, _
    ByVal lpReadRegion 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

ReadConsoleOutputW = ctypes.windll.kernel32.ReadConsoleOutputW
ReadConsoleOutputW.restype = wintypes.BOOL
ReadConsoleOutputW.argtypes = [
    wintypes.HANDLE,  # hConsoleOutput : HANDLE
    ctypes.c_void_p,  # lpBuffer : CHAR_INFO* out
    COORD,  # dwBufferSize : COORD
    COORD,  # dwBufferCoord : COORD
    ctypes.c_void_p,  # lpReadRegion : SMALL_RECT* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
ReadConsoleOutputW = Fiddle::Function.new(
  lib['ReadConsoleOutputW'],
  [
    Fiddle::TYPE_VOIDP,  # hConsoleOutput : HANDLE
    Fiddle::TYPE_VOIDP,  # lpBuffer : CHAR_INFO* out
    Fiddle::TYPE_VOIDP,  # dwBufferSize : COORD
    Fiddle::TYPE_VOIDP,  # dwBufferCoord : COORD
    Fiddle::TYPE_VOIDP,  # lpReadRegion : SMALL_RECT* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn ReadConsoleOutputW(
        hConsoleOutput: *mut core::ffi::c_void,  // HANDLE
        lpBuffer: *mut CHAR_INFO,  // CHAR_INFO* out
        dwBufferSize: COORD,  // COORD
        dwBufferCoord: COORD,  // COORD
        lpReadRegion: *mut SMALL_RECT  // SMALL_RECT* in/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 ReadConsoleOutputW(IntPtr hConsoleOutput, IntPtr lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, IntPtr lpReadRegion);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_ReadConsoleOutputW' -Namespace Win32 -PassThru
# $api::ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
#uselib "KERNEL32.dll"
#func global ReadConsoleOutputW "ReadConsoleOutputW" wptr, wptr, wptr, wptr, wptr
; ReadConsoleOutputW hConsoleOutput, varptr(lpBuffer), dwBufferSize, dwBufferCoord, varptr(lpReadRegion)   ; 戻り値は stat
; hConsoleOutput : HANDLE -> "wptr"
; lpBuffer : CHAR_INFO* out -> "wptr"
; dwBufferSize : COORD -> "wptr"
; dwBufferCoord : COORD -> "wptr"
; lpReadRegion : SMALL_RECT* in/out -> "wptr"
; ※値渡し構造体は直接渡せません。intにパック、または var で構造体変数を渡してください。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global ReadConsoleOutputW "ReadConsoleOutputW" sptr, var, int, int, var
; res = ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
; hConsoleOutput : HANDLE -> "sptr"
; lpBuffer : CHAR_INFO* out -> "var"
; dwBufferSize : COORD -> "int"
; dwBufferCoord : COORD -> "int"
; lpReadRegion : SMALL_RECT* in/out -> "var"
; ※値渡し構造体は直接渡せません。intにパック、または var で構造体変数を渡してください。
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL ReadConsoleOutputW(HANDLE hConsoleOutput, CHAR_INFO* lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, SMALL_RECT* lpReadRegion)
#uselib "KERNEL32.dll"
#cfunc global ReadConsoleOutputW "ReadConsoleOutputW" intptr, var, int, int, var
; res = ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
; hConsoleOutput : HANDLE -> "intptr"
; lpBuffer : CHAR_INFO* out -> "var"
; dwBufferSize : COORD -> "int"
; dwBufferCoord : COORD -> "int"
; lpReadRegion : SMALL_RECT* in/out -> "var"
; ※値渡し構造体は直接渡せません。intにパック、または var で構造体変数を渡してください。
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procReadConsoleOutputW = kernel32.NewProc("ReadConsoleOutputW")
)

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

extern "kernel32" fn ReadConsoleOutputW(
    hConsoleOutput: ?*anyopaque, // HANDLE
    lpBuffer: [*c]CHAR_INFO, // CHAR_INFO* out
    dwBufferSize: COORD, // COORD
    dwBufferCoord: COORD, // COORD
    lpReadRegion: [*c]SMALL_RECT // SMALL_RECT* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc ReadConsoleOutputW(
    hConsoleOutput: pointer,  # HANDLE
    lpBuffer: ptr CHAR_INFO,  # CHAR_INFO* out
    dwBufferSize: COORD,  # COORD
    dwBufferCoord: COORD,  # COORD
    lpReadRegion: ptr SMALL_RECT  # SMALL_RECT* in/out
): int32 {.importc: "ReadConsoleOutputW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "kernel32");
extern(Windows)
int ReadConsoleOutputW(
    void* hConsoleOutput,   // HANDLE
    CHAR_INFO* lpBuffer,   // CHAR_INFO* out
    COORD dwBufferSize,   // COORD
    COORD dwBufferCoord,   // COORD
    SMALL_RECT* lpReadRegion   // SMALL_RECT* in/out
);
ccall((:ReadConsoleOutputW, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{CHAR_INFO}, COORD, COORD, Ptr{SMALL_RECT}),
      hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
# hConsoleOutput : HANDLE -> Ptr{Cvoid}
# lpBuffer : CHAR_INFO* out -> Ptr{CHAR_INFO}
# dwBufferSize : COORD -> COORD
# dwBufferCoord : COORD -> COORD
# lpReadRegion : SMALL_RECT* in/out -> Ptr{SMALL_RECT}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t ReadConsoleOutputW(
    void* hConsoleOutput,
    void* lpBuffer,
    COORD dwBufferSize,
    COORD dwBufferCoord,
    void* lpReadRegion);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
-- hConsoleOutput : HANDLE
-- lpBuffer : CHAR_INFO* out
-- dwBufferSize : COORD
-- dwBufferCoord : COORD
-- lpReadRegion : SMALL_RECT* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const ReadConsoleOutputW = lib.func('__stdcall', 'ReadConsoleOutputW', 'int32_t', ['void *', 'void *', 'COORD', 'COORD', 'void *']);
// ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
// hConsoleOutput : HANDLE -> 'void *'
// lpBuffer : CHAR_INFO* out -> 'void *'
// dwBufferSize : COORD -> 'COORD'
// dwBufferCoord : COORD -> 'COORD'
// lpReadRegion : SMALL_RECT* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  ReadConsoleOutputW: { parameters: ["pointer", "pointer", "buffer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion)
// hConsoleOutput : HANDLE -> "pointer"
// lpBuffer : CHAR_INFO* out -> "pointer"
// dwBufferSize : COORD -> "buffer"
// dwBufferCoord : COORD -> "buffer"
// lpReadRegion : SMALL_RECT* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t ReadConsoleOutputW(
    void* hConsoleOutput,
    void* lpBuffer,
    COORD dwBufferSize,
    COORD dwBufferCoord,
    void* lpReadRegion);
C, "KERNEL32.dll");
// $ffi->ReadConsoleOutputW(hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion);
// hConsoleOutput : HANDLE
// lpBuffer : CHAR_INFO* out
// dwBufferSize : COORD
// dwBufferCoord : COORD
// lpReadRegion : SMALL_RECT* in/out
// 構造体/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 Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean ReadConsoleOutputW(
        Pointer hConsoleOutput,   // HANDLE
        Pointer lpBuffer,   // CHAR_INFO* out
        COORD /* extends Structure (by value) */ dwBufferSize,   // COORD
        COORD /* extends Structure (by value) */ dwBufferCoord,   // COORD
        Pointer lpReadRegion   // SMALL_RECT* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("kernel32")]
lib LibKERNEL32
  fun ReadConsoleOutputW = ReadConsoleOutputW(
    hConsoleOutput : Void*,   # HANDLE
    lpBuffer : CHAR_INFO*,   # CHAR_INFO* out
    dwBufferSize : COORD,   # COORD
    dwBufferCoord : COORD,   # COORD
    lpReadRegion : SMALL_RECT*   # SMALL_RECT* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef ReadConsoleOutputWNative = Int32 Function(Pointer<Void>, Pointer<Void>, COORD, COORD, Pointer<Void>);
typedef ReadConsoleOutputWDart = int Function(Pointer<Void>, Pointer<Void>, int, int, Pointer<Void>);
final ReadConsoleOutputW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<ReadConsoleOutputWNative, ReadConsoleOutputWDart>('ReadConsoleOutputW');
// hConsoleOutput : HANDLE -> Pointer<Void>
// lpBuffer : CHAR_INFO* out -> Pointer<Void>
// dwBufferSize : COORD -> COORD
// dwBufferCoord : COORD -> COORD
// lpReadRegion : SMALL_RECT* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function ReadConsoleOutputW(
  hConsoleOutput: THandle;   // HANDLE
  lpBuffer: Pointer;   // CHAR_INFO* out
  dwBufferSize: COORD;   // COORD
  dwBufferCoord: COORD;   // COORD
  lpReadRegion: Pointer   // SMALL_RECT* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'ReadConsoleOutputW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "ReadConsoleOutputW"
  c_ReadConsoleOutputW :: Ptr () -> Ptr () -> COORD -> COORD -> Ptr () -> IO CInt
-- hConsoleOutput : HANDLE -> Ptr ()
-- lpBuffer : CHAR_INFO* out -> Ptr ()
-- dwBufferSize : COORD -> COORD
-- dwBufferCoord : COORD -> COORD
-- lpReadRegion : SMALL_RECT* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
-- ※値渡し構造体は GHC FFI で直接渡せません。Ptr で渡すラッパ(C側)経由にしてください。
open Ctypes
open Foreign

let readconsoleoutputw =
  foreign "ReadConsoleOutputW"
    ((ptr void) @-> (ptr void) @-> COORD @-> COORD @-> (ptr void) @-> returning int32_t)
(* hConsoleOutput : HANDLE -> (ptr void) *)
(* lpBuffer : CHAR_INFO* out -> (ptr void) *)
(* dwBufferSize : COORD -> COORD *)
(* dwBufferCoord : COORD -> COORD *)
(* lpReadRegion : SMALL_RECT* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("ReadConsoleOutputW" read-console-output-w :convention :stdcall) :int32
  (h-console-output :pointer)   ; HANDLE
  (lp-buffer :pointer)   ; CHAR_INFO* out
  (dw-buffer-size (:struct coord))   ; COORD
  (dw-buffer-coord (:struct coord))   ; COORD
  (lp-read-region :pointer))   ; SMALL_RECT* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ReadConsoleOutputW = Win32::API::More->new('KERNEL32',
    'BOOL ReadConsoleOutputW(HANDLE hConsoleOutput, LPVOID lpBuffer, LPVOID dwBufferSize, LPVOID dwBufferCoord, LPVOID lpReadRegion)');
# my $ret = $ReadConsoleOutputW->Call($hConsoleOutput, $lpBuffer, $dwBufferSize, $dwBufferCoord, $lpReadRegion);
# hConsoleOutput : HANDLE -> HANDLE
# lpBuffer : CHAR_INFO* out -> LPVOID
# dwBufferSize : COORD -> LPVOID
# dwBufferCoord : COORD -> LPVOID
# lpReadRegion : SMALL_RECT* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型