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

WTSLogoffSession

関数
指定セッションをログオフする。
DLLWTSAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

BOOL WTSLogoffSession(
    HANDLE hServer,   // optional
    DWORD SessionId,
    BOOL bWait
);

パラメーター

名前方向説明
hServerHANDLEoptional対象RDサーバーのハンドル。ローカルにはWTS_CURRENT_SERVER_HANDLEを指定。
SessionIdDWORDinログオフする対象セッションのID。WTS_CURRENT_SESSIONで現在のセッション。
bWaitBOOLinTRUEでログオフ完了まで待機、FALSEで即座に戻る。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL WTSLogoffSession(
    HANDLE hServer,   // optional
    DWORD SessionId,
    BOOL bWait
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WTSLogoffSession(
    IntPtr hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    bool bWait   // BOOL
);
<DllImport("WTSAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WTSLogoffSession(
    hServer As IntPtr,   ' HANDLE optional
    SessionId As UInteger,   ' DWORD
    bWait As Boolean   ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hServer : HANDLE optional
' SessionId : DWORD
' bWait : BOOL
Declare PtrSafe Function WTSLogoffSession Lib "wtsapi32" ( _
    ByVal hServer As LongPtr, _
    ByVal SessionId As Long, _
    ByVal bWait As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WTSLogoffSession = ctypes.windll.wtsapi32.WTSLogoffSession
WTSLogoffSession.restype = wintypes.BOOL
WTSLogoffSession.argtypes = [
    wintypes.HANDLE,  # hServer : HANDLE optional
    wintypes.DWORD,  # SessionId : DWORD
    wintypes.BOOL,  # bWait : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WTSAPI32.dll')
WTSLogoffSession = Fiddle::Function.new(
  lib['WTSLogoffSession'],
  [
    Fiddle::TYPE_VOIDP,  # hServer : HANDLE optional
    -Fiddle::TYPE_INT,  # SessionId : DWORD
    Fiddle::TYPE_INT,  # bWait : BOOL
  ],
  Fiddle::TYPE_INT)
#[link(name = "wtsapi32")]
extern "system" {
    fn WTSLogoffSession(
        hServer: *mut core::ffi::c_void,  // HANDLE optional
        SessionId: u32,  // DWORD
        bWait: i32  // BOOL
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", SetLastError = true)]
public static extern bool WTSLogoffSession(IntPtr hServer, uint SessionId, bool bWait);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WTSAPI32_WTSLogoffSession' -Namespace Win32 -PassThru
# $api::WTSLogoffSession(hServer, SessionId, bWait)
#uselib "WTSAPI32.dll"
#func global WTSLogoffSession "WTSLogoffSession" sptr, sptr, sptr
; WTSLogoffSession hServer, SessionId, bWait   ; 戻り値は stat
; hServer : HANDLE optional -> "sptr"
; SessionId : DWORD -> "sptr"
; bWait : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WTSAPI32.dll"
#cfunc global WTSLogoffSession "WTSLogoffSession" sptr, int, int
; res = WTSLogoffSession(hServer, SessionId, bWait)
; hServer : HANDLE optional -> "sptr"
; SessionId : DWORD -> "int"
; bWait : BOOL -> "int"
; BOOL WTSLogoffSession(HANDLE hServer, DWORD SessionId, BOOL bWait)
#uselib "WTSAPI32.dll"
#cfunc global WTSLogoffSession "WTSLogoffSession" intptr, int, int
; res = WTSLogoffSession(hServer, SessionId, bWait)
; hServer : HANDLE optional -> "intptr"
; SessionId : DWORD -> "int"
; bWait : BOOL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wtsapi32 = windows.NewLazySystemDLL("WTSAPI32.dll")
	procWTSLogoffSession = wtsapi32.NewProc("WTSLogoffSession")
)

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

extern "wtsapi32" fn WTSLogoffSession(
    hServer: ?*anyopaque, // HANDLE optional
    SessionId: u32, // DWORD
    bWait: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;
proc WTSLogoffSession(
    hServer: pointer,  # HANDLE optional
    SessionId: uint32,  # DWORD
    bWait: int32  # BOOL
): int32 {.importc: "WTSLogoffSession", stdcall, dynlib: "WTSAPI32.dll".}
pragma(lib, "wtsapi32");
extern(Windows)
int WTSLogoffSession(
    void* hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    int bWait   // BOOL
);
ccall((:WTSLogoffSession, "WTSAPI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, Int32),
      hServer, SessionId, bWait)
# hServer : HANDLE optional -> Ptr{Cvoid}
# SessionId : DWORD -> UInt32
# bWait : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WTSLogoffSession(
    void* hServer,
    uint32_t SessionId,
    int32_t bWait);
]]
local wtsapi32 = ffi.load("wtsapi32")
-- wtsapi32.WTSLogoffSession(hServer, SessionId, bWait)
-- hServer : HANDLE optional
-- SessionId : DWORD
-- bWait : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WTSAPI32.dll');
const WTSLogoffSession = lib.func('__stdcall', 'WTSLogoffSession', 'int32_t', ['void *', 'uint32_t', 'int32_t']);
// WTSLogoffSession(hServer, SessionId, bWait)
// hServer : HANDLE optional -> 'void *'
// SessionId : DWORD -> 'uint32_t'
// bWait : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WTSAPI32.dll", {
  WTSLogoffSession: { parameters: ["pointer", "u32", "i32"], result: "i32" },
});
// lib.symbols.WTSLogoffSession(hServer, SessionId, bWait)
// hServer : HANDLE optional -> "pointer"
// SessionId : DWORD -> "u32"
// bWait : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WTSLogoffSession(
    void* hServer,
    uint32_t SessionId,
    int32_t bWait);
C, "WTSAPI32.dll");
// $ffi->WTSLogoffSession(hServer, SessionId, bWait);
// hServer : HANDLE optional
// SessionId : DWORD
// bWait : BOOL
// 構造体/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);
    boolean WTSLogoffSession(
        Pointer hServer,   // HANDLE optional
        int SessionId,   // DWORD
        boolean bWait   // BOOL
    );
}
@[Link("wtsapi32")]
lib LibWTSAPI32
  fun WTSLogoffSession = WTSLogoffSession(
    hServer : Void*,   # HANDLE optional
    SessionId : UInt32,   # DWORD
    bWait : Int32   # BOOL
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WTSLogoffSessionNative = Int32 Function(Pointer<Void>, Uint32, Int32);
typedef WTSLogoffSessionDart = int Function(Pointer<Void>, int, int);
final WTSLogoffSession = DynamicLibrary.open('WTSAPI32.dll')
    .lookupFunction<WTSLogoffSessionNative, WTSLogoffSessionDart>('WTSLogoffSession');
// hServer : HANDLE optional -> Pointer<Void>
// SessionId : DWORD -> Uint32
// bWait : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WTSLogoffSession(
  hServer: THandle;   // HANDLE optional
  SessionId: DWORD;   // DWORD
  bWait: BOOL   // BOOL
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSLogoffSession';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WTSLogoffSession"
  c_WTSLogoffSession :: Ptr () -> Word32 -> CInt -> IO CInt
-- hServer : HANDLE optional -> Ptr ()
-- SessionId : DWORD -> Word32
-- bWait : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

(cffi:defcfun ("WTSLogoffSession" wtslogoff-session :convention :stdcall) :int32
  (h-server :pointer)   ; HANDLE optional
  (session-id :uint32)   ; DWORD
  (b-wait :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WTSLogoffSession = Win32::API::More->new('WTSAPI32',
    'BOOL WTSLogoffSession(HANDLE hServer, DWORD SessionId, BOOL bWait)');
# my $ret = $WTSLogoffSession->Call($hServer, $SessionId, $bWait);
# hServer : HANDLE optional -> HANDLE
# SessionId : DWORD -> DWORD
# bWait : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。