Win32 API 日本語リファレンス
ホームGraphics.Gdi › GetMonitorInfoW

GetMonitorInfoW

関数
指定したモニタの情報を取得する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

BOOL GetMonitorInfoW(
    HMONITOR hMonitor,
    MONITORINFO* lpmi
);

パラメーター

名前方向説明
hMonitorHMONITORin対象となるディスプレイモニターへのハンドルです。
lpmiMONITORINFO*inout

指定したディスプレイモニターに関する情報を受け取る MONITORINFO 構造体または MONITORINFOEX 構造体へのポインターです。

GetMonitorInfo 関数を呼び出す前に、構造体の cbSize メンバーを sizeof(MONITORINFO) または sizeof(MONITORINFOEX) に設定する必要があります。これにより、関数は渡された構造体の種類を判別できます。

MONITORINFOEX 構造体は MONITORINFO 構造体のスーパーセットです。これには追加のメンバーが 1 つあり、ディスプレイモニターの名前を格納する文字列です。ほとんどのアプリケーションではディスプレイモニターの名前を使用しないため、MONITORINFO 構造体を使用することで数バイトを節約できます。

戻り値の型: BOOL

公式ドキュメント

GetMonitorInfo 関数は、ディスプレイモニターに関する情報を取得します。(Unicode)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。

解説(Remarks)

メモ

winuser.h ヘッダーは、GetMonitorInfo を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディングに依存しないこのエイリアスを、エンコーディングに依存しないわけではないコードと混在させて使用すると、不一致が生じてコンパイルエラーや実行時エラーの原因となることがあります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL GetMonitorInfoW(
    HMONITOR hMonitor,
    MONITORINFO* lpmi
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool GetMonitorInfoW(
    IntPtr hMonitor,   // HMONITOR
    IntPtr lpmi   // MONITORINFO* in/out
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function GetMonitorInfoW(
    hMonitor As IntPtr,   ' HMONITOR
    lpmi As IntPtr   ' MONITORINFO* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hMonitor : HMONITOR
' lpmi : MONITORINFO* in/out
Declare PtrSafe Function GetMonitorInfoW Lib "user32" ( _
    ByVal hMonitor As LongPtr, _
    ByVal lpmi 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

GetMonitorInfoW = ctypes.windll.user32.GetMonitorInfoW
GetMonitorInfoW.restype = wintypes.BOOL
GetMonitorInfoW.argtypes = [
    wintypes.HANDLE,  # hMonitor : HMONITOR
    ctypes.c_void_p,  # lpmi : MONITORINFO* in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
)

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

extern "user32" fn GetMonitorInfoW(
    hMonitor: ?*anyopaque, // HMONITOR
    lpmi: [*c]MONITORINFO // MONITORINFO* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetMonitorInfoW(
    hMonitor: pointer,  # HMONITOR
    lpmi: ptr MONITORINFO  # MONITORINFO* in/out
): int32 {.importc: "GetMonitorInfoW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "user32");
extern(Windows)
int GetMonitorInfoW(
    void* hMonitor,   // HMONITOR
    MONITORINFO* lpmi   // MONITORINFO* in/out
);
ccall((:GetMonitorInfoW, "USER32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{MONITORINFO}),
      hMonitor, lpmi)
# hMonitor : HMONITOR -> Ptr{Cvoid}
# lpmi : MONITORINFO* in/out -> Ptr{MONITORINFO}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetMonitorInfoW(
    void* hMonitor,
    void* lpmi);
]]
local user32 = ffi.load("user32")
-- user32.GetMonitorInfoW(hMonitor, lpmi)
-- hMonitor : HMONITOR
-- lpmi : MONITORINFO* 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('USER32.dll');
const GetMonitorInfoW = lib.func('__stdcall', 'GetMonitorInfoW', 'int32_t', ['void *', 'void *']);
// GetMonitorInfoW(hMonitor, lpmi)
// hMonitor : HMONITOR -> 'void *'
// lpmi : MONITORINFO* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  GetMonitorInfoW: { parameters: ["pointer", "pointer"], result: "i32" },
});
// lib.symbols.GetMonitorInfoW(hMonitor, lpmi)
// hMonitor : HMONITOR -> "pointer"
// lpmi : MONITORINFO* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetMonitorInfoW(
    void* hMonitor,
    void* lpmi);
C, "USER32.dll");
// $ffi->GetMonitorInfoW(hMonitor, lpmi);
// hMonitor : HMONITOR
// lpmi : MONITORINFO* 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 User32 extends StdCallLibrary {
    User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetMonitorInfoW(
        Pointer hMonitor,   // HMONITOR
        Pointer lpmi   // MONITORINFO* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("user32")]
lib LibUSER32
  fun GetMonitorInfoW = GetMonitorInfoW(
    hMonitor : Void*,   # HMONITOR
    lpmi : MONITORINFO*   # MONITORINFO* 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 GetMonitorInfoWNative = Int32 Function(Pointer<Void>, Pointer<Void>);
typedef GetMonitorInfoWDart = int Function(Pointer<Void>, Pointer<Void>);
final GetMonitorInfoW = DynamicLibrary.open('USER32.dll')
    .lookupFunction<GetMonitorInfoWNative, GetMonitorInfoWDart>('GetMonitorInfoW');
// hMonitor : HMONITOR -> Pointer<Void>
// lpmi : MONITORINFO* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetMonitorInfoW(
  hMonitor: THandle;   // HMONITOR
  lpmi: Pointer   // MONITORINFO* in/out
): BOOL; stdcall;
  external 'USER32.dll' name 'GetMonitorInfoW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetMonitorInfoW"
  c_GetMonitorInfoW :: Ptr () -> Ptr () -> IO CInt
-- hMonitor : HMONITOR -> Ptr ()
-- lpmi : MONITORINFO* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getmonitorinfow =
  foreign "GetMonitorInfoW"
    ((ptr void) @-> (ptr void) @-> returning int32_t)
(* hMonitor : HMONITOR -> (ptr void) *)
(* lpmi : MONITORINFO* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("GetMonitorInfoW" get-monitor-info-w :convention :stdcall) :int32
  (h-monitor :pointer)   ; HMONITOR
  (lpmi :pointer))   ; MONITORINFO* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetMonitorInfoW = Win32::API::More->new('USER32',
    'BOOL GetMonitorInfoW(HANDLE hMonitor, LPVOID lpmi)');
# my $ret = $GetMonitorInfoW->Call($hMonitor, $lpmi);
# hMonitor : HMONITOR -> HANDLE
# lpmi : MONITORINFO* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目
使用する型