Win32 API 日本語リファレンス
ホームSecurity › GetTokenInformation

GetTokenInformation

関数
アクセストークンの指定種別の情報を取得する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL GetTokenInformation(
    HANDLE TokenHandle,
    TOKEN_INFORMATION_CLASS TokenInformationClass,
    void* TokenInformation,   // optional
    DWORD TokenInformationLength,
    DWORD* ReturnLength
);

パラメーター

名前方向
TokenHandleHANDLEin
TokenInformationClassTOKEN_INFORMATION_CLASSin
TokenInformationvoid*outoptional
TokenInformationLengthDWORDin
ReturnLengthDWORD*out

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL GetTokenInformation(
    HANDLE TokenHandle,
    TOKEN_INFORMATION_CLASS TokenInformationClass,
    void* TokenInformation,   // optional
    DWORD TokenInformationLength,
    DWORD* ReturnLength
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetTokenInformation(
    IntPtr TokenHandle,   // HANDLE
    int TokenInformationClass,   // TOKEN_INFORMATION_CLASS
    IntPtr TokenInformation,   // void* optional, out
    uint TokenInformationLength,   // DWORD
    out uint ReturnLength   // DWORD* out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetTokenInformation(
    TokenHandle As IntPtr,   ' HANDLE
    TokenInformationClass As Integer,   ' TOKEN_INFORMATION_CLASS
    TokenInformation As IntPtr,   ' void* optional, out
    TokenInformationLength As UInteger,   ' DWORD
    <Out> ByRef ReturnLength As UInteger   ' DWORD* out
) As Boolean
End Function
' TokenHandle : HANDLE
' TokenInformationClass : TOKEN_INFORMATION_CLASS
' TokenInformation : void* optional, out
' TokenInformationLength : DWORD
' ReturnLength : DWORD* out
Declare PtrSafe Function GetTokenInformation Lib "advapi32" ( _
    ByVal TokenHandle As LongPtr, _
    ByVal TokenInformationClass As Long, _
    ByVal TokenInformation As LongPtr, _
    ByVal TokenInformationLength As Long, _
    ByRef ReturnLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetTokenInformation = ctypes.windll.advapi32.GetTokenInformation
GetTokenInformation.restype = wintypes.BOOL
GetTokenInformation.argtypes = [
    wintypes.HANDLE,  # TokenHandle : HANDLE
    ctypes.c_int,  # TokenInformationClass : TOKEN_INFORMATION_CLASS
    ctypes.POINTER(None),  # TokenInformation : void* optional, out
    wintypes.DWORD,  # TokenInformationLength : DWORD
    ctypes.POINTER(wintypes.DWORD),  # ReturnLength : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
GetTokenInformation = Fiddle::Function.new(
  lib['GetTokenInformation'],
  [
    Fiddle::TYPE_VOIDP,  # TokenHandle : HANDLE
    Fiddle::TYPE_INT,  # TokenInformationClass : TOKEN_INFORMATION_CLASS
    Fiddle::TYPE_VOIDP,  # TokenInformation : void* optional, out
    -Fiddle::TYPE_INT,  # TokenInformationLength : DWORD
    Fiddle::TYPE_VOIDP,  # ReturnLength : DWORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn GetTokenInformation(
        TokenHandle: *mut core::ffi::c_void,  // HANDLE
        TokenInformationClass: i32,  // TOKEN_INFORMATION_CLASS
        TokenInformation: *mut (),  // void* optional, out
        TokenInformationLength: u32,  // DWORD
        ReturnLength: *mut u32  // DWORD* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool GetTokenInformation(IntPtr TokenHandle, int TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_GetTokenInformation' -Namespace Win32 -PassThru
# $api::GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength, ReturnLength)
#uselib "ADVAPI32.dll"
#func global GetTokenInformation "GetTokenInformation" sptr, sptr, sptr, sptr, sptr
; GetTokenInformation TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength, varptr(ReturnLength)   ; 戻り値は stat
; TokenHandle : HANDLE -> "sptr"
; TokenInformationClass : TOKEN_INFORMATION_CLASS -> "sptr"
; TokenInformation : void* optional, out -> "sptr"
; TokenInformationLength : DWORD -> "sptr"
; ReturnLength : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global GetTokenInformation "GetTokenInformation" sptr, int, sptr, int, var
; res = GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength, ReturnLength)
; TokenHandle : HANDLE -> "sptr"
; TokenInformationClass : TOKEN_INFORMATION_CLASS -> "int"
; TokenInformation : void* optional, out -> "sptr"
; TokenInformationLength : DWORD -> "int"
; ReturnLength : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetTokenInformation(HANDLE TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, void* TokenInformation, DWORD TokenInformationLength, DWORD* ReturnLength)
#uselib "ADVAPI32.dll"
#cfunc global GetTokenInformation "GetTokenInformation" intptr, int, intptr, int, var
; res = GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength, ReturnLength)
; TokenHandle : HANDLE -> "intptr"
; TokenInformationClass : TOKEN_INFORMATION_CLASS -> "int"
; TokenInformation : void* optional, out -> "intptr"
; TokenInformationLength : DWORD -> "int"
; ReturnLength : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procGetTokenInformation = advapi32.NewProc("GetTokenInformation")
)

// TokenHandle (HANDLE), TokenInformationClass (TOKEN_INFORMATION_CLASS), TokenInformation (void* optional, out), TokenInformationLength (DWORD), ReturnLength (DWORD* out)
r1, _, err := procGetTokenInformation.Call(
	uintptr(TokenHandle),
	uintptr(TokenInformationClass),
	uintptr(TokenInformation),
	uintptr(TokenInformationLength),
	uintptr(ReturnLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetTokenInformation(
  TokenHandle: THandle;   // HANDLE
  TokenInformationClass: Integer;   // TOKEN_INFORMATION_CLASS
  TokenInformation: Pointer;   // void* optional, out
  TokenInformationLength: DWORD;   // DWORD
  ReturnLength: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'GetTokenInformation';
result := DllCall("ADVAPI32\GetTokenInformation"
    , "Ptr", TokenHandle   ; HANDLE
    , "Int", TokenInformationClass   ; TOKEN_INFORMATION_CLASS
    , "Ptr", TokenInformation   ; void* optional, out
    , "UInt", TokenInformationLength   ; DWORD
    , "Ptr", ReturnLength   ; DWORD* out
    , "Int")   ; return: BOOL
●GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength, ReturnLength) = DLL("ADVAPI32.dll", "bool GetTokenInformation(void*, int, void*, dword, void*)")
# 呼び出し: GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength, ReturnLength)
# TokenHandle : HANDLE -> "void*"
# TokenInformationClass : TOKEN_INFORMATION_CLASS -> "int"
# TokenInformation : void* optional, out -> "void*"
# TokenInformationLength : DWORD -> "dword"
# ReturnLength : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。