RegGetKeySecurity
関数シグネチャ
// ADVAPI32.dll
#include <windows.h>
WIN32_ERROR RegGetKeySecurity(
HKEY hKey,
OBJECT_SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor, // optional
DWORD* lpcbSecurityDescriptor
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | セキュリティ記述子を取得する対象の、開いているキーのハンドル。 |
| SecurityInformation | OBJECT_SECURITY_INFORMATION | in | 要求するセキュリティ情報を示す SECURITY_INFORMATION 値。 |
| pSecurityDescriptor | PSECURITY_DESCRIPTOR | outoptional | 要求されたセキュリティ記述子のコピーを受け取るバッファーへのポインター。 |
| lpcbSecurityDescriptor | DWORD* | inout | pSecurityDescriptor パラメーターが指すバッファーのサイズ(バイト単位)を指定する変数へのポインター。関数が返ると、この変数にはバッファーに書き込まれたバイト数が格納されます。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
指定された開いているレジストリキーを保護しているセキュリティ記述子のコピーを取得します。
戻り値
関数が成功した場合、ERROR_SUCCESS を返します。
関数が失敗した場合、WinError.h で定義されている 0 以外のエラーコードを返します。 FormatMessage 関数を FORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用すると、エラーの一般的な説明を取得できます。
解説(Remarks)
pSecurityDescriptor パラメーターで指定したバッファーが小さすぎる場合、関数は ERROR_INSUFFICIENT_BUFFER を返し、lpcbSecurityDescriptor パラメーターには要求されたセキュリティ記述子に必要なバイト数が格納されます。
キーのセキュリティ記述子から所有者、グループ、または 随意アクセス制御リスト (DACL) を読み取るには、ハンドルを開いた際に呼び出し元の プロセス に READ_CONTROL アクセス権が付与されている必要があります。READ_CONTROL アクセス権を取得するには、呼び出し元がキーの所有者であるか、キーの DACL がそのアクセスを許可している必要があります。
セキュリティ記述子から システムアクセス制御リスト (SACL) を読み取るには、キーを開いた際に呼び出し元のプロセスに ACCESS_SYSTEM_SECURITY アクセス権が付与されている必要があります。このアクセス権を取得する正しい方法は、呼び出し元の現在のトークンで SE_SECURITY_NAME 特権を有効にし、ACCESS_SYSTEM_SECURITY アクセス権でハンドルを開いた後、特権を無効にすることです。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
WIN32_ERROR RegGetKeySecurity(
HKEY hKey,
OBJECT_SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor, // optional
DWORD* lpcbSecurityDescriptor
);[DllImport("ADVAPI32.dll", ExactSpelling = true)]
static extern uint RegGetKeySecurity(
IntPtr hKey, // HKEY
uint SecurityInformation, // OBJECT_SECURITY_INFORMATION
IntPtr pSecurityDescriptor, // PSECURITY_DESCRIPTOR optional, out
ref uint lpcbSecurityDescriptor // DWORD* in/out
);<DllImport("ADVAPI32.dll", ExactSpelling:=True)>
Public Shared Function RegGetKeySecurity(
hKey As IntPtr, ' HKEY
SecurityInformation As UInteger, ' OBJECT_SECURITY_INFORMATION
pSecurityDescriptor As IntPtr, ' PSECURITY_DESCRIPTOR optional, out
ByRef lpcbSecurityDescriptor As UInteger ' DWORD* in/out
) As UInteger
End Function' hKey : HKEY
' SecurityInformation : OBJECT_SECURITY_INFORMATION
' pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
' lpcbSecurityDescriptor : DWORD* in/out
Declare PtrSafe Function RegGetKeySecurity Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal SecurityInformation As Long, _
ByVal pSecurityDescriptor As LongPtr, _
ByRef lpcbSecurityDescriptor As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RegGetKeySecurity = ctypes.windll.advapi32.RegGetKeySecurity
RegGetKeySecurity.restype = wintypes.DWORD
RegGetKeySecurity.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.DWORD, # SecurityInformation : OBJECT_SECURITY_INFORMATION
wintypes.HANDLE, # pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
ctypes.POINTER(wintypes.DWORD), # lpcbSecurityDescriptor : DWORD* in/out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RegGetKeySecurity = Fiddle::Function.new(
lib['RegGetKeySecurity'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
-Fiddle::TYPE_INT, # SecurityInformation : OBJECT_SECURITY_INFORMATION
Fiddle::TYPE_VOIDP, # pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
Fiddle::TYPE_VOIDP, # lpcbSecurityDescriptor : DWORD* in/out
],
-Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn RegGetKeySecurity(
hKey: *mut core::ffi::c_void, // HKEY
SecurityInformation: u32, // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: *mut core::ffi::c_void, // PSECURITY_DESCRIPTOR optional, out
lpcbSecurityDescriptor: *mut u32 // DWORD* in/out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ADVAPI32.dll")]
public static extern uint RegGetKeySecurity(IntPtr hKey, uint SecurityInformation, IntPtr pSecurityDescriptor, ref uint lpcbSecurityDescriptor);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegGetKeySecurity' -Namespace Win32 -PassThru
# $api::RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor)#uselib "ADVAPI32.dll"
#func global RegGetKeySecurity "RegGetKeySecurity" sptr, sptr, sptr, sptr
; RegGetKeySecurity hKey, SecurityInformation, pSecurityDescriptor, varptr(lpcbSecurityDescriptor) ; 戻り値は stat
; hKey : HKEY -> "sptr"
; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "sptr"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "sptr"
; lpcbSecurityDescriptor : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll" #cfunc global RegGetKeySecurity "RegGetKeySecurity" sptr, int, sptr, var ; res = RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor) ; hKey : HKEY -> "sptr" ; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "int" ; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "sptr" ; lpcbSecurityDescriptor : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global RegGetKeySecurity "RegGetKeySecurity" sptr, int, sptr, sptr ; res = RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, varptr(lpcbSecurityDescriptor)) ; hKey : HKEY -> "sptr" ; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "int" ; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "sptr" ; lpcbSecurityDescriptor : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; WIN32_ERROR RegGetKeySecurity(HKEY hKey, OBJECT_SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD* lpcbSecurityDescriptor) #uselib "ADVAPI32.dll" #cfunc global RegGetKeySecurity "RegGetKeySecurity" intptr, int, intptr, var ; res = RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor) ; hKey : HKEY -> "intptr" ; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "int" ; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "intptr" ; lpcbSecurityDescriptor : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; WIN32_ERROR RegGetKeySecurity(HKEY hKey, OBJECT_SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD* lpcbSecurityDescriptor) #uselib "ADVAPI32.dll" #cfunc global RegGetKeySecurity "RegGetKeySecurity" intptr, int, intptr, intptr ; res = RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, varptr(lpcbSecurityDescriptor)) ; hKey : HKEY -> "intptr" ; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "int" ; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "intptr" ; lpcbSecurityDescriptor : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegGetKeySecurity = advapi32.NewProc("RegGetKeySecurity")
)
// hKey (HKEY), SecurityInformation (OBJECT_SECURITY_INFORMATION), pSecurityDescriptor (PSECURITY_DESCRIPTOR optional, out), lpcbSecurityDescriptor (DWORD* in/out)
r1, _, err := procRegGetKeySecurity.Call(
uintptr(hKey),
uintptr(SecurityInformation),
uintptr(pSecurityDescriptor),
uintptr(lpcbSecurityDescriptor),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegGetKeySecurity(
hKey: THandle; // HKEY
SecurityInformation: DWORD; // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: THandle; // PSECURITY_DESCRIPTOR optional, out
lpcbSecurityDescriptor: Pointer // DWORD* in/out
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegGetKeySecurity';result := DllCall("ADVAPI32\RegGetKeySecurity"
, "Ptr", hKey ; HKEY
, "UInt", SecurityInformation ; OBJECT_SECURITY_INFORMATION
, "Ptr", pSecurityDescriptor ; PSECURITY_DESCRIPTOR optional, out
, "Ptr", lpcbSecurityDescriptor ; DWORD* in/out
, "UInt") ; return: WIN32_ERROR●RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor) = DLL("ADVAPI32.dll", "dword RegGetKeySecurity(void*, dword, void*, void*)")
# 呼び出し: RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor)
# hKey : HKEY -> "void*"
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> "dword"
# pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "void*"
# lpcbSecurityDescriptor : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn RegGetKeySecurity(
hKey: ?*anyopaque, // HKEY
SecurityInformation: u32, // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: ?*anyopaque, // PSECURITY_DESCRIPTOR optional, out
lpcbSecurityDescriptor: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u32;proc RegGetKeySecurity(
hKey: pointer, # HKEY
SecurityInformation: uint32, # OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: pointer, # PSECURITY_DESCRIPTOR optional, out
lpcbSecurityDescriptor: ptr uint32 # DWORD* in/out
): uint32 {.importc: "RegGetKeySecurity", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
uint RegGetKeySecurity(
void* hKey, // HKEY
uint SecurityInformation, // OBJECT_SECURITY_INFORMATION
void* pSecurityDescriptor, // PSECURITY_DESCRIPTOR optional, out
uint* lpcbSecurityDescriptor // DWORD* in/out
);ccall((:RegGetKeySecurity, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, UInt32, Ptr{Cvoid}, Ptr{UInt32}),
hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor)
# hKey : HKEY -> Ptr{Cvoid}
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> UInt32
# pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> Ptr{Cvoid}
# lpcbSecurityDescriptor : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegGetKeySecurity(
void* hKey,
uint32_t SecurityInformation,
void* pSecurityDescriptor,
uint32_t* lpcbSecurityDescriptor);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor)
-- hKey : HKEY
-- SecurityInformation : OBJECT_SECURITY_INFORMATION
-- pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
-- lpcbSecurityDescriptor : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegGetKeySecurity = lib.func('__stdcall', 'RegGetKeySecurity', 'uint32_t', ['void *', 'uint32_t', 'void *', 'uint32_t *']);
// RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor)
// hKey : HKEY -> 'void *'
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> 'uint32_t'
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> 'void *'
// lpcbSecurityDescriptor : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegGetKeySecurity: { parameters: ["pointer", "u32", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor)
// hKey : HKEY -> "pointer"
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> "u32"
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "pointer"
// lpcbSecurityDescriptor : DWORD* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegGetKeySecurity(
void* hKey,
uint32_t SecurityInformation,
void* pSecurityDescriptor,
uint32_t* lpcbSecurityDescriptor);
C, "ADVAPI32.dll");
// $ffi->RegGetKeySecurity(hKey, SecurityInformation, pSecurityDescriptor, lpcbSecurityDescriptor);
// hKey : HKEY
// SecurityInformation : OBJECT_SECURITY_INFORMATION
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
// lpcbSecurityDescriptor : DWORD* in/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 Advapi32 extends StdCallLibrary {
Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class);
int RegGetKeySecurity(
Pointer hKey, // HKEY
int SecurityInformation, // OBJECT_SECURITY_INFORMATION
Pointer pSecurityDescriptor, // PSECURITY_DESCRIPTOR optional, out
IntByReference lpcbSecurityDescriptor // DWORD* in/out
);
}@[Link("advapi32")]
lib LibADVAPI32
fun RegGetKeySecurity = RegGetKeySecurity(
hKey : Void*, # HKEY
SecurityInformation : UInt32, # OBJECT_SECURITY_INFORMATION
pSecurityDescriptor : Void*, # PSECURITY_DESCRIPTOR optional, out
lpcbSecurityDescriptor : UInt32* # DWORD* in/out
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef RegGetKeySecurityNative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Void>, Pointer<Uint32>);
typedef RegGetKeySecurityDart = int Function(Pointer<Void>, int, Pointer<Void>, Pointer<Uint32>);
final RegGetKeySecurity = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegGetKeySecurityNative, RegGetKeySecurityDart>('RegGetKeySecurity');
// hKey : HKEY -> Pointer<Void>
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> Uint32
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> Pointer<Void>
// lpcbSecurityDescriptor : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegGetKeySecurity(
hKey: THandle; // HKEY
SecurityInformation: DWORD; // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: THandle; // PSECURITY_DESCRIPTOR optional, out
lpcbSecurityDescriptor: Pointer // DWORD* in/out
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegGetKeySecurity';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegGetKeySecurity"
c_RegGetKeySecurity :: Ptr () -> Word32 -> Ptr () -> Ptr Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- SecurityInformation : OBJECT_SECURITY_INFORMATION -> Word32
-- pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> Ptr ()
-- lpcbSecurityDescriptor : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let reggetkeysecurity =
foreign "RegGetKeySecurity"
((ptr void) @-> uint32_t @-> (ptr void) @-> (ptr uint32_t) @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* SecurityInformation : OBJECT_SECURITY_INFORMATION -> uint32_t *)
(* pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> (ptr void) *)
(* lpcbSecurityDescriptor : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("RegGetKeySecurity" reg-get-key-security :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(security-information :uint32) ; OBJECT_SECURITY_INFORMATION
(p-security-descriptor :pointer) ; PSECURITY_DESCRIPTOR optional, out
(lpcb-security-descriptor :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegGetKeySecurity = Win32::API::More->new('ADVAPI32',
'DWORD RegGetKeySecurity(HANDLE hKey, DWORD SecurityInformation, HANDLE pSecurityDescriptor, LPVOID lpcbSecurityDescriptor)');
# my $ret = $RegGetKeySecurity->Call($hKey, $SecurityInformation, $pSecurityDescriptor, $lpcbSecurityDescriptor);
# hKey : HKEY -> HANDLE
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> DWORD
# pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> HANDLE
# lpcbSecurityDescriptor : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f RegDeleteKeyW — 指定したサブキーをレジストリから削除する。
- f RegOpenKeyExW — アクセス権を指定してレジストリキーを開く。
- f RegSetKeySecurity — 指定したレジストリキーのセキュリティ記述子を設定する。