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

RealGetWindowClassW

関数
ウィンドウの実際のクラス名を取得する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

DWORD RealGetWindowClassW(
    HWND hwnd,
    LPWSTR ptszClassName,
    DWORD cchClassNameMax
);

パラメーター

名前方向説明
hwndHWNDin種類を取得する対象のウィンドウへのハンドル。
ptszClassNameLPWSTRoutウィンドウの種類を受け取る文字列へのポインター。
cchClassNameMaxDWORDinpszType パラメーターが指すバッファーの長さ(文字数)。

戻り値の型: DWORD

公式ドキュメント

ウィンドウの種類を指定する文字列を取得します。(Unicode)

戻り値

型: UINT

関数が成功した場合、戻り値は指定したバッファーにコピーされた文字数です。

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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>

DWORD RealGetWindowClassW(
    HWND hwnd,
    LPWSTR ptszClassName,
    DWORD cchClassNameMax
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint RealGetWindowClassW(
    IntPtr hwnd,   // HWND
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder ptszClassName,   // LPWSTR out
    uint cchClassNameMax   // DWORD
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function RealGetWindowClassW(
    hwnd As IntPtr,   ' HWND
    <MarshalAs(UnmanagedType.LPWStr)> ptszClassName As System.Text.StringBuilder,   ' LPWSTR out
    cchClassNameMax As UInteger   ' DWORD
) As UInteger
End Function
' hwnd : HWND
' ptszClassName : LPWSTR out
' cchClassNameMax : DWORD
Declare PtrSafe Function RealGetWindowClassW Lib "user32" ( _
    ByVal hwnd As LongPtr, _
    ByVal ptszClassName As LongPtr, _
    ByVal cchClassNameMax 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

RealGetWindowClassW = ctypes.windll.user32.RealGetWindowClassW
RealGetWindowClassW.restype = wintypes.DWORD
RealGetWindowClassW.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND
    wintypes.LPWSTR,  # ptszClassName : LPWSTR out
    wintypes.DWORD,  # cchClassNameMax : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procRealGetWindowClassW = user32.NewProc("RealGetWindowClassW")
)

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

extern "user32" fn RealGetWindowClassW(
    hwnd: ?*anyopaque, // HWND
    ptszClassName: [*c]u16, // LPWSTR out
    cchClassNameMax: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc RealGetWindowClassW(
    hwnd: pointer,  # HWND
    ptszClassName: ptr uint16,  # LPWSTR out
    cchClassNameMax: uint32  # DWORD
): uint32 {.importc: "RealGetWindowClassW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "user32");
extern(Windows)
uint RealGetWindowClassW(
    void* hwnd,   // HWND
    wchar* ptszClassName,   // LPWSTR out
    uint cchClassNameMax   // DWORD
);
ccall((:RealGetWindowClassW, "USER32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Ptr{UInt16}, UInt32),
      hwnd, ptszClassName, cchClassNameMax)
# hwnd : HWND -> Ptr{Cvoid}
# ptszClassName : LPWSTR out -> Ptr{UInt16}
# cchClassNameMax : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t RealGetWindowClassW(
    void* hwnd,
    uint16_t* ptszClassName,
    uint32_t cchClassNameMax);
]]
local user32 = ffi.load("user32")
-- user32.RealGetWindowClassW(hwnd, ptszClassName, cchClassNameMax)
-- hwnd : HWND
-- ptszClassName : LPWSTR out
-- cchClassNameMax : DWORD
-- 構造体/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 RealGetWindowClassW = lib.func('__stdcall', 'RealGetWindowClassW', 'uint32_t', ['void *', 'uint16_t *', 'uint32_t']);
// RealGetWindowClassW(hwnd, ptszClassName, cchClassNameMax)
// hwnd : HWND -> 'void *'
// ptszClassName : LPWSTR out -> 'uint16_t *'
// cchClassNameMax : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  RealGetWindowClassW: { parameters: ["pointer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.RealGetWindowClassW(hwnd, ptszClassName, cchClassNameMax)
// hwnd : HWND -> "pointer"
// ptszClassName : LPWSTR out -> "buffer"
// cchClassNameMax : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t RealGetWindowClassW(
    void* hwnd,
    uint16_t* ptszClassName,
    uint32_t cchClassNameMax);
C, "USER32.dll");
// $ffi->RealGetWindowClassW(hwnd, ptszClassName, cchClassNameMax);
// hwnd : HWND
// ptszClassName : LPWSTR out
// cchClassNameMax : DWORD
// 構造体/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);
    int RealGetWindowClassW(
        Pointer hwnd,   // HWND
        char[] ptszClassName,   // LPWSTR out
        int cchClassNameMax   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("user32")]
lib LibUSER32
  fun RealGetWindowClassW = RealGetWindowClassW(
    hwnd : Void*,   # HWND
    ptszClassName : UInt16*,   # LPWSTR out
    cchClassNameMax : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef RealGetWindowClassWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Uint32);
typedef RealGetWindowClassWDart = int Function(Pointer<Void>, Pointer<Utf16>, int);
final RealGetWindowClassW = DynamicLibrary.open('USER32.dll')
    .lookupFunction<RealGetWindowClassWNative, RealGetWindowClassWDart>('RealGetWindowClassW');
// hwnd : HWND -> Pointer<Void>
// ptszClassName : LPWSTR out -> Pointer<Utf16>
// cchClassNameMax : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RealGetWindowClassW(
  hwnd: THandle;   // HWND
  ptszClassName: PWideChar;   // LPWSTR out
  cchClassNameMax: DWORD   // DWORD
): DWORD; stdcall;
  external 'USER32.dll' name 'RealGetWindowClassW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RealGetWindowClassW"
  c_RealGetWindowClassW :: Ptr () -> CWString -> Word32 -> IO Word32
-- hwnd : HWND -> Ptr ()
-- ptszClassName : LPWSTR out -> CWString
-- cchClassNameMax : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let realgetwindowclassw =
  foreign "RealGetWindowClassW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* hwnd : HWND -> (ptr void) *)
(* ptszClassName : LPWSTR out -> (ptr uint16_t) *)
(* cchClassNameMax : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("RealGetWindowClassW" real-get-window-class-w :convention :stdcall) :uint32
  (hwnd :pointer)   ; HWND
  (ptsz-class-name :pointer)   ; LPWSTR out
  (cch-class-name-max :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RealGetWindowClassW = Win32::API::More->new('USER32',
    'DWORD RealGetWindowClassW(HANDLE hwnd, LPWSTR ptszClassName, DWORD cchClassNameMax)');
# my $ret = $RealGetWindowClassW->Call($hwnd, $ptszClassName, $cchClassNameMax);
# hwnd : HWND -> HANDLE
# ptszClassName : LPWSTR out -> LPWSTR
# cchClassNameMax : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い