SetFileSecurityA
関数指定ファイルのセキュリティ情報を設定する(ANSI版)。
シグネチャ
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
BOOL SetFileSecurityA(
LPCSTR lpFileName,
OBJECT_SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFileName | LPCSTR | in | セキュリティを設定する対象のファイルまたはディレクトリを指定する、null 終端文字列へのポインターです。ディレクトリに適用されたセキュリティは、その子に継承されないことに注意してください。 |
| SecurityInformation | OBJECT_SECURITY_INFORMATION | in | pSecurityDescriptor パラメーターが指すセキュリティ記述子の内容を識別する SECURITY_INFORMATION 構造体を指定します。 |
| pSecurityDescriptor | PSECURITY_DESCRIPTOR | in | SECURITY_DESCRIPTOR 構造体へのポインターです。 |
戻り値の型: BOOL
公式ドキュメント
SetFileSecurityA (ANSI) 関数 (winbase.h) は、ファイルまたはディレクトリ オブジェクトのセキュリティを設定します。
戻り値
関数が成功した場合、0 以外の値を返します。
関数が失敗した場合、0 を返します。拡張エラー情報を取得するには、 GetLastError を呼び出してください。
解説(Remarks)
SetFileSecurity 関数は、次の条件が満たされた場合にのみ成功します。
- オブジェクトの所有者を設定する場合、呼び出し元のプロセスは WRITE_OWNER アクセス許可を持っているか、オブジェクトの所有者である必要があります。
- オブジェクトの随意アクセス制御リスト (DACL) を設定する場合、呼び出し元のプロセスは WRITE_DAC アクセス許可を持っているか、オブジェクトの所有者である必要があります。
- オブジェクトのシステム アクセス制御リスト (SACL) を設定する場合、呼び出し元のプロセスで SE_SECURITY_NAME 特権が有効になっている必要があります。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
BOOL SetFileSecurityA(
LPCSTR lpFileName,
OBJECT_SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool SetFileSecurityA(
[MarshalAs(UnmanagedType.LPStr)] string lpFileName, // LPCSTR
uint SecurityInformation, // OBJECT_SECURITY_INFORMATION
IntPtr pSecurityDescriptor // PSECURITY_DESCRIPTOR
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetFileSecurityA(
<MarshalAs(UnmanagedType.LPStr)> lpFileName As String, ' LPCSTR
SecurityInformation As UInteger, ' OBJECT_SECURITY_INFORMATION
pSecurityDescriptor As IntPtr ' PSECURITY_DESCRIPTOR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpFileName : LPCSTR
' SecurityInformation : OBJECT_SECURITY_INFORMATION
' pSecurityDescriptor : PSECURITY_DESCRIPTOR
Declare PtrSafe Function SetFileSecurityA Lib "advapi32" ( _
ByVal lpFileName As String, _
ByVal SecurityInformation As Long, _
ByVal pSecurityDescriptor As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetFileSecurityA = ctypes.windll.advapi32.SetFileSecurityA
SetFileSecurityA.restype = wintypes.BOOL
SetFileSecurityA.argtypes = [
wintypes.LPCSTR, # lpFileName : LPCSTR
wintypes.DWORD, # SecurityInformation : OBJECT_SECURITY_INFORMATION
wintypes.HANDLE, # pSecurityDescriptor : PSECURITY_DESCRIPTOR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
SetFileSecurityA = Fiddle::Function.new(
lib['SetFileSecurityA'],
[
Fiddle::TYPE_VOIDP, # lpFileName : LPCSTR
-Fiddle::TYPE_INT, # SecurityInformation : OBJECT_SECURITY_INFORMATION
Fiddle::TYPE_VOIDP, # pSecurityDescriptor : PSECURITY_DESCRIPTOR
],
Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn SetFileSecurityA(
lpFileName: *const u8, // LPCSTR
SecurityInformation: u32, // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: *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", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool SetFileSecurityA([MarshalAs(UnmanagedType.LPStr)] string lpFileName, uint SecurityInformation, IntPtr pSecurityDescriptor);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_SetFileSecurityA' -Namespace Win32 -PassThru
# $api::SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)#uselib "ADVAPI32.dll"
#func global SetFileSecurityA "SetFileSecurityA" sptr, sptr, sptr
; SetFileSecurityA lpFileName, SecurityInformation, pSecurityDescriptor ; 戻り値は stat
; lpFileName : LPCSTR -> "sptr"
; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "sptr"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global SetFileSecurityA "SetFileSecurityA" str, int, sptr
; res = SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)
; lpFileName : LPCSTR -> "str"
; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "int"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR -> "sptr"; BOOL SetFileSecurityA(LPCSTR lpFileName, OBJECT_SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor)
#uselib "ADVAPI32.dll"
#cfunc global SetFileSecurityA "SetFileSecurityA" str, int, intptr
; res = SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)
; lpFileName : LPCSTR -> "str"
; SecurityInformation : OBJECT_SECURITY_INFORMATION -> "int"
; pSecurityDescriptor : PSECURITY_DESCRIPTOR -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procSetFileSecurityA = advapi32.NewProc("SetFileSecurityA")
)
// lpFileName (LPCSTR), SecurityInformation (OBJECT_SECURITY_INFORMATION), pSecurityDescriptor (PSECURITY_DESCRIPTOR)
r1, _, err := procSetFileSecurityA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFileName))),
uintptr(SecurityInformation),
uintptr(pSecurityDescriptor),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetFileSecurityA(
lpFileName: PAnsiChar; // LPCSTR
SecurityInformation: DWORD; // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: THandle // PSECURITY_DESCRIPTOR
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'SetFileSecurityA';result := DllCall("ADVAPI32\SetFileSecurityA"
, "AStr", lpFileName ; LPCSTR
, "UInt", SecurityInformation ; OBJECT_SECURITY_INFORMATION
, "Ptr", pSecurityDescriptor ; PSECURITY_DESCRIPTOR
, "Int") ; return: BOOL●SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor) = DLL("ADVAPI32.dll", "bool SetFileSecurityA(char*, dword, void*)")
# 呼び出し: SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)
# lpFileName : LPCSTR -> "char*"
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> "dword"
# pSecurityDescriptor : PSECURITY_DESCRIPTOR -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn SetFileSecurityA(
lpFileName: [*c]const u8, // LPCSTR
SecurityInformation: u32, // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: ?*anyopaque // PSECURITY_DESCRIPTOR
) callconv(std.os.windows.WINAPI) i32;proc SetFileSecurityA(
lpFileName: cstring, # LPCSTR
SecurityInformation: uint32, # OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: pointer # PSECURITY_DESCRIPTOR
): int32 {.importc: "SetFileSecurityA", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
int SetFileSecurityA(
const(char)* lpFileName, // LPCSTR
uint SecurityInformation, // OBJECT_SECURITY_INFORMATION
void* pSecurityDescriptor // PSECURITY_DESCRIPTOR
);ccall((:SetFileSecurityA, "ADVAPI32.dll"), stdcall, Int32,
(Cstring, UInt32, Ptr{Cvoid}),
lpFileName, SecurityInformation, pSecurityDescriptor)
# lpFileName : LPCSTR -> Cstring
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> UInt32
# pSecurityDescriptor : PSECURITY_DESCRIPTOR -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetFileSecurityA(
const char* lpFileName,
uint32_t SecurityInformation,
void* pSecurityDescriptor);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)
-- lpFileName : LPCSTR
-- SecurityInformation : OBJECT_SECURITY_INFORMATION
-- pSecurityDescriptor : PSECURITY_DESCRIPTOR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const SetFileSecurityA = lib.func('__stdcall', 'SetFileSecurityA', 'int32_t', ['str', 'uint32_t', 'void *']);
// SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)
// lpFileName : LPCSTR -> 'str'
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> 'uint32_t'
// pSecurityDescriptor : PSECURITY_DESCRIPTOR -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
SetFileSecurityA: { parameters: ["buffer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor)
// lpFileName : LPCSTR -> "buffer"
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> "u32"
// pSecurityDescriptor : PSECURITY_DESCRIPTOR -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetFileSecurityA(
const char* lpFileName,
uint32_t SecurityInformation,
void* pSecurityDescriptor);
C, "ADVAPI32.dll");
// $ffi->SetFileSecurityA(lpFileName, SecurityInformation, pSecurityDescriptor);
// lpFileName : LPCSTR
// SecurityInformation : OBJECT_SECURITY_INFORMATION
// pSecurityDescriptor : 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, W32APIOptions.ASCII_OPTIONS);
boolean SetFileSecurityA(
String lpFileName, // LPCSTR
int SecurityInformation, // OBJECT_SECURITY_INFORMATION
Pointer pSecurityDescriptor // PSECURITY_DESCRIPTOR
);
}@[Link("advapi32")]
lib LibADVAPI32
fun SetFileSecurityA = SetFileSecurityA(
lpFileName : UInt8*, # LPCSTR
SecurityInformation : UInt32, # OBJECT_SECURITY_INFORMATION
pSecurityDescriptor : 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 SetFileSecurityANative = Int32 Function(Pointer<Utf8>, Uint32, Pointer<Void>);
typedef SetFileSecurityADart = int Function(Pointer<Utf8>, int, Pointer<Void>);
final SetFileSecurityA = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<SetFileSecurityANative, SetFileSecurityADart>('SetFileSecurityA');
// lpFileName : LPCSTR -> Pointer<Utf8>
// SecurityInformation : OBJECT_SECURITY_INFORMATION -> Uint32
// pSecurityDescriptor : PSECURITY_DESCRIPTOR -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetFileSecurityA(
lpFileName: PAnsiChar; // LPCSTR
SecurityInformation: DWORD; // OBJECT_SECURITY_INFORMATION
pSecurityDescriptor: THandle // PSECURITY_DESCRIPTOR
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'SetFileSecurityA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetFileSecurityA"
c_SetFileSecurityA :: CString -> Word32 -> Ptr () -> IO CInt
-- lpFileName : LPCSTR -> CString
-- SecurityInformation : OBJECT_SECURITY_INFORMATION -> Word32
-- pSecurityDescriptor : PSECURITY_DESCRIPTOR -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setfilesecuritya =
foreign "SetFileSecurityA"
(string @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* lpFileName : LPCSTR -> string *)
(* SecurityInformation : OBJECT_SECURITY_INFORMATION -> uint32_t *)
(* pSecurityDescriptor : 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 ("SetFileSecurityA" set-file-security-a :convention :stdcall) :int32
(lp-file-name :string) ; LPCSTR
(security-information :uint32) ; OBJECT_SECURITY_INFORMATION
(p-security-descriptor :pointer)) ; PSECURITY_DESCRIPTOR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetFileSecurityA = Win32::API::More->new('ADVAPI32',
'BOOL SetFileSecurityA(LPCSTR lpFileName, DWORD SecurityInformation, HANDLE pSecurityDescriptor)');
# my $ret = $SetFileSecurityA->Call($lpFileName, $SecurityInformation, $pSecurityDescriptor);
# lpFileName : LPCSTR -> LPCSTR
# SecurityInformation : OBJECT_SECURITY_INFORMATION -> DWORD
# pSecurityDescriptor : PSECURITY_DESCRIPTOR -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f SetFileSecurityW (Unicode版) — ファイルまたはディレクトリにセキュリティ記述子を設定する。
公式の関連項目
- f GetFileSecurityA — 指定ファイルのセキュリティ記述子を取得する(ANSI版)。
- s SECURITY_DESCRIPTOR
- f SetKernelObjectSecurity — 指定カーネルオブジェクトのセキュリティ記述子を設定する。
- f SetNamedSecurityInfoA — 名前付きオブジェクトのセキュリティ情報を設定する(ANSI版)。
- f SetPrivateObjectSecurity — プライベートオブジェクトのセキュリティ記述子を変更し更新する。
- f SetUserObjectSecurity — ユーザーオブジェクトのセキュリティ記述子を設定する。