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

WTSQuerySessionInformationA

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

シグネチャ

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

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

パラメーター

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

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL WTSQuerySessionInformationA(
    HANDLE hServer,   // optional
    DWORD SessionId,
    WTS_INFO_CLASS WTSInfoClass,
    LPSTR* ppBuffer,
    DWORD* pBytesReturned
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool WTSQuerySessionInformationA(
    IntPtr hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    int WTSInfoClass,   // WTS_INFO_CLASS
    IntPtr ppBuffer,   // LPSTR* out
    out uint pBytesReturned   // DWORD* out
);
<DllImport("WTSAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WTSQuerySessionInformationA(
    hServer As IntPtr,   ' HANDLE optional
    SessionId As UInteger,   ' DWORD
    WTSInfoClass As Integer,   ' WTS_INFO_CLASS
    ppBuffer As IntPtr,   ' LPSTR* 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 : LPSTR* out
' pBytesReturned : DWORD* out
Declare PtrSafe Function WTSQuerySessionInformationA Lib "wtsapi32" ( _
    ByVal hServer As LongPtr, _
    ByVal SessionId As Long, _
    ByVal WTSInfoClass As Long, _
    ByVal ppBuffer As LongPtr, _
    ByRef pBytesReturned As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WTSQuerySessionInformationA = ctypes.windll.wtsapi32.WTSQuerySessionInformationA
WTSQuerySessionInformationA.restype = wintypes.BOOL
WTSQuerySessionInformationA.argtypes = [
    wintypes.HANDLE,  # hServer : HANDLE optional
    wintypes.DWORD,  # SessionId : DWORD
    ctypes.c_int,  # WTSInfoClass : WTS_INFO_CLASS
    ctypes.c_void_p,  # ppBuffer : LPSTR* 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')
WTSQuerySessionInformationA = Fiddle::Function.new(
  lib['WTSQuerySessionInformationA'],
  [
    Fiddle::TYPE_VOIDP,  # hServer : HANDLE optional
    -Fiddle::TYPE_INT,  # SessionId : DWORD
    Fiddle::TYPE_INT,  # WTSInfoClass : WTS_INFO_CLASS
    Fiddle::TYPE_VOIDP,  # ppBuffer : LPSTR* out
    Fiddle::TYPE_VOIDP,  # pBytesReturned : DWORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "wtsapi32")]
extern "system" {
    fn WTSQuerySessionInformationA(
        hServer: *mut core::ffi::c_void,  // HANDLE optional
        SessionId: u32,  // DWORD
        WTSInfoClass: i32,  // WTS_INFO_CLASS
        ppBuffer: *mut *mut u8,  // LPSTR* 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.Ansi, SetLastError = true)]
public static extern bool WTSQuerySessionInformationA(IntPtr hServer, uint SessionId, int WTSInfoClass, IntPtr ppBuffer, out uint pBytesReturned);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WTSAPI32_WTSQuerySessionInformationA' -Namespace Win32 -PassThru
# $api::WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
#uselib "WTSAPI32.dll"
#func global WTSQuerySessionInformationA "WTSQuerySessionInformationA" sptr, sptr, sptr, sptr, sptr
; WTSQuerySessionInformationA hServer, SessionId, WTSInfoClass, varptr(ppBuffer), varptr(pBytesReturned)   ; 戻り値は stat
; hServer : HANDLE optional -> "sptr"
; SessionId : DWORD -> "sptr"
; WTSInfoClass : WTS_INFO_CLASS -> "sptr"
; ppBuffer : LPSTR* out -> "sptr"
; pBytesReturned : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WTSAPI32.dll"
#cfunc global WTSQuerySessionInformationA "WTSQuerySessionInformationA" sptr, int, int, var, var
; res = WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
; hServer : HANDLE optional -> "sptr"
; SessionId : DWORD -> "int"
; WTSInfoClass : WTS_INFO_CLASS -> "int"
; ppBuffer : LPSTR* out -> "var"
; pBytesReturned : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL WTSQuerySessionInformationA(HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer, DWORD* pBytesReturned)
#uselib "WTSAPI32.dll"
#cfunc global WTSQuerySessionInformationA "WTSQuerySessionInformationA" intptr, int, int, var, var
; res = WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
; hServer : HANDLE optional -> "intptr"
; SessionId : DWORD -> "int"
; WTSInfoClass : WTS_INFO_CLASS -> "int"
; ppBuffer : LPSTR* out -> "var"
; pBytesReturned : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wtsapi32 = windows.NewLazySystemDLL("WTSAPI32.dll")
	procWTSQuerySessionInformationA = wtsapi32.NewProc("WTSQuerySessionInformationA")
)

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

extern "wtsapi32" fn WTSQuerySessionInformationA(
    hServer: ?*anyopaque, // HANDLE optional
    SessionId: u32, // DWORD
    WTSInfoClass: i32, // WTS_INFO_CLASS
    ppBuffer: [*c][*c]u8, // LPSTR* out
    pBytesReturned: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
proc WTSQuerySessionInformationA(
    hServer: pointer,  # HANDLE optional
    SessionId: uint32,  # DWORD
    WTSInfoClass: int32,  # WTS_INFO_CLASS
    ppBuffer: ptr cstring,  # LPSTR* out
    pBytesReturned: ptr uint32  # DWORD* out
): int32 {.importc: "WTSQuerySessionInformationA", stdcall, dynlib: "WTSAPI32.dll".}
pragma(lib, "wtsapi32");
extern(Windows)
int WTSQuerySessionInformationA(
    void* hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    int WTSInfoClass,   // WTS_INFO_CLASS
    char** ppBuffer,   // LPSTR* out
    uint* pBytesReturned   // DWORD* out
);
ccall((:WTSQuerySessionInformationA, "WTSAPI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, Int32, Ptr{Ptr{UInt8}}, Ptr{UInt32}),
      hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
# hServer : HANDLE optional -> Ptr{Cvoid}
# SessionId : DWORD -> UInt32
# WTSInfoClass : WTS_INFO_CLASS -> Int32
# ppBuffer : LPSTR* out -> Ptr{Ptr{UInt8}}
# pBytesReturned : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WTSQuerySessionInformationA(
    void* hServer,
    uint32_t SessionId,
    int32_t WTSInfoClass,
    char** ppBuffer,
    uint32_t* pBytesReturned);
]]
local wtsapi32 = ffi.load("wtsapi32")
-- wtsapi32.WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
-- hServer : HANDLE optional
-- SessionId : DWORD
-- WTSInfoClass : WTS_INFO_CLASS
-- ppBuffer : LPSTR* out
-- pBytesReturned : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WTSAPI32.dll');
const WTSQuerySessionInformationA = lib.func('__stdcall', 'WTSQuerySessionInformationA', 'int32_t', ['void *', 'uint32_t', 'int32_t', 'void *', 'uint32_t *']);
// WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
// hServer : HANDLE optional -> 'void *'
// SessionId : DWORD -> 'uint32_t'
// WTSInfoClass : WTS_INFO_CLASS -> 'int32_t'
// ppBuffer : LPSTR* out -> 'void *'
// pBytesReturned : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WTSAPI32.dll", {
  WTSQuerySessionInformationA: { parameters: ["pointer", "u32", "i32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned)
// hServer : HANDLE optional -> "pointer"
// SessionId : DWORD -> "u32"
// WTSInfoClass : WTS_INFO_CLASS -> "i32"
// ppBuffer : LPSTR* out -> "pointer"
// pBytesReturned : DWORD* out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WTSQuerySessionInformationA(
    void* hServer,
    uint32_t SessionId,
    int32_t WTSInfoClass,
    char** ppBuffer,
    uint32_t* pBytesReturned);
C, "WTSAPI32.dll");
// $ffi->WTSQuerySessionInformationA(hServer, SessionId, WTSInfoClass, ppBuffer, pBytesReturned);
// hServer : HANDLE optional
// SessionId : DWORD
// WTSInfoClass : WTS_INFO_CLASS
// ppBuffer : LPSTR* 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.ASCII_OPTIONS);
    boolean WTSQuerySessionInformationA(
        Pointer hServer,   // HANDLE optional
        int SessionId,   // DWORD
        int WTSInfoClass,   // WTS_INFO_CLASS
        PointerByReference ppBuffer,   // LPSTR* out
        IntByReference pBytesReturned   // DWORD* out
    );
}
@[Link("wtsapi32")]
lib LibWTSAPI32
  fun WTSQuerySessionInformationA = WTSQuerySessionInformationA(
    hServer : Void*,   # HANDLE optional
    SessionId : UInt32,   # DWORD
    WTSInfoClass : Int32,   # WTS_INFO_CLASS
    ppBuffer : UInt8**,   # LPSTR* 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 WTSQuerySessionInformationANative = Int32 Function(Pointer<Void>, Uint32, Int32, Pointer<Pointer<Utf8>>, Pointer<Uint32>);
typedef WTSQuerySessionInformationADart = int Function(Pointer<Void>, int, int, Pointer<Pointer<Utf8>>, Pointer<Uint32>);
final WTSQuerySessionInformationA = DynamicLibrary.open('WTSAPI32.dll')
    .lookupFunction<WTSQuerySessionInformationANative, WTSQuerySessionInformationADart>('WTSQuerySessionInformationA');
// hServer : HANDLE optional -> Pointer<Void>
// SessionId : DWORD -> Uint32
// WTSInfoClass : WTS_INFO_CLASS -> Int32
// ppBuffer : LPSTR* out -> Pointer<Pointer<Utf8>>
// pBytesReturned : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WTSQuerySessionInformationA(
  hServer: THandle;   // HANDLE optional
  SessionId: DWORD;   // DWORD
  WTSInfoClass: Integer;   // WTS_INFO_CLASS
  ppBuffer: PPAnsiChar;   // LPSTR* out
  pBytesReturned: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSQuerySessionInformationA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let wtsquerysessioninformationa =
  foreign "WTSQuerySessionInformationA"
    ((ptr void) @-> uint32_t @-> int32_t @-> (ptr string) @-> (ptr uint32_t) @-> returning int32_t)
(* hServer : HANDLE optional -> (ptr void) *)
(* SessionId : DWORD -> uint32_t *)
(* WTSInfoClass : WTS_INFO_CLASS -> int32_t *)
(* ppBuffer : LPSTR* out -> (ptr string) *)
(* 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 ("WTSQuerySessionInformationA" wtsquery-session-information-a :convention :stdcall) :int32
  (h-server :pointer)   ; HANDLE optional
  (session-id :uint32)   ; DWORD
  (wtsinfo-class :int32)   ; WTS_INFO_CLASS
  (pp-buffer :pointer)   ; LPSTR* out
  (p-bytes-returned :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WTSQuerySessionInformationA = Win32::API::More->new('WTSAPI32',
    'BOOL WTSQuerySessionInformationA(HANDLE hServer, DWORD SessionId, int WTSInfoClass, LPVOID ppBuffer, LPVOID pBytesReturned)');
# my $ret = $WTSQuerySessionInformationA->Call($hServer, $SessionId, $WTSInfoClass, $ppBuffer, $pBytesReturned);
# hServer : HANDLE optional -> HANDLE
# SessionId : DWORD -> DWORD
# WTSInfoClass : WTS_INFO_CLASS -> int
# ppBuffer : LPSTR* out -> LPVOID
# pBytesReturned : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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