Win32 API 日本語リファレンス
ホームSystem.RemoteDesktop › WTSQuerySessionInformationW

WTSQuerySessionInformationW

関数
指定セッションの情報を情報クラス別に取得する。
DLLWTSAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

BOOL WTSQuerySessionInformationW(
    HANDLE hServer,   // optional
    DWORD SessionId,
    WTS_INFO_CLASS WTSInfoClass,
    LPWSTR* ppBuffer,
    DWORD* pBytesReturned
);

パラメーター

名前方向説明
hServerHANDLEoptional対象RDサーバーのハンドル。ローカルにはWTS_CURRENT_SERVER_HANDLEを指定。
SessionIdDWORDin情報を取得する対象セッションのID。WTS_CURRENT_SESSIONで現在のセッション。
WTSInfoClassWTS_INFO_CLASSin取得する情報の種類を示す列挙値(ユーザー名・クライアント情報等)。
ppBufferLPWSTR*out取得情報を格納したバッファへのポインタを受け取る(出力、WTSFreeMemoryで解放)。
pBytesReturnedDWORD*out返されたデータのバイト数を受け取るポインタ(出力)。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL WTSQuerySessionInformationW(
    HANDLE hServer,   // optional
    DWORD SessionId,
    WTS_INFO_CLASS WTSInfoClass,
    LPWSTR* ppBuffer,
    DWORD* pBytesReturned
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool WTSQuerySessionInformationW(
    IntPtr hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    int WTSInfoClass,   // WTS_INFO_CLASS
    IntPtr ppBuffer,   // LPWSTR* out
    out uint pBytesReturned   // DWORD* out
);
<DllImport("WTSAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WTSQuerySessionInformationW(
    hServer As IntPtr,   ' HANDLE optional
    SessionId As UInteger,   ' DWORD
    WTSInfoClass As Integer,   ' WTS_INFO_CLASS
    ppBuffer As IntPtr,   ' LPWSTR* out
    <Out> ByRef pBytesReturned As UInteger   ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hServer : HANDLE optional
' SessionId : DWORD
' WTSInfoClass : WTS_INFO_CLASS
' ppBuffer : LPWSTR* out
' pBytesReturned : DWORD* out
Declare PtrSafe Function WTSQuerySessionInformationW Lib "wtsapi32" ( _
    ByVal hServer As LongPtr, _
    ByVal SessionId As Long, _
    ByVal WTSInfoClass As Long, _
    ByVal ppBuffer As LongPtr, _
    ByRef pBytesReturned 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

WTSQuerySessionInformationW = ctypes.windll.wtsapi32.WTSQuerySessionInformationW
WTSQuerySessionInformationW.restype = wintypes.BOOL
WTSQuerySessionInformationW.argtypes = [
    wintypes.HANDLE,  # hServer : HANDLE optional
    wintypes.DWORD,  # SessionId : DWORD
    ctypes.c_int,  # WTSInfoClass : WTS_INFO_CLASS
    ctypes.c_void_p,  # ppBuffer : LPWSTR* out
    ctypes.POINTER(wintypes.DWORD),  # pBytesReturned : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	wtsapi32 = windows.NewLazySystemDLL("WTSAPI32.dll")
	procWTSQuerySessionInformationW = wtsapi32.NewProc("WTSQuerySessionInformationW")
)

// hServer (HANDLE optional), SessionId (DWORD), WTSInfoClass (WTS_INFO_CLASS), ppBuffer (LPWSTR* out), pBytesReturned (DWORD* out)
r1, _, err := procWTSQuerySessionInformationW.Call(
	uintptr(hServer),
	uintptr(SessionId),
	uintptr(WTSInfoClass),
	uintptr(ppBuffer),
	uintptr(pBytesReturned),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WTSQuerySessionInformationW(
  hServer: THandle;   // HANDLE optional
  SessionId: DWORD;   // DWORD
  WTSInfoClass: Integer;   // WTS_INFO_CLASS
  ppBuffer: PPWideChar;   // LPWSTR* out
  pBytesReturned: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSQuerySessionInformationW';
result := DllCall("WTSAPI32\WTSQuerySessionInformationW"
    , "Ptr", hServer   ; HANDLE optional
    , "UInt", SessionId   ; DWORD
    , "Int", WTSInfoClass   ; WTS_INFO_CLASS
    , "Ptr", ppBuffer   ; LPWSTR* out
    , "Ptr", pBytesReturned   ; DWORD* out
    , "Int")   ; return: BOOL
●WTSQuerySessionInformationW(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned) = DLL("WTSAPI32.dll", "bool WTSQuerySessionInformationW(void*, dword, int, void*, void*)")
# 呼び出し: WTSQuerySessionInformationW(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
# hServer : HANDLE optional -> "void*"
# SessionId : DWORD -> "dword"
# WTSInfoClass : WTS_INFO_CLASS -> "int"
# ppBuffer : LPWSTR* out -> "void*"
# pBytesReturned : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "wtsapi32" fn WTSQuerySessionInformationW(
    hServer: ?*anyopaque, // HANDLE optional
    SessionId: u32, // DWORD
    WTSInfoClass: i32, // WTS_INFO_CLASS
    ppBuffer: [*c][*c]u16, // LPWSTR* out
    pBytesReturned: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WTSQuerySessionInformationW(
    hServer: pointer,  # HANDLE optional
    SessionId: uint32,  # DWORD
    WTSInfoClass: int32,  # WTS_INFO_CLASS
    ppBuffer: ptr WideCString,  # LPWSTR* out
    pBytesReturned: ptr uint32  # DWORD* out
): int32 {.importc: "WTSQuerySessionInformationW", stdcall, dynlib: "WTSAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "wtsapi32");
extern(Windows)
int WTSQuerySessionInformationW(
    void* hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    int WTSInfoClass,   // WTS_INFO_CLASS
    wchar** ppBuffer,   // LPWSTR* out
    uint* pBytesReturned   // DWORD* out
);
ccall((:WTSQuerySessionInformationW, "WTSAPI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, Int32, Ptr{Ptr{UInt16}}, Ptr{UInt32}),
      hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
# hServer : HANDLE optional -> Ptr{Cvoid}
# SessionId : DWORD -> UInt32
# WTSInfoClass : WTS_INFO_CLASS -> Int32
# ppBuffer : LPWSTR* out -> Ptr{Ptr{UInt16}}
# pBytesReturned : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t WTSQuerySessionInformationW(
    void* hServer,
    uint32_t SessionId,
    int32_t WTSInfoClass,
    uint16_t** ppBuffer,
    uint32_t* pBytesReturned);
]]
local wtsapi32 = ffi.load("wtsapi32")
-- wtsapi32.WTSQuerySessionInformationW(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
-- hServer : HANDLE optional
-- SessionId : DWORD
-- WTSInfoClass : WTS_INFO_CLASS
-- ppBuffer : LPWSTR* out
-- pBytesReturned : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('WTSAPI32.dll');
const WTSQuerySessionInformationW = lib.func('__stdcall', 'WTSQuerySessionInformationW', 'int32_t', ['void *', 'uint32_t', 'int32_t', 'void *', 'uint32_t *']);
// WTSQuerySessionInformationW(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
// hServer : HANDLE optional -> 'void *'
// SessionId : DWORD -> 'uint32_t'
// WTSInfoClass : WTS_INFO_CLASS -> 'int32_t'
// ppBuffer : LPWSTR* out -> 'void *'
// pBytesReturned : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WTSAPI32.dll", {
  WTSQuerySessionInformationW: { parameters: ["pointer", "u32", "i32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WTSQuerySessionInformationW(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
// hServer : HANDLE optional -> "pointer"
// SessionId : DWORD -> "u32"
// WTSInfoClass : WTS_INFO_CLASS -> "i32"
// ppBuffer : LPWSTR* out -> "pointer"
// pBytesReturned : DWORD* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WTSQuerySessionInformationW(
    void* hServer,
    uint32_t SessionId,
    int32_t WTSInfoClass,
    uint16_t** ppBuffer,
    uint32_t* pBytesReturned);
C, "WTSAPI32.dll");
// $ffi->WTSQuerySessionInformationW(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned);
// hServer : HANDLE optional
// SessionId : DWORD
// WTSInfoClass : WTS_INFO_CLASS
// ppBuffer : LPWSTR* out
// pBytesReturned : DWORD* 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 Wtsapi32 extends StdCallLibrary {
    Wtsapi32 INSTANCE = Native.load("wtsapi32", Wtsapi32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean WTSQuerySessionInformationW(
        Pointer hServer,   // HANDLE optional
        int SessionId,   // DWORD
        int WTSInfoClass,   // WTS_INFO_CLASS
        PointerByReference ppBuffer,   // LPWSTR* out
        IntByReference pBytesReturned   // DWORD* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("wtsapi32")]
lib LibWTSAPI32
  fun WTSQuerySessionInformationW = WTSQuerySessionInformationW(
    hServer : Void*,   # HANDLE optional
    SessionId : UInt32,   # DWORD
    WTSInfoClass : Int32,   # WTS_INFO_CLASS
    ppBuffer : UInt16**,   # LPWSTR* out
    pBytesReturned : UInt32*   # DWORD* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WTSQuerySessionInformationWNative = Int32 Function(Pointer<Void>, Uint32, Int32, Pointer<Pointer<Utf16>>, Pointer<Uint32>);
typedef WTSQuerySessionInformationWDart = int Function(Pointer<Void>, int, int, Pointer<Pointer<Utf16>>, Pointer<Uint32>);
final WTSQuerySessionInformationW = DynamicLibrary.open('WTSAPI32.dll')
    .lookupFunction<WTSQuerySessionInformationWNative, WTSQuerySessionInformationWDart>('WTSQuerySessionInformationW');
// hServer : HANDLE optional -> Pointer<Void>
// SessionId : DWORD -> Uint32
// WTSInfoClass : WTS_INFO_CLASS -> Int32
// ppBuffer : LPWSTR* out -> Pointer<Pointer<Utf16>>
// pBytesReturned : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WTSQuerySessionInformationW(
  hServer: THandle;   // HANDLE optional
  SessionId: DWORD;   // DWORD
  WTSInfoClass: Integer;   // WTS_INFO_CLASS
  ppBuffer: PPWideChar;   // LPWSTR* out
  pBytesReturned: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSQuerySessionInformationW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WTSQuerySessionInformationW"
  c_WTSQuerySessionInformationW :: Ptr () -> Word32 -> Int32 -> Ptr CWString -> Ptr Word32 -> IO CInt
-- hServer : HANDLE optional -> Ptr ()
-- SessionId : DWORD -> Word32
-- WTSInfoClass : WTS_INFO_CLASS -> Int32
-- ppBuffer : LPWSTR* out -> Ptr CWString
-- pBytesReturned : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wtsquerysessioninformationw =
  foreign "WTSQuerySessionInformationW"
    ((ptr void) @-> uint32_t @-> int32_t @-> (ptr (ptr uint16_t)) @-> (ptr uint32_t) @-> returning int32_t)
(* hServer : HANDLE optional -> (ptr void) *)
(* SessionId : DWORD -> uint32_t *)
(* WTSInfoClass : WTS_INFO_CLASS -> int32_t *)
(* ppBuffer : LPWSTR* out -> (ptr (ptr uint16_t)) *)
(* pBytesReturned : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library wtsapi32 (t "WTSAPI32.dll"))
(cffi:use-foreign-library wtsapi32)

(cffi:defcfun ("WTSQuerySessionInformationW" wtsquery-session-information-w :convention :stdcall) :int32
  (h-server :pointer)   ; HANDLE optional
  (session-id :uint32)   ; DWORD
  (wtsinfo-class :int32)   ; WTS_INFO_CLASS
  (pp-buffer :pointer)   ; LPWSTR* out
  (p-bytes-returned :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WTSQuerySessionInformationW = Win32::API::More->new('WTSAPI32',
    'BOOL WTSQuerySessionInformationW(HANDLE hServer, DWORD SessionId, int WTSInfoClass, LPVOID ppBuffer, LPVOID pBytesReturned)');
# my $ret = $WTSQuerySessionInformationW->Call($hServer, $SessionId, $WTSInfoClass, $ppBuffer, $pBytesReturned);
# hServer : HANDLE optional -> HANDLE
# SessionId : DWORD -> DWORD
# WTSInfoClass : WTS_INFO_CLASS -> int
# ppBuffer : LPWSTR* out -> LPVOID
# pBytesReturned : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型