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

ImpersonateSelf

関数
現在のスレッドを自身のトークンで偽装する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL ImpersonateSelf(
    SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
);

パラメーター

名前方向
ImpersonationLevelSECURITY_IMPERSONATION_LEVELin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL ImpersonateSelf(
    SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool ImpersonateSelf(
    int ImpersonationLevel   // SECURITY_IMPERSONATION_LEVEL
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ImpersonateSelf(
    ImpersonationLevel As Integer   ' SECURITY_IMPERSONATION_LEVEL
) As Boolean
End Function
' ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL
Declare PtrSafe Function ImpersonateSelf Lib "advapi32" ( _
    ByVal ImpersonationLevel As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ImpersonateSelf = ctypes.windll.advapi32.ImpersonateSelf
ImpersonateSelf.restype = wintypes.BOOL
ImpersonateSelf.argtypes = [
    ctypes.c_int,  # ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
ImpersonateSelf = Fiddle::Function.new(
  lib['ImpersonateSelf'],
  [
    Fiddle::TYPE_INT,  # ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn ImpersonateSelf(
        ImpersonationLevel: i32  // SECURITY_IMPERSONATION_LEVEL
    ) -> 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 ImpersonateSelf(int ImpersonationLevel);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_ImpersonateSelf' -Namespace Win32 -PassThru
# $api::ImpersonateSelf(ImpersonationLevel)
#uselib "ADVAPI32.dll"
#func global ImpersonateSelf "ImpersonateSelf" sptr
; ImpersonateSelf ImpersonationLevel   ; 戻り値は stat
; ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global ImpersonateSelf "ImpersonateSelf" int
; res = ImpersonateSelf(ImpersonationLevel)
; ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "int"
; BOOL ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
#uselib "ADVAPI32.dll"
#cfunc global ImpersonateSelf "ImpersonateSelf" int
; res = ImpersonateSelf(ImpersonationLevel)
; ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procImpersonateSelf = advapi32.NewProc("ImpersonateSelf")
)

// ImpersonationLevel (SECURITY_IMPERSONATION_LEVEL)
r1, _, err := procImpersonateSelf.Call(
	uintptr(ImpersonationLevel),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function ImpersonateSelf(
  ImpersonationLevel: Integer   // SECURITY_IMPERSONATION_LEVEL
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'ImpersonateSelf';
result := DllCall("ADVAPI32\ImpersonateSelf"
    , "Int", ImpersonationLevel   ; SECURITY_IMPERSONATION_LEVEL
    , "Int")   ; return: BOOL
●ImpersonateSelf(ImpersonationLevel) = DLL("ADVAPI32.dll", "bool ImpersonateSelf(int)")
# 呼び出し: ImpersonateSelf(ImpersonationLevel)
# ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。