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

SetKernelObjectSecurity

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

シグネチャ

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

BOOL SetKernelObjectSecurity(
    HANDLE Handle,
    OBJECT_SECURITY_INFORMATION SecurityInformation,
    PSECURITY_DESCRIPTOR SecurityDescriptor
);

パラメーター

名前方向説明
HandleHANDLEinセキュリティ情報を設定する対象のカーネルオブジェクトへのハンドル。
SecurityInformationOBJECT_SECURITY_INFORMATIONin設定するセキュリティ情報の種類を示す ビットフラグのセット。このパラメーターには、 SECURITY_INFORMATION ビットフラグの組み合わせを指定できます。
SecurityDescriptorPSECURITY_DESCRIPTORin新しいセキュリティ情報を格納する SECURITY_DESCRIPTOR 構造体へのポインター。

戻り値の型: BOOL

公式ドキュメント

カーネルオブジェクトのセキュリティを設定します。

戻り値

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

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

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

各言語での呼び出し定義

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

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

SetKernelObjectSecurity = ctypes.windll.advapi32.SetKernelObjectSecurity
SetKernelObjectSecurity.restype = wintypes.BOOL
SetKernelObjectSecurity.argtypes = [
    wintypes.HANDLE,  # Handle : HANDLE
    wintypes.DWORD,  # SecurityInformation : OBJECT_SECURITY_INFORMATION
    wintypes.HANDLE,  # SecurityDescriptor : PSECURITY_DESCRIPTOR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procSetKernelObjectSecurity = advapi32.NewProc("SetKernelObjectSecurity")
)

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

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

typedef SetKernelObjectSecurityNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Void>);
typedef SetKernelObjectSecurityDart = int Function(Pointer<Void>, int, Pointer<Void>);
final SetKernelObjectSecurity = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<SetKernelObjectSecurityNative, SetKernelObjectSecurityDart>('SetKernelObjectSecurity');
// Handle : HANDLE -> Pointer<Void>
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> Uint32
// SecurityDescriptor : PSECURITY_DESCRIPTOR -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetKernelObjectSecurity(
  Handle: THandle;   // HANDLE
  SecurityInformation: DWORD;   // OBJECT_SECURITY_INFORMATION
  SecurityDescriptor: THandle   // PSECURITY_DESCRIPTOR
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'SetKernelObjectSecurity';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetKernelObjectSecurity"
  c_SetKernelObjectSecurity :: Ptr () -> Word32 -> Ptr () -> IO CInt
-- Handle : HANDLE -> Ptr ()
-- SecurityInformation : OBJECT_SECURITY_INFORMATION -> Word32
-- SecurityDescriptor : PSECURITY_DESCRIPTOR -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setkernelobjectsecurity =
  foreign "SetKernelObjectSecurity"
    ((ptr void) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* Handle : HANDLE -> (ptr void) *)
(* SecurityInformation : OBJECT_SECURITY_INFORMATION -> uint32_t *)
(* SecurityDescriptor : PSECURITY_DESCRIPTOR -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("SetKernelObjectSecurity" set-kernel-object-security :convention :stdcall) :int32
  (handle :pointer)   ; HANDLE
  (security-information :uint32)   ; OBJECT_SECURITY_INFORMATION
  (security-descriptor :pointer))   ; PSECURITY_DESCRIPTOR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetKernelObjectSecurity = Win32::API::More->new('ADVAPI32',
    'BOOL SetKernelObjectSecurity(HANDLE Handle, DWORD SecurityInformation, HANDLE SecurityDescriptor)');
# my $ret = $SetKernelObjectSecurity->Call($Handle, $SecurityInformation, $SecurityDescriptor);
# Handle : HANDLE -> HANDLE
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> DWORD
# SecurityDescriptor : PSECURITY_DESCRIPTOR -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型