Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › GetWindowWord

GetWindowWord

関数
ウィンドウの追加メモリからWORD値を取得する旧式関数。
DLLUSER32.dll呼出規約winapi

シグネチャ

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

WORD GetWindowWord(
    HWND hWnd,
    INT nIndex
);

パラメーター

名前方向説明
hWndHWNDinウィンドウへのハンドル。間接的に、そのウィンドウが属するクラスを示します。
nIndexINTin

取得する値への 0 から始まるオフセットです。有効な値は、0 から追加のウィンドウメモリのバイト数から 4 を引いた値までの範囲です。たとえば、12 バイト以上の追加メモリを指定した場合、値 8 は 3 番目の 32 ビット整数へのインデックスになります。その他の値を取得するには、次のいずれかの値を指定します。

定数 意味
GWW_HINSTANCE -6 アプリケーションインスタンスへのハンドルを取得します。
GWW_HWNDPARENT -8 親ウィンドウが存在する場合、そのハンドルを取得します。
GWW_ID -12 ウィンドウの識別子を取得します。

戻り値の型: WORD

公式ドキュメント

指定したオフセットにある、追加のウィンドウメモリ内の 16 ビット (DWORD) 値を取得します。

戻り値

関数が成功した場合、戻り値は要求された値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

追加のウィンドウメモリを確保するには、RegisterClassEx 関数で使用する WNDCLASSEX 構造体の cbWndExtra メンバーに 0 以外の値を指定します。

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

各言語での呼び出し定義

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

WORD GetWindowWord(
    HWND hWnd,
    INT nIndex
);
[DllImport("USER32.dll", ExactSpelling = true)]
static extern ushort GetWindowWord(
    IntPtr hWnd,   // HWND
    int nIndex   // INT
);
<DllImport("USER32.dll", ExactSpelling:=True)>
Public Shared Function GetWindowWord(
    hWnd As IntPtr,   ' HWND
    nIndex As Integer   ' INT
) As UShort
End Function
' hWnd : HWND
' nIndex : INT
Declare PtrSafe Function GetWindowWord Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIndex As Long) As Integer
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetWindowWord = ctypes.windll.user32.GetWindowWord
GetWindowWord.restype = ctypes.c_ushort
GetWindowWord.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    ctypes.c_int,  # nIndex : INT
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
GetWindowWord = Fiddle::Function.new(
  lib['GetWindowWord'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    Fiddle::TYPE_INT,  # nIndex : INT
  ],
  -Fiddle::TYPE_SHORT)
#[link(name = "user32")]
extern "system" {
    fn GetWindowWord(
        hWnd: *mut core::ffi::c_void,  // HWND
        nIndex: i32  // INT
    ) -> u16;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll")]
public static extern ushort GetWindowWord(IntPtr hWnd, int nIndex);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_GetWindowWord' -Namespace Win32 -PassThru
# $api::GetWindowWord(hWnd, nIndex)
#uselib "USER32.dll"
#func global GetWindowWord "GetWindowWord" sptr, sptr
; GetWindowWord hWnd, nIndex   ; 戻り値は stat
; hWnd : HWND -> "sptr"
; nIndex : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global GetWindowWord "GetWindowWord" sptr, int
; res = GetWindowWord(hWnd, nIndex)
; hWnd : HWND -> "sptr"
; nIndex : INT -> "int"
; WORD GetWindowWord(HWND hWnd, INT nIndex)
#uselib "USER32.dll"
#cfunc global GetWindowWord "GetWindowWord" intptr, int
; res = GetWindowWord(hWnd, nIndex)
; hWnd : HWND -> "intptr"
; nIndex : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetWindowWord = user32.NewProc("GetWindowWord")
)

// hWnd (HWND), nIndex (INT)
r1, _, err := procGetWindowWord.Call(
	uintptr(hWnd),
	uintptr(nIndex),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WORD
function GetWindowWord(
  hWnd: THandle;   // HWND
  nIndex: Integer   // INT
): Word; stdcall;
  external 'USER32.dll' name 'GetWindowWord';
result := DllCall("USER32\GetWindowWord"
    , "Ptr", hWnd   ; HWND
    , "Int", nIndex   ; INT
    , "UShort")   ; return: WORD
●GetWindowWord(hWnd, nIndex) = DLL("USER32.dll", "int GetWindowWord(void*, int)")
# 呼び出し: GetWindowWord(hWnd, nIndex)
# hWnd : HWND -> "void*"
# nIndex : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef GetWindowWordNative = Uint16 Function(Pointer<Void>, Int32);
typedef GetWindowWordDart = int Function(Pointer<Void>, int);
final GetWindowWord = DynamicLibrary.open('USER32.dll')
    .lookupFunction<GetWindowWordNative, GetWindowWordDart>('GetWindowWord');
// hWnd : HWND -> Pointer<Void>
// nIndex : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetWindowWord(
  hWnd: THandle;   // HWND
  nIndex: Integer   // INT
): Word; stdcall;
  external 'USER32.dll' name 'GetWindowWord';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetWindowWord"
  c_GetWindowWord :: Ptr () -> Int32 -> IO Word16
-- hWnd : HWND -> Ptr ()
-- nIndex : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getwindowword =
  foreign "GetWindowWord"
    ((ptr void) @-> int32_t @-> returning uint16_t)
(* hWnd : HWND -> (ptr void) *)
(* nIndex : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("GetWindowWord" get-window-word :convention :stdcall) :uint16
  (h-wnd :pointer)   ; HWND
  (n-index :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetWindowWord = Win32::API::More->new('USER32',
    'WORD GetWindowWord(HANDLE hWnd, int nIndex)');
# my $ret = $GetWindowWord->Call($hWnd, $nIndex);
# hWnd : HWND -> HANDLE
# nIndex : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。