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

GetFileSecurityW

関数
ファイルまたはディレクトリのセキュリティ記述子を取得する。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり

シグネチャ

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetFileSecurityW(
    LPCWSTR lpFileName,
    DWORD RequestedInformation,
    PSECURITY_DESCRIPTOR pSecurityDescriptor,   // optional
    DWORD nLength,
    DWORD* lpnLengthNeeded
);

パラメーター

名前方向説明
lpFileNameLPCWSTRinセキュリティ情報を取得する対象のファイルまたはディレクトリを指定する、null で終わる文字列へのポインターです。
RequestedInformationDWORDin要求するセキュリティ情報を識別する SECURITY_INFORMATION 値です。
pSecurityDescriptorPSECURITY_DESCRIPTORoutoptionallpFileName パラメーターで指定されたオブジェクトの セキュリティ記述子 のコピーを受け取るバッファーへのポインターです。呼び出し元の プロセス は、対象オブジェクトのセキュリティ状態のうち、指定された側面を参照する権限を持っている必要があります。 SECURITY_DESCRIPTOR 構造体は 自己相対形式のセキュリティ記述子 形式で返されます。
nLengthDWORDinpSecurityDescriptor パラメーターが指すバッファーのサイズをバイト単位で指定します。
lpnLengthNeededDWORD*out完全な セキュリティ記述子 を格納するために必要なバイト数を受け取る変数へのポインターです。返されるバイト数が nLength 以下の場合は、セキュリティ記述子全体が出力バッファーに返されます。それ以外の場合は、記述子はまったく返されません。

戻り値の型: BOOL

公式ドキュメント

ファイルまたはディレクトリのセキュリティに関する指定された情報を取得します。取得できる情報は、呼び出し元のアクセス権と特権によって制限されます。(GetFileSecurityW)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

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

解説(Remarks)

指定したファイルまたはディレクトリのセキュリティ記述子から所有者、グループ、または DACL を読み取るには、そのファイルまたはディレクトリの DACL が呼び出し元に READ_CONTROL アクセスを許可しているか、呼び出し元がそのファイルまたはディレクトリの所有者である必要があります。

ファイルまたはディレクトリの SACL を読み取るには、呼び出し元プロセスで SE_SECURITY_NAME 特権が有効になっている必要があります。

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

