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

GetUserObjectSecurity

関数
ユーザーオブジェクトのセキュリティ記述子を取得する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL GetUserObjectSecurity(
    HANDLE hObj,
    DWORD* pSIRequested,
    PSECURITY_DESCRIPTOR pSID,   // optional
    DWORD nLength,
    DWORD* lpnLengthNeeded
);

パラメーター

名前方向説明
hObjHANDLEinセキュリティ情報を返す対象のユーザーオブジェクトへのハンドル。
pSIRequestedDWORD*in要求するセキュリティ情報を指定する SECURITY_INFORMATION 値へのポインター。
pSIDPSECURITY_DESCRIPTORoutoptional関数が返るときに、要求された情報を格納する self-relative 形式の SECURITY_DESCRIPTOR 構造体へのポインター。このバッファーは 4 バイト境界にアラインされている必要があります。
nLengthDWORDinpSD パラメーターが指すバッファーの長さ(バイト単位)。
lpnLengthNeededDWORD*out完全な security descriptor を格納するのに必要なバイト数を受け取る変数へのポインター。関数が返るときにこの変数の値が nLength パラメーターの値より大きい場合、関数は FALSE を返し、security descriptor はバッファーにまったくコピーされません。そうでない場合は、security descriptor 全体がコピーされます。

戻り値の型: BOOL

公式ドキュメント

指定したユーザーオブジェクトのセキュリティ情報を取得します。

戻り値

関数が成功した場合は、0 以外の値を返します。

関数が失敗した場合は、0 を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

ユーザーオブジェクトの security descriptor から所有者、グループ、または discretionary access control list(DACL)を読み取るには、呼び出し元の process がハンドルを開いたときに READ_CONTROL アクセスが付与されている必要があります。

security descriptor から system access control list(SACL)を読み取るには、呼び出し元プロセスがハンドルを開いたときに ACCESS_SYSTEM_SECURITY アクセスが付与されている必要があります。このアクセスを取得する正しい方法は、呼び出し元の現在のトークンで SE_SECURITY_NAME 特権を有効にし、ACCESS_SYSTEM_SECURITY アクセスでハンドルを開き、その後に特権を無効にすることです。

Examples

