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

SetUserObjectSecurity

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

シグネチャ

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

BOOL SetUserObjectSecurity(
    HANDLE hObj,
    OBJECT_SECURITY_INFORMATION* pSIRequested,
    PSECURITY_DESCRIPTOR pSID
);

パラメーター

名前方向
hObjHANDLEin
pSIRequestedOBJECT_SECURITY_INFORMATION*in
pSIDPSECURITY_DESCRIPTORin

戻り値の型: BOOL

各言語での呼び出し定義

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

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

SetUserObjectSecurity = ctypes.windll.user32.SetUserObjectSecurity
SetUserObjectSecurity.restype = wintypes.BOOL
SetUserObjectSecurity.argtypes = [
    wintypes.HANDLE,  # hObj : HANDLE
    ctypes.c_void_p,  # pSIRequested : OBJECT_SECURITY_INFORMATION*
    wintypes.HANDLE,  # pSID : PSECURITY_DESCRIPTOR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetUserObjectSecurity = Fiddle::Function.new(
  lib['SetUserObjectSecurity'],
  [
    Fiddle::TYPE_VOIDP,  # hObj : HANDLE
    Fiddle::TYPE_VOIDP,  # pSIRequested : OBJECT_SECURITY_INFORMATION*
    Fiddle::TYPE_VOIDP,  # pSID : PSECURITY_DESCRIPTOR
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetUserObjectSecurity(
        hObj: *mut core::ffi::c_void,  // HANDLE
        pSIRequested: *mut u32,  // OBJECT_SECURITY_INFORMATION*
        pSID: *mut core::ffi::c_void  // PSECURITY_DESCRIPTOR
    ) -> 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 SetUserObjectSecurity(IntPtr hObj, ref uint pSIRequested, IntPtr pSID);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetUserObjectSecurity' -Namespace Win32 -PassThru
# $api::SetUserObjectSecurity(hObj, pSIRequested, pSID)
#uselib "USER32.dll"
#func global SetUserObjectSecurity "SetUserObjectSecurity" sptr, sptr, sptr
; SetUserObjectSecurity hObj, pSIRequested, pSID   ; 戻り値は stat
; hObj : HANDLE -> "sptr"
; pSIRequested : OBJECT_SECURITY_INFORMATION* -> "sptr"
; pSID : PSECURITY_DESCRIPTOR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetUserObjectSecurity "SetUserObjectSecurity" sptr, int, sptr
; res = SetUserObjectSecurity(hObj, pSIRequested, pSID)
; hObj : HANDLE -> "sptr"
; pSIRequested : OBJECT_SECURITY_INFORMATION* -> "int"
; pSID : PSECURITY_DESCRIPTOR -> "sptr"
; BOOL SetUserObjectSecurity(HANDLE hObj, OBJECT_SECURITY_INFORMATION* pSIRequested, PSECURITY_DESCRIPTOR pSID)
#uselib "USER32.dll"
#cfunc global SetUserObjectSecurity "SetUserObjectSecurity" intptr, int, intptr
; res = SetUserObjectSecurity(hObj, pSIRequested, pSID)
; hObj : HANDLE -> "intptr"
; pSIRequested : OBJECT_SECURITY_INFORMATION* -> "int"
; pSID : PSECURITY_DESCRIPTOR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetUserObjectSecurity = user32.NewProc("SetUserObjectSecurity")
)

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