各言語での呼び出し定義

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetFileSecurityW(
    LPCWSTR lpFileName,
    DWORD RequestedInformation,
    PSECURITY_DESCRIPTOR pSecurityDescriptor,   // optional
    DWORD nLength,
    DWORD* lpnLengthNeeded
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool GetFileSecurityW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,   // LPCWSTR
    uint RequestedInformation,   // DWORD
    IntPtr pSecurityDescriptor,   // PSECURITY_DESCRIPTOR optional, out
    uint nLength,   // DWORD
    out uint lpnLengthNeeded   // DWORD* out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetFileSecurityW(
    <MarshalAs(UnmanagedType.LPWStr)> lpFileName As String,   ' LPCWSTR
    RequestedInformation As UInteger,   ' DWORD
    pSecurityDescriptor As IntPtr,   ' PSECURITY_DESCRIPTOR optional, out
    nLength As UInteger,   ' DWORD
    <Out> ByRef lpnLengthNeeded As UInteger   ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpFileName : LPCWSTR
' RequestedInformation : DWORD
' pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
' nLength : DWORD
' lpnLengthNeeded : DWORD* out
Declare PtrSafe Function GetFileSecurityW Lib "advapi32" ( _
    ByVal lpFileName As LongPtr, _
    ByVal RequestedInformation As Long, _
    ByVal pSecurityDescriptor As LongPtr, _
    ByVal nLength As Long, _
    ByRef lpnLengthNeeded As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetFileSecurityW = ctypes.windll.advapi32.GetFileSecurityW
GetFileSecurityW.restype = wintypes.BOOL
GetFileSecurityW.argtypes = [
    wintypes.LPCWSTR,  # lpFileName : LPCWSTR
    wintypes.DWORD,  # RequestedInformation : DWORD
    wintypes.HANDLE,  # pSecurityDescriptor : 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('ADVAPI32.dll')
GetFileSecurityW = Fiddle::Function.new(
  lib['GetFileSecurityW'],
  [
    Fiddle::TYPE_VOIDP,  # lpFileName : LPCWSTR
    -Fiddle::TYPE_INT,  # RequestedInformation : DWORD
    Fiddle::TYPE_VOIDP,  # pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
    -Fiddle::TYPE_INT,  # nLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpnLengthNeeded : DWORD* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn GetFileSecurityW(
        lpFileName: *const u16,  // LPCWSTR
        RequestedInformation: u32,  // DWORD
        pSecurityDescriptor: *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("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetFileSecurityW([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, uint RequestedInformation, IntPtr pSecurityDescriptor, uint nLength, out uint lpnLengthNeeded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_GetFileSecurityW' -Namespace Win32 -PassThru
# $api::GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
#uselib "ADVAPI32.dll"
#func global GetFileSecurityW "GetFileSecurityW" wptr, wptr, wptr, wptr, wptr
; GetFileSecurityW lpFileName, RequestedInformation, pSecurityDescriptor, nLength, varptr(lpnLengthNeeded)   ; 戻り値は stat
; lpFileName : LPCWSTR -> "wptr"
; RequestedInformation : DWORD -> "wptr"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "wptr"
; nLength : DWORD -> "wptr"
; lpnLengthNeeded : DWORD* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global GetFileSecurityW "GetFileSecurityW" wstr, int, sptr, int, var
; res = GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
; lpFileName : LPCWSTR -> "wstr"
; RequestedInformation : DWORD -> "int"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "sptr"
; nLength : DWORD -> "int"
; lpnLengthNeeded : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetFileSecurityW(LPCWSTR lpFileName, DWORD RequestedInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD nLength, DWORD* lpnLengthNeeded)
#uselib "ADVAPI32.dll"
#cfunc global GetFileSecurityW "GetFileSecurityW" wstr, int, intptr, int, var
; res = GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
; lpFileName : LPCWSTR -> "wstr"
; RequestedInformation : DWORD -> "int"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "intptr"
; nLength : DWORD -> "int"
; lpnLengthNeeded : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procGetFileSecurityW = advapi32.NewProc("GetFileSecurityW")
)

// lpFileName (LPCWSTR), RequestedInformation (DWORD), pSecurityDescriptor (PSECURITY_DESCRIPTOR optional, out), nLength (DWORD), lpnLengthNeeded (DWORD* out)
r1, _, err := procGetFileSecurityW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFileName))),
	uintptr(RequestedInformation),
	uintptr(pSecurityDescriptor),
	uintptr(nLength),
	uintptr(lpnLengthNeeded),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetFileSecurityW(
  lpFileName: PWideChar;   // LPCWSTR
  RequestedInformation: DWORD;   // DWORD
  pSecurityDescriptor: THandle;   // PSECURITY_DESCRIPTOR optional, out
  nLength: DWORD;   // DWORD
  lpnLengthNeeded: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'GetFileSecurityW';
result := DllCall("ADVAPI32\GetFileSecurityW"
    , "WStr", lpFileName   ; LPCWSTR
    , "UInt", RequestedInformation   ; DWORD
    , "Ptr", pSecurityDescriptor   ; PSECURITY_DESCRIPTOR optional, out
    , "UInt", nLength   ; DWORD
    , "Ptr", lpnLengthNeeded   ; DWORD* out
    , "Int")   ; return: BOOL
●GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded) = DLL("ADVAPI32.dll", "bool GetFileSecurityW(char*, dword, void*, dword, void*)")
# 呼び出し: GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
# lpFileName : LPCWSTR -> "char*"
# RequestedInformation : DWORD -> "dword"
# pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "void*"
# nLength : DWORD -> "dword"
# lpnLengthNeeded : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "advapi32" fn GetFileSecurityW(
    lpFileName: [*c]const u16, // LPCWSTR
    RequestedInformation: u32, // DWORD
    pSecurityDescriptor: ?*anyopaque, // PSECURITY_DESCRIPTOR optional, out
    nLength: u32, // DWORD
    lpnLengthNeeded: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetFileSecurityW(
    lpFileName: WideCString,  # LPCWSTR
    RequestedInformation: uint32,  # DWORD
    pSecurityDescriptor: pointer,  # PSECURITY_DESCRIPTOR optional, out
    nLength: uint32,  # DWORD
    lpnLengthNeeded: ptr uint32  # DWORD* out
): int32 {.importc: "GetFileSecurityW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
int GetFileSecurityW(
    const(wchar)* lpFileName,   // LPCWSTR
    uint RequestedInformation,   // DWORD
    void* pSecurityDescriptor,   // PSECURITY_DESCRIPTOR optional, out
    uint nLength,   // DWORD
    uint* lpnLengthNeeded   // DWORD* out
);
ccall((:GetFileSecurityW, "ADVAPI32.dll"), stdcall, Int32,
      (Cwstring, UInt32, Ptr{Cvoid}, UInt32, Ptr{UInt32}),
      lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
# lpFileName : LPCWSTR -> Cwstring
# RequestedInformation : DWORD -> UInt32
# pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> Ptr{Cvoid}
# nLength : DWORD -> UInt32
# lpnLengthNeeded : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetFileSecurityW(
    const uint16_t* lpFileName,
    uint32_t RequestedInformation,
    void* pSecurityDescriptor,
    uint32_t nLength,
    uint32_t* lpnLengthNeeded);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
-- lpFileName : LPCWSTR
-- RequestedInformation : DWORD
-- pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out
-- nLength : DWORD
-- lpnLengthNeeded : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const GetFileSecurityW = lib.func('__stdcall', 'GetFileSecurityW', 'int32_t', ['str16', 'uint32_t', 'void *', 'uint32_t', 'uint32_t *']);
// GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
// lpFileName : LPCWSTR -> 'str16'
// RequestedInformation : DWORD -> 'uint32_t'
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> 'void *'
// nLength : DWORD -> 'uint32_t'
// lpnLengthNeeded : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  GetFileSecurityW: { parameters: ["buffer", "u32", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded)
// lpFileName : LPCWSTR -> "buffer"
// RequestedInformation : DWORD -> "u32"
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> "pointer"
// nLength : DWORD -> "u32"
// lpnLengthNeeded : DWORD* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetFileSecurityW(
    const uint16_t* lpFileName,
    uint32_t RequestedInformation,
    void* pSecurityDescriptor,
    uint32_t nLength,
    uint32_t* lpnLengthNeeded);
C, "ADVAPI32.dll");
// $ffi->GetFileSecurityW(lpFileName, RequestedInformation, pSecurityDescriptor, nLength, lpnLengthNeeded);
// lpFileName : LPCWSTR
// RequestedInformation : DWORD
// pSecurityDescriptor : 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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetFileSecurityW(
        WString lpFileName,   // LPCWSTR
        int RequestedInformation,   // DWORD
        Pointer pSecurityDescriptor,   // PSECURITY_DESCRIPTOR optional, out
        int nLength,   // DWORD
        IntByReference lpnLengthNeeded   // DWORD* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun GetFileSecurityW = GetFileSecurityW(
    lpFileName : UInt16*,   # LPCWSTR
    RequestedInformation : UInt32,   # DWORD
    pSecurityDescriptor : 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 GetFileSecurityWNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Void>, Uint32, Pointer<Uint32>);
