Win32 API 日本語リファレンス
ホームDevices.Display › GetPhysicalMonitorsFromHMONITOR

GetPhysicalMonitorsFromHMONITOR

関数
HMONITORから物理モニタのハンドル配列を取得する。
DLLdxva2.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// dxva2.dll
#include <windows.h>

BOOL GetPhysicalMonitorsFromHMONITOR(
    HMONITOR hMonitor,
    DWORD dwPhysicalMonitorArraySize,
    PHYSICAL_MONITOR* pPhysicalMonitorArray
);

パラメーター

名前方向説明
hMonitorHMONITORinモニターハンドルです。モニターハンドルは、グラフィックスデバイスインターフェイス (GDI) の一部である EnumDisplayMonitorsMonitorFromWindow など、複数のマルチディスプレイモニター関数から返されます。
dwPhysicalMonitorArraySizeDWORDinpPhysicalMonitorArray の要素数です。配列に必要なサイズを取得するには、GetNumberOfPhysicalMonitorsFromHMONITOR を呼び出してください。
pPhysicalMonitorArrayPHYSICAL_MONITOR*outPHYSICAL_MONITOR 構造体の配列へのポインターです。呼び出し側が配列を割り当てる必要があります。

戻り値の型: BOOL

公式ドキュメント

HMONITOR モニターハンドルに関連付けられた物理モニターを取得します。

戻り値

関数が成功した場合、戻り値は TRUE です。関数が失敗した場合、戻り値は FALSE です。拡張エラー情報を取得するには、GetLastError を呼び出してください。

解説(Remarks)

1 つの HMONITOR ハンドルは、複数の物理モニターに関連付けられる場合があります。この関数は、各物理モニターに対するハンドルとテキストによる説明を返します。

モニターハンドルの使用を終えたら、pPhysicalMonitorArray 配列を DestroyPhysicalMonitors 関数に渡してハンドルを閉じてください。


HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;

// モニターハンドルを取得します。
hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);

// 物理モニターの数を取得します。
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(
  hMonitor, 
  &cPhysicalMonitors
   );

