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

InternalGetWindowText

関数
ウィンドウのタイトルテキストを直接取得する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

INT InternalGetWindowText(
    HWND hWnd,
    LPWSTR pString,
    INT cchMaxCount
);

パラメーター

名前方向
hWndHWNDin
pStringLPWSTRout
cchMaxCountINTin

戻り値の型: INT

各言語での呼び出し定義

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

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

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

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procInternalGetWindowText = user32.NewProc("InternalGetWindowText")
)

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