typedef GetFileSecurityWDart = int Function(Pointer<Utf16>, int, Pointer<Void>, int, Pointer<Uint32>);
final GetFileSecurityW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<GetFileSecurityWNative, GetFileSecurityWDart>('GetFileSecurityW');
// lpFileName : LPCWSTR -> Pointer<Utf16>
// RequestedInformation : DWORD -> Uint32
// pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> Pointer<Void>
// nLength : DWORD -> Uint32
// lpnLengthNeeded : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetFileSecurityW(
  lpFileName: PWideChar;   // LPCWSTR
  RequestedInformation: DWORD;   // DWORD
  pSecurityDescriptor: THandle;   // PSECURITY_DESCRIPTOR optional, out
  nLength: DWORD;   // DWORD
  lpnLengthNeeded: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'GetFileSecurityW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetFileSecurityW"
  c_GetFileSecurityW :: CWString -> Word32 -> Ptr () -> Word32 -> Ptr Word32 -> IO CInt
-- lpFileName : LPCWSTR -> CWString
-- RequestedInformation : DWORD -> Word32
-- pSecurityDescriptor : 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 getfilesecurityw =
  foreign "GetFileSecurityW"
    ((ptr uint16_t) @-> uint32_t @-> (ptr void) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* RequestedInformation : DWORD -> uint32_t *)
(* pSecurityDescriptor : 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 advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("GetFileSecurityW" get-file-security-w :convention :stdcall) :int32
  (lp-file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (requested-information :uint32)   ; DWORD
  (p-security-descriptor :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 $GetFileSecurityW = Win32::API::More->new('ADVAPI32',
    'BOOL GetFileSecurityW(LPCWSTR lpFileName, DWORD RequestedInformation, HANDLE pSecurityDescriptor, DWORD nLength, LPVOID lpnLengthNeeded)');
# my $ret = $GetFileSecurityW->Call($lpFileName, $RequestedInformation, $pSecurityDescriptor, $nLength, $lpnLengthNeeded);
# lpFileName : LPCWSTR -> LPCWSTR
# RequestedInformation : DWORD -> DWORD
# pSecurityDescriptor : PSECURITY_DESCRIPTOR optional, out -> HANDLE
# nLength : DWORD -> DWORD
# lpnLengthNeeded : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目