if (bSuccess)
{
    // PHYSICAL_MONITOR 構造体の配列を割り当てます。
    pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
        cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));

    if (pPhysicalMonitors != NULL)
    {
        // 配列を取得します。
        bSuccess = GetPhysicalMonitorsFromHMONITOR(
            hMonitor, cPhysicalMonitors, pPhysicalMonitors);

       // モニターハンドルを使用します (省略)。

        // モニターハンドルを閉じます。
        bSuccess = DestroyPhysicalMonitors(
            cPhysicalMonitors, 
            pPhysicalMonitors);

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

各言語での呼び出し定義

// dxva2.dll
#include <windows.h>

BOOL GetPhysicalMonitorsFromHMONITOR(
    HMONITOR hMonitor,
    DWORD dwPhysicalMonitorArraySize,
    PHYSICAL_MONITOR* pPhysicalMonitorArray
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("dxva2.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetPhysicalMonitorsFromHMONITOR(
    IntPtr hMonitor,   // HMONITOR
    uint dwPhysicalMonitorArraySize,   // DWORD
    IntPtr pPhysicalMonitorArray   // PHYSICAL_MONITOR* out
);
<DllImport("dxva2.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetPhysicalMonitorsFromHMONITOR(
    hMonitor As IntPtr,   ' HMONITOR
    dwPhysicalMonitorArraySize As UInteger,   ' DWORD
    pPhysicalMonitorArray As IntPtr   ' PHYSICAL_MONITOR* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hMonitor : HMONITOR
' dwPhysicalMonitorArraySize : DWORD
' pPhysicalMonitorArray : PHYSICAL_MONITOR* out
Declare PtrSafe Function GetPhysicalMonitorsFromHMONITOR Lib "dxva2" ( _
    ByVal hMonitor As LongPtr, _
    ByVal dwPhysicalMonitorArraySize As Long, _
    ByVal pPhysicalMonitorArray As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetPhysicalMonitorsFromHMONITOR = ctypes.windll.dxva2.GetPhysicalMonitorsFromHMONITOR
GetPhysicalMonitorsFromHMONITOR.restype = wintypes.BOOL
GetPhysicalMonitorsFromHMONITOR.argtypes = [
    wintypes.HANDLE,  # hMonitor : HMONITOR
    wintypes.DWORD,  # dwPhysicalMonitorArraySize : DWORD
    ctypes.c_void_p,  # pPhysicalMonitorArray : PHYSICAL_MONITOR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	dxva2 = windows.NewLazySystemDLL("dxva2.dll")
	procGetPhysicalMonitorsFromHMONITOR = dxva2.NewProc("GetPhysicalMonitorsFromHMONITOR")
)

// hMonitor (HMONITOR), dwPhysicalMonitorArraySize (DWORD), pPhysicalMonitorArray (PHYSICAL_MONITOR* out)
r1, _, err := procGetPhysicalMonitorsFromHMONITOR.Call(
	uintptr(hMonitor),
	uintptr(dwPhysicalMonitorArraySize),
	uintptr(pPhysicalMonitorArray),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetPhysicalMonitorsFromHMONITOR(
  hMonitor: THandle;   // HMONITOR
  dwPhysicalMonitorArraySize: DWORD;   // DWORD
  pPhysicalMonitorArray: Pointer   // PHYSICAL_MONITOR* out
): BOOL; stdcall;
  external 'dxva2.dll' name 'GetPhysicalMonitorsFromHMONITOR';
result := DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR"
    , "Ptr", hMonitor   ; HMONITOR
    , "UInt", dwPhysicalMonitorArraySize   ; DWORD
    , "Ptr", pPhysicalMonitorArray   ; PHYSICAL_MONITOR* out
    , "Int")   ; return: BOOL
●GetPhysicalMonitorsFromHMONITOR(hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray) = DLL("dxva2.dll", "bool GetPhysicalMonitorsFromHMONITOR(void*, dword, void*)")
# 呼び出し: GetPhysicalMonitorsFromHMONITOR(hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray)
# hMonitor : HMONITOR -> "void*"
# dwPhysicalMonitorArraySize : DWORD -> "dword"
# pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "dxva2" fn GetPhysicalMonitorsFromHMONITOR(
    hMonitor: ?*anyopaque, // HMONITOR
    dwPhysicalMonitorArraySize: u32, // DWORD
    pPhysicalMonitorArray: [*c]PHYSICAL_MONITOR // PHYSICAL_MONITOR* out
) callconv(std.os.windows.WINAPI) i32;
proc GetPhysicalMonitorsFromHMONITOR(
    hMonitor: pointer,  # HMONITOR
    dwPhysicalMonitorArraySize: uint32,  # DWORD
    pPhysicalMonitorArray: ptr PHYSICAL_MONITOR  # PHYSICAL_MONITOR* out
): int32 {.importc: "GetPhysicalMonitorsFromHMONITOR", stdcall, dynlib: "dxva2.dll".}
pragma(lib, "dxva2");
extern(Windows)
int GetPhysicalMonitorsFromHMONITOR(
    void* hMonitor,   // HMONITOR
    uint dwPhysicalMonitorArraySize,   // DWORD
    PHYSICAL_MONITOR* pPhysicalMonitorArray   // PHYSICAL_MONITOR* out
);
ccall((:GetPhysicalMonitorsFromHMONITOR, "dxva2.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, Ptr{PHYSICAL_MONITOR}),
      hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray)
# hMonitor : HMONITOR -> Ptr{Cvoid}
# dwPhysicalMonitorArraySize : DWORD -> UInt32
# pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> Ptr{PHYSICAL_MONITOR}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetPhysicalMonitorsFromHMONITOR(
    void* hMonitor,
    uint32_t dwPhysicalMonitorArraySize,
    void* pPhysicalMonitorArray);
]]
local dxva2 = ffi.load("dxva2")
-- dxva2.GetPhysicalMonitorsFromHMONITOR(hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray)
-- hMonitor : HMONITOR
-- dwPhysicalMonitorArraySize : DWORD
-- pPhysicalMonitorArray : PHYSICAL_MONITOR* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('dxva2.dll');
const GetPhysicalMonitorsFromHMONITOR = lib.func('__stdcall', 'GetPhysicalMonitorsFromHMONITOR', 'int32_t', ['void *', 'uint32_t', 'void *']);
// GetPhysicalMonitorsFromHMONITOR(hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray)
// hMonitor : HMONITOR -> 'void *'
// dwPhysicalMonitorArraySize : DWORD -> 'uint32_t'
// pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("dxva2.dll", {
  GetPhysicalMonitorsFromHMONITOR: { parameters: ["pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.GetPhysicalMonitorsFromHMONITOR(hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray)
// hMonitor : HMONITOR -> "pointer"
// dwPhysicalMonitorArraySize : DWORD -> "u32"
// pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetPhysicalMonitorsFromHMONITOR(
    void* hMonitor,
    uint32_t dwPhysicalMonitorArraySize,
    void* pPhysicalMonitorArray);
C, "dxva2.dll");
// $ffi->GetPhysicalMonitorsFromHMONITOR(hMonitor, dwPhysicalMonitorArraySize, pPhysicalMonitorArray);
// hMonitor : HMONITOR
// dwPhysicalMonitorArraySize : DWORD
// pPhysicalMonitorArray : PHYSICAL_MONITOR* 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 Dxva2 extends StdCallLibrary {
    Dxva2 INSTANCE = Native.load("dxva2", Dxva2.class);
    boolean GetPhysicalMonitorsFromHMONITOR(
        Pointer hMonitor,   // HMONITOR
        int dwPhysicalMonitorArraySize,   // DWORD
        Pointer pPhysicalMonitorArray   // PHYSICAL_MONITOR* out
    );
}
@[Link("dxva2")]
lib Libdxva2
  fun GetPhysicalMonitorsFromHMONITOR = GetPhysicalMonitorsFromHMONITOR(
    hMonitor : Void*,   # HMONITOR
    dwPhysicalMonitorArraySize : UInt32,   # DWORD
    pPhysicalMonitorArray : PHYSICAL_MONITOR*   # PHYSICAL_MONITOR* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetPhysicalMonitorsFromHMONITORNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Void>);
typedef GetPhysicalMonitorsFromHMONITORDart = int Function(Pointer<Void>, int, Pointer<Void>);
final GetPhysicalMonitorsFromHMONITOR = DynamicLibrary.open('dxva2.dll')
    .lookupFunction<GetPhysicalMonitorsFromHMONITORNative, GetPhysicalMonitorsFromHMONITORDart>('GetPhysicalMonitorsFromHMONITOR');
// hMonitor : HMONITOR -> Pointer<Void>
// dwPhysicalMonitorArraySize : DWORD -> Uint32
// pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetPhysicalMonitorsFromHMONITOR(
  hMonitor: THandle;   // HMONITOR
  dwPhysicalMonitorArraySize: DWORD;   // DWORD
  pPhysicalMonitorArray: Pointer   // PHYSICAL_MONITOR* out
): BOOL; stdcall;
  external 'dxva2.dll' name 'GetPhysicalMonitorsFromHMONITOR';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetPhysicalMonitorsFromHMONITOR"
  c_GetPhysicalMonitorsFromHMONITOR :: Ptr () -> Word32 -> Ptr () -> IO CInt
