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
RequestedInformationDWORDin
pSecurityDescriptorPSECURITY_DESCRIPTORoutoptional
nLengthDWORDin
lpnLengthNeededDWORD*out

戻り値の型: BOOL

各言語での呼び出し定義

// 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 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 版の利用を推奨。