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

GetClassNameA

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

シグネチャ

// USER32.dll  (ANSI / -A)
#include <windows.h>

INT GetClassNameA(
    HWND hWnd,
    LPSTR lpClassName,
    INT nMaxCount
);

パラメーター

名前方向
hWndHWNDin
lpClassNameLPSTRout
nMaxCountINTin

戻り値の型: INT

各言語での呼び出し定義

// USER32.dll  (ANSI / -A)
#include <windows.h>

INT GetClassNameA(
    HWND hWnd,
    LPSTR lpClassName,
    INT nMaxCount
);
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern int GetClassNameA(
    IntPtr hWnd,   // HWND
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpClassName,   // LPSTR out
    int nMaxCount   // INT
);
<DllImport("USER32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetClassNameA(
    hWnd As IntPtr,   ' HWND
    <MarshalAs(UnmanagedType.LPStr)> lpClassName As System.Text.StringBuilder,   ' LPSTR out
    nMaxCount As Integer   ' INT
) As Integer
End Function
' hWnd : HWND
' lpClassName : LPSTR out
' nMaxCount : INT
Declare PtrSafe Function GetClassNameA Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal lpClassName As String, _
    ByVal nMaxCount As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetClassNameA = ctypes.windll.user32.GetClassNameA
GetClassNameA.restype = ctypes.c_int
GetClassNameA.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.LPSTR,  # lpClassName : LPSTR out
    ctypes.c_int,  # nMaxCount : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
GetClassNameA = Fiddle::Function.new(
  lib['GetClassNameA'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    Fiddle::TYPE_VOIDP,  # lpClassName : LPSTR out
    Fiddle::TYPE_INT,  # nMaxCount : INT
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn GetClassNameA(
        hWnd: *mut core::ffi::c_void,  // HWND
        lpClassName: *mut u8,  // LPSTR out
        nMaxCount: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int GetClassNameA(IntPtr hWnd, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpClassName, int nMaxCount);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_GetClassNameA' -Namespace Win32 -PassThru
# $api::GetClassNameA(hWnd, lpClassName, nMaxCount)
#uselib "USER32.dll"
#func global GetClassNameA "GetClassNameA" sptr, sptr, sptr
; GetClassNameA hWnd, varptr(lpClassName), nMaxCount   ; 戻り値は stat
; hWnd : HWND -> "sptr"
; lpClassName : LPSTR out -> "sptr"
; nMaxCount : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global GetClassNameA "GetClassNameA" sptr, var, int
; res = GetClassNameA(hWnd, lpClassName, nMaxCount)
; hWnd : HWND -> "sptr"
; lpClassName : LPSTR out -> "var"
; nMaxCount : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT GetClassNameA(HWND hWnd, LPSTR lpClassName, INT nMaxCount)
#uselib "USER32.dll"
#cfunc global GetClassNameA "GetClassNameA" intptr, var, int
; res = GetClassNameA(hWnd, lpClassName, nMaxCount)
; hWnd : HWND -> "intptr"
; lpClassName : LPSTR out -> "var"
; nMaxCount : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetClassNameA = user32.NewProc("GetClassNameA")
)

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