この関数を使用する例については、Starting an Interactive Client Process を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL GetUserObjectSecurity(
    HANDLE hObj,
    DWORD* pSIRequested,
    PSECURITY_DESCRIPTOR pSID,   // optional
    DWORD nLength,
    DWORD* lpnLengthNeeded
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetUserObjectSecurity(
    IntPtr hObj,   // HANDLE
    ref uint pSIRequested,   // DWORD*
    IntPtr pSID,   // PSECURITY_DESCRIPTOR optional, out
    uint nLength,   // DWORD
    out uint lpnLengthNeeded   // DWORD* out
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetUserObjectSecurity(
    hObj As IntPtr,   ' HANDLE
    ByRef pSIRequested As UInteger,   ' DWORD*
    pSID As IntPtr,   ' PSECURITY_DESCRIPTOR optional, out
    nLength As UInteger,   ' DWORD
    <Out> ByRef lpnLengthNeeded As UInteger   ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hObj : HANDLE
' pSIRequested : DWORD*
' pSID : PSECURITY_DESCRIPTOR optional, out
' nLength : DWORD
' lpnLengthNeeded : DWORD* out
Declare PtrSafe Function GetUserObjectSecurity Lib "user32" ( _
    ByVal hObj As LongPtr, _
    ByRef pSIRequested As Long, _
    ByVal pSID As LongPtr, _
    ByVal nLength As Long, _
    ByRef lpnLengthNeeded As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetUserObjectSecurity = ctypes.windll.user32.GetUserObjectSecurity
GetUserObjectSecurity.restype = wintypes.BOOL
GetUserObjectSecurity.argtypes = [
    wintypes.HANDLE,  # hObj : HANDLE
    ctypes.POINTER(wintypes.DWORD),  # pSIRequested : DWORD*
    wintypes.HANDLE,  # pSID : PSECURITY_DESCRIPTOR optional, out
    wintypes.DWORD,  # nLength : DWORD
    ctypes.POINTER(wintypes.DWORD),  # lpnLengthNeeded : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetUserObjectSecurity = user32.NewProc("GetUserObjectSecurity")
)

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

extern "user32" fn GetUserObjectSecurity(
    hObj: ?*anyopaque, // HANDLE
    pSIRequested: [*c]u32, // DWORD*
    pSID: ?*anyopaque, // PSECURITY_DESCRIPTOR optional, out
    nLength: u32, // DWORD
    lpnLengthNeeded: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
proc GetUserObjectSecurity(
    hObj: pointer,  # HANDLE
    pSIRequested: ptr uint32,  # DWORD*
    pSID: pointer,  # PSECURITY_DESCRIPTOR optional, out
    nLength: uint32,  # DWORD
    lpnLengthNeeded: ptr uint32  # DWORD* out
): int32 {.importc: "GetUserObjectSecurity", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
int GetUserObjectSecurity(
    void* hObj,   // HANDLE
    uint* pSIRequested,   // DWORD*
    void* pSID,   // PSECURITY_DESCRIPTOR optional, out
    uint nLength,   // DWORD
    uint* lpnLengthNeeded   // DWORD* out
);
ccall((:GetUserObjectSecurity, "USER32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{UInt32}, Ptr{Cvoid}, UInt32, Ptr{UInt32}),
      hObj, pSIRequested, pSID, nLength, lpnLengthNeeded)
# hObj : HANDLE -> Ptr{Cvoid}
# pSIRequested : DWORD* -> Ptr{UInt32}
# pSID : PSECURITY_DESCRIPTOR optional, out -> Ptr{Cvoid}
# nLength : DWORD -> UInt32
# lpnLengthNeeded : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetUserObjectSecurity(
    void* hObj,
    uint32_t* pSIRequested,
    void* pSID,
    uint32_t nLength,
    uint32_t* lpnLengthNeeded);
]]
local user32 = ffi.load("user32")
-- user32.GetUserObjectSecurity(hObj, pSIRequested, pSID, nLength, lpnLengthNeeded)
-- hObj : HANDLE
-- pSIRequested : DWORD*
-- pSID : PSECURITY_DESCRIPTOR optional, out
-- nLength : DWORD
-- lpnLengthNeeded : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const GetUserObjectSecurity = lib.func('__stdcall', 'GetUserObjectSecurity', 'int32_t', ['void *', 'uint32_t *', 'void *', 'uint32_t', 'uint32_t *']);
// GetUserObjectSecurity(hObj, pSIRequested, pSID, nLength, lpnLengthNeeded)
// hObj : HANDLE -> 'void *'
// pSIRequested : DWORD* -> 'uint32_t *'
// pSID : PSECURITY_DESCRIPTOR optional, out -> 'void *'
// nLength : DWORD -> 'uint32_t'
// lpnLengthNeeded : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  GetUserObjectSecurity: { parameters: ["pointer", "pointer", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.GetUserObjectSecurity(hObj, pSIRequested, pSID, nLength, lpnLengthNeeded)
// hObj : HANDLE -> "pointer"
// pSIRequested : DWORD* -> "pointer"
// pSID : PSECURITY_DESCRIPTOR optional, out -> "pointer"
// nLength : DWORD -> "u32"
// lpnLengthNeeded : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetUserObjectSecurity(
    void* hObj,
    uint32_t* pSIRequested,
    void* pSID,
    uint32_t nLength,
    uint32_t* lpnLengthNeeded);
C, "USER32.dll");
// $ffi->GetUserObjectSecurity(hObj, pSIRequested, pSID, nLength, lpnLengthNeeded);
// hObj : HANDLE
// pSIRequested : DWORD*
// pSID : PSECURITY_DESCRIPTOR optional, out
// nLength : DWORD
// lpnLengthNeeded : 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 User32 extends StdCallLibrary {
    User32 INSTANCE = Native.load("user32", User32.class);
    boolean GetUserObjectSecurity(
        Pointer hObj,   // HANDLE
        IntByReference pSIRequested,   // DWORD*
        Pointer pSID,   // PSECURITY_DESCRIPTOR optional, out
        int nLength,   // DWORD
        IntByReference lpnLengthNeeded   // DWORD* out
    );
}
@[Link("user32")]
lib LibUSER32
  fun GetUserObjectSecurity = GetUserObjectSecurity(
    hObj : Void*,   # HANDLE
    pSIRequested : UInt32*,   # DWORD*
    pSID : Void*,   # PSECURITY_DESCRIPTOR optional, out
    nLength : UInt32,   # DWORD
    lpnLengthNeeded : 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 GetUserObjectSecurityNative = Int32 Function(Pointer<Void>, Pointer<Uint32>, Pointer<Void>, Uint32, Pointer<Uint32>);
typedef GetUserObjectSecurityDart = int Function(Pointer<Void>, Pointer<Uint32>, Pointer<Void>, int, Pointer<Uint32>);
final GetUserObjectSecurity = DynamicLibrary.open('USER32.dll')
    .lookupFunction<GetUserObjectSecurityNative, GetUserObjectSecurityDart>('GetUserObjectSecurity');
// hObj : HANDLE -> Pointer<Void>
// pSIRequested : DWORD* -> Pointer<Uint32>
// pSID : PSECURITY_DESCRIPTOR optional, out -> Pointer<Void>
// nLength : DWORD -> Uint32
// lpnLengthNeeded : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetUserObjectSecurity(
  hObj: THandle;   // HANDLE
  pSIRequested: Pointer;   // DWORD*
  pSID: THandle;   // PSECURITY_DESCRIPTOR optional, out
  nLength: DWORD;   // DWORD
  lpnLengthNeeded: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'USER32.dll' name 'GetUserObjectSecurity';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetUserObjectSecurity"
  c_GetUserObjectSecurity :: Ptr () -> Ptr Word32 -> Ptr () -> Word32 -> Ptr Word32 -> IO CInt
-- hObj : HANDLE -> Ptr ()
-- pSIRequested : DWORD* -> Ptr Word32
-- pSID : PSECURITY_DESCRIPTOR optional, out -> Ptr ()
-- nLength : DWORD -> Word32
-- lpnLengthNeeded : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getuserobjectsecurity =
  foreign "GetUserObjectSecurity"
    ((ptr void) @-> (ptr uint32_t) @-> (ptr void) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* hObj : HANDLE -> (ptr void) *)
(* pSIRequested : DWORD* -> (ptr uint32_t) *)
(* pSID : PSECURITY_DESCRIPTOR optional, out -> (ptr void) *)
(* nLength : DWORD -> uint32_t *)
(* lpnLengthNeeded : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("GetUserObjectSecurity" get-user-object-security :convention :stdcall) :int32
  (h-obj :pointer)   ; HANDLE
  (p-sirequested :pointer)   ; DWORD*
  (p-sid :pointer)   ; PSECURITY_DESCRIPTOR optional, out
  (n-length :uint32)   ; DWORD
  (lpn-length-needed :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetUserObjectSecurity = Win32::API::More->new('USER32',
    'BOOL GetUserObjectSecurity(HANDLE hObj, LPVOID pSIRequested, HANDLE pSID, DWORD nLength, LPVOID lpnLengthNeeded)');
# my $ret = $GetUserObjectSecurity->Call($hObj, $pSIRequested, $pSID, $nLength, $lpnLengthNeeded);
# hObj : HANDLE -> HANDLE
# pSIRequested : DWORD* -> LPVOID
# pSID : PSECURITY_DESCRIPTOR optional, out -> HANDLE
# nLength : DWORD -> DWORD
# lpnLengthNeeded : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目