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

WTSEnumerateSessionsExW

関数
フィルター指定で指定サーバーのセッションを列挙する。
DLLWTSAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 7 以降

シグネチャ

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

BOOL WTSEnumerateSessionsExW(
    HANDLE hServer,   // optional
    DWORD* pLevel,
    DWORD Filter,
    WTS_SESSION_INFO_1W** ppSessionInfo,
    DWORD* pCount
);

パラメーター

名前方向説明
hServerHANDLEinoptional対象RDサーバーのハンドル。ローカルにはWTS_CURRENT_SERVER_HANDLEを指定。
pLevelDWORD*inout情報レベルを指すポインタ。現在は1のみ有効(入出力)。
FilterDWORDin返すセッションを絞り込むフィルタフラグ。
ppSessionInfoWTS_SESSION_INFO_1W**outWTS_SESSION_INFO_1W配列へのポインタを受け取る(出力、WTSFreeMemoryExで解放)。
pCountDWORD*out返されたセッション数を受け取るポインタ(出力)。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL WTSEnumerateSessionsExW(
    HANDLE hServer,   // optional
    DWORD* pLevel,
    DWORD Filter,
    WTS_SESSION_INFO_1W** ppSessionInfo,
    DWORD* pCount
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool WTSEnumerateSessionsExW(
    IntPtr hServer,   // HANDLE optional
    ref uint pLevel,   // DWORD* in/out
    uint Filter,   // DWORD
    IntPtr ppSessionInfo,   // WTS_SESSION_INFO_1W** out
    out uint pCount   // DWORD* out
);
<DllImport("WTSAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WTSEnumerateSessionsExW(
    hServer As IntPtr,   ' HANDLE optional
    ByRef pLevel As UInteger,   ' DWORD* in/out
    Filter As UInteger,   ' DWORD
    ppSessionInfo As IntPtr,   ' WTS_SESSION_INFO_1W** out
    <Out> ByRef pCount As UInteger   ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hServer : HANDLE optional
' pLevel : DWORD* in/out
' Filter : DWORD
' ppSessionInfo : WTS_SESSION_INFO_1W** out
' pCount : DWORD* out
Declare PtrSafe Function WTSEnumerateSessionsExW Lib "wtsapi32" ( _
    ByVal hServer As LongPtr, _
    ByRef pLevel As Long, _
    ByVal Filter As Long, _
    ByVal ppSessionInfo As LongPtr, _
    ByRef pCount 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

WTSEnumerateSessionsExW = ctypes.windll.wtsapi32.WTSEnumerateSessionsExW
WTSEnumerateSessionsExW.restype = wintypes.BOOL
WTSEnumerateSessionsExW.argtypes = [
    wintypes.HANDLE,  # hServer : HANDLE optional
    ctypes.POINTER(wintypes.DWORD),  # pLevel : DWORD* in/out
    wintypes.DWORD,  # Filter : DWORD
    ctypes.c_void_p,  # ppSessionInfo : WTS_SESSION_INFO_1W** out
    ctypes.POINTER(wintypes.DWORD),  # pCount : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WTSAPI32.dll')
WTSEnumerateSessionsExW = Fiddle::Function.new(
  lib['WTSEnumerateSessionsExW'],
  [
    Fiddle::TYPE_VOIDP,  # hServer : HANDLE optional
    Fiddle::TYPE_VOIDP,  # pLevel : DWORD* in/out
    -Fiddle::TYPE_INT,  # Filter : DWORD
    Fiddle::TYPE_VOIDP,  # ppSessionInfo : WTS_SESSION_INFO_1W** out
    Fiddle::TYPE_VOIDP,  # pCount : DWORD* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "wtsapi32")]
extern "system" {
    fn WTSEnumerateSessionsExW(
        hServer: *mut core::ffi::c_void,  // HANDLE optional
        pLevel: *mut u32,  // DWORD* in/out
        Filter: u32,  // DWORD
        ppSessionInfo: *mut *mut WTS_SESSION_INFO_1W,  // WTS_SESSION_INFO_1W** out
        pCount: *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 WTSEnumerateSessionsExW(IntPtr hServer, ref uint pLevel, uint Filter, IntPtr ppSessionInfo, out uint pCount);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WTSAPI32_WTSEnumerateSessionsExW' -Namespace Win32 -PassThru
# $api::WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount)
#uselib "WTSAPI32.dll"
#func global WTSEnumerateSessionsExW "WTSEnumerateSessionsExW" wptr, wptr, wptr, wptr, wptr
; WTSEnumerateSessionsExW hServer, varptr(pLevel), Filter, varptr(ppSessionInfo), varptr(pCount)   ; 戻り値は stat
; hServer : HANDLE optional -> "wptr"
; pLevel : DWORD* in/out -> "wptr"
; Filter : DWORD -> "wptr"
; ppSessionInfo : WTS_SESSION_INFO_1W** out -> "wptr"
; pCount : DWORD* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WTSAPI32.dll"
#cfunc global WTSEnumerateSessionsExW "WTSEnumerateSessionsExW" sptr, var, int, var, var
; res = WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount)
; hServer : HANDLE optional -> "sptr"
; pLevel : DWORD* in/out -> "var"
; Filter : DWORD -> "int"
; ppSessionInfo : WTS_SESSION_INFO_1W** out -> "var"
; pCount : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL WTSEnumerateSessionsExW(HANDLE hServer, DWORD* pLevel, DWORD Filter, WTS_SESSION_INFO_1W** ppSessionInfo, DWORD* pCount)
#uselib "WTSAPI32.dll"
#cfunc global WTSEnumerateSessionsExW "WTSEnumerateSessionsExW" intptr, var, int, var, var
; res = WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount)
; hServer : HANDLE optional -> "intptr"
; pLevel : DWORD* in/out -> "var"
; Filter : DWORD -> "int"
; ppSessionInfo : WTS_SESSION_INFO_1W** out -> "var"
; pCount : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wtsapi32 = windows.NewLazySystemDLL("WTSAPI32.dll")
	procWTSEnumerateSessionsExW = wtsapi32.NewProc("WTSEnumerateSessionsExW")
)

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

extern "wtsapi32" fn WTSEnumerateSessionsExW(
    hServer: ?*anyopaque, // HANDLE optional
    pLevel: [*c]u32, // DWORD* in/out
    Filter: u32, // DWORD
    ppSessionInfo: [*c][*c]WTS_SESSION_INFO_1W, // WTS_SESSION_INFO_1W** out
    pCount: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WTSEnumerateSessionsExW(
    hServer: pointer,  # HANDLE optional
    pLevel: ptr uint32,  # DWORD* in/out
    Filter: uint32,  # DWORD
    ppSessionInfo: ptr WTS_SESSION_INFO_1W,  # WTS_SESSION_INFO_1W** out
    pCount: ptr uint32  # DWORD* out
): int32 {.importc: "WTSEnumerateSessionsExW", stdcall, dynlib: "WTSAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "wtsapi32");
extern(Windows)
int WTSEnumerateSessionsExW(
    void* hServer,   // HANDLE optional
    uint* pLevel,   // DWORD* in/out
    uint Filter,   // DWORD
    WTS_SESSION_INFO_1W** ppSessionInfo,   // WTS_SESSION_INFO_1W** out
    uint* pCount   // DWORD* out
);
ccall((:WTSEnumerateSessionsExW, "WTSAPI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{UInt32}, UInt32, Ptr{WTS_SESSION_INFO_1W}, Ptr{UInt32}),
      hServer, pLevel, Filter, ppSessionInfo, pCount)
# hServer : HANDLE optional -> Ptr{Cvoid}
# pLevel : DWORD* in/out -> Ptr{UInt32}
# Filter : DWORD -> UInt32
# ppSessionInfo : WTS_SESSION_INFO_1W** out -> Ptr{WTS_SESSION_INFO_1W}
# pCount : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t WTSEnumerateSessionsExW(
    void* hServer,
    uint32_t* pLevel,
    uint32_t Filter,
    void* ppSessionInfo,
    uint32_t* pCount);
]]
local wtsapi32 = ffi.load("wtsapi32")
-- wtsapi32.WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount)
-- hServer : HANDLE optional
-- pLevel : DWORD* in/out
-- Filter : DWORD
-- ppSessionInfo : WTS_SESSION_INFO_1W** out
-- pCount : 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 WTSEnumerateSessionsExW = lib.func('__stdcall', 'WTSEnumerateSessionsExW', 'int32_t', ['void *', 'uint32_t *', 'uint32_t', 'void *', 'uint32_t *']);
// WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount)
// hServer : HANDLE optional -> 'void *'
// pLevel : DWORD* in/out -> 'uint32_t *'
// Filter : DWORD -> 'uint32_t'
// ppSessionInfo : WTS_SESSION_INFO_1W** out -> 'void *'
// pCount : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WTSAPI32.dll", {
  WTSEnumerateSessionsExW: { parameters: ["pointer", "pointer", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount)
// hServer : HANDLE optional -> "pointer"
// pLevel : DWORD* in/out -> "pointer"
// Filter : DWORD -> "u32"
// ppSessionInfo : WTS_SESSION_INFO_1W** out -> "pointer"
// pCount : DWORD* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WTSEnumerateSessionsExW(
    void* hServer,
    uint32_t* pLevel,
    uint32_t Filter,
    void* ppSessionInfo,
    uint32_t* pCount);
C, "WTSAPI32.dll");
// $ffi->WTSEnumerateSessionsExW(hServer, pLevel, Filter, ppSessionInfo, pCount);
// hServer : HANDLE optional
// pLevel : DWORD* in/out
// Filter : DWORD
// ppSessionInfo : WTS_SESSION_INFO_1W** out
// pCount : 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 WTSEnumerateSessionsExW(
        Pointer hServer,   // HANDLE optional
        IntByReference pLevel,   // DWORD* in/out
        int Filter,   // DWORD
        Pointer ppSessionInfo,   // WTS_SESSION_INFO_1W** out
        IntByReference pCount   // DWORD* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("wtsapi32")]
lib LibWTSAPI32
  fun WTSEnumerateSessionsExW = WTSEnumerateSessionsExW(
    hServer : Void*,   # HANDLE optional
    pLevel : UInt32*,   # DWORD* in/out
    Filter : UInt32,   # DWORD
    ppSessionInfo : WTS_SESSION_INFO_1W**,   # WTS_SESSION_INFO_1W** out
    pCount : 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 WTSEnumerateSessionsExWNative = Int32 Function(Pointer<Void>, Pointer<Uint32>, Uint32, Pointer<Void>, Pointer<Uint32>);
typedef WTSEnumerateSessionsExWDart = int Function(Pointer<Void>, Pointer<Uint32>, int, Pointer<Void>, Pointer<Uint32>);
final WTSEnumerateSessionsExW = DynamicLibrary.open('WTSAPI32.dll')
    .lookupFunction<WTSEnumerateSessionsExWNative, WTSEnumerateSessionsExWDart>('WTSEnumerateSessionsExW');
// hServer : HANDLE optional -> Pointer<Void>
// pLevel : DWORD* in/out -> Pointer<Uint32>
// Filter : DWORD -> Uint32
// ppSessionInfo : WTS_SESSION_INFO_1W** out -> Pointer<Void>
// pCount : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WTSEnumerateSessionsExW(
  hServer: THandle;   // HANDLE optional
  pLevel: Pointer;   // DWORD* in/out
  Filter: DWORD;   // DWORD
  ppSessionInfo: Pointer;   // WTS_SESSION_INFO_1W** out
  pCount: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSEnumerateSessionsExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WTSEnumerateSessionsExW"
  c_WTSEnumerateSessionsExW :: Ptr () -> Ptr Word32 -> Word32 -> Ptr () -> Ptr Word32 -> IO CInt
-- hServer : HANDLE optional -> Ptr ()
-- pLevel : DWORD* in/out -> Ptr Word32
-- Filter : DWORD -> Word32
-- ppSessionInfo : WTS_SESSION_INFO_1W** out -> Ptr ()
-- pCount : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wtsenumeratesessionsexw =
  foreign "WTSEnumerateSessionsExW"
    ((ptr void) @-> (ptr uint32_t) @-> uint32_t @-> (ptr void) @-> (ptr uint32_t) @-> returning int32_t)
(* hServer : HANDLE optional -> (ptr void) *)
(* pLevel : DWORD* in/out -> (ptr uint32_t) *)
(* Filter : DWORD -> uint32_t *)
(* ppSessionInfo : WTS_SESSION_INFO_1W** out -> (ptr void) *)
(* pCount : 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 ("WTSEnumerateSessionsExW" wtsenumerate-sessions-ex-w :convention :stdcall) :int32
  (h-server :pointer)   ; HANDLE optional
  (p-level :pointer)   ; DWORD* in/out
  (filter :uint32)   ; DWORD
  (pp-session-info :pointer)   ; WTS_SESSION_INFO_1W** out
  (p-count :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WTSEnumerateSessionsExW = Win32::API::More->new('WTSAPI32',
    'BOOL WTSEnumerateSessionsExW(HANDLE hServer, LPVOID pLevel, DWORD Filter, LPVOID ppSessionInfo, LPVOID pCount)');
# my $ret = $WTSEnumerateSessionsExW->Call($hServer, $pLevel, $Filter, $ppSessionInfo, $pCount);
# hServer : HANDLE optional -> HANDLE
# pLevel : DWORD* in/out -> LPVOID
# Filter : DWORD -> DWORD
# ppSessionInfo : WTS_SESSION_INFO_1W** out -> LPVOID
# pCount : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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