-- hMonitor : HMONITOR -> Ptr ()
-- dwPhysicalMonitorArraySize : DWORD -> Word32
-- pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getphysicalmonitorsfromhmonitor =
  foreign "GetPhysicalMonitorsFromHMONITOR"
    ((ptr void) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* hMonitor : HMONITOR -> (ptr void) *)
(* dwPhysicalMonitorArraySize : DWORD -> uint32_t *)
(* pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dxva2 (t "dxva2.dll"))
(cffi:use-foreign-library dxva2)

(cffi:defcfun ("GetPhysicalMonitorsFromHMONITOR" get-physical-monitors-from-hmonitor :convention :stdcall) :int32
  (h-monitor :pointer)   ; HMONITOR
  (dw-physical-monitor-array-size :uint32)   ; DWORD
  (p-physical-monitor-array :pointer))   ; PHYSICAL_MONITOR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetPhysicalMonitorsFromHMONITOR = Win32::API::More->new('dxva2',
    'BOOL GetPhysicalMonitorsFromHMONITOR(HANDLE hMonitor, DWORD dwPhysicalMonitorArraySize, LPVOID pPhysicalMonitorArray)');
# my $ret = $GetPhysicalMonitorsFromHMONITOR->Call($hMonitor, $dwPhysicalMonitorArraySize, $pPhysicalMonitorArray);
# hMonitor : HMONITOR -> HANDLE
# dwPhysicalMonitorArraySize : DWORD -> DWORD
# pPhysicalMonitorArray : PHYSICAL_MONITOR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型