SetFileAttributesTransactedW
関数シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL SetFileAttributesTransactedW(
LPCWSTR lpFileName,
DWORD dwFileAttributes,
HANDLE hTransaction
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFileName | LPCWSTR | in | 属性を設定する対象のファイルの名前です。 ファイルはローカルコンピューター上に存在する必要があります。そうでない場合、関数は失敗し、最終エラーコードに ERROR_TRANSACTIONS_UNSUPPORTED_REMOTE が設定されます。 既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳細については、Naming Files, Paths, and Namespaces を参照してください。 ヒント
Windows 10 Version 1607 以降では、"\\?\" を付加することなく MAX_PATH 制限を解除するようにオプトインできます。詳細については、Naming Files, Paths, and Namespaces の「Maximum Path Length Limitation」セクションを参照してください。 |
| dwFileAttributes | DWORD | in | ファイルに設定するファイル属性です。 ファイル属性値とその説明の一覧については、 File Attribute Constants を参照してください。このパラメーターには、ビット単位の OR 演算子で結合した 1 つ以上の値を指定できます。ただし、その他のすべての値は FILE_ATTRIBUTE_NORMAL よりも優先されます。 すべての属性がこの関数でサポートされているわけではありません。詳細については、「解説」セクションを参照してください。 以下はサポートされる属性値の一覧です。 FILE_ATTRIBUTE_ARCHIVE (32 (0x20))FILE_ATTRIBUTE_HIDDEN (2 (0x2))FILE_ATTRIBUTE_NORMAL (128 (0x80))FILE_ATTRIBUTE_NOT_CONTENT_INDEXED (8192 (0x2000))FILE_ATTRIBUTE_OFFLINE (4096 (0x1000))FILE_ATTRIBUTE_READONLY (1 (0x1))FILE_ATTRIBUTE_SYSTEM (4 (0x4))FILE_ATTRIBUTE_TEMPORARY (256 (0x100)) |
| hTransaction | HANDLE | in | トランザクションへのハンドルです。このハンドルは CreateTransaction 関数によって返されます。 - dwFileAttributes.FILE_ATTRIBUTE_ARCHIVE (32 (0x20))- dwFileAttributes.FILE_ATTRIBUTE_HIDDEN (2 (0x2))- dwFileAttributes.FILE_ATTRIBUTE_NORMAL (128 (0x80))- dwFileAttributes.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED (8192 (0x2000))- dwFileAttributes.FILE_ATTRIBUTE_OFFLINE (4096 (0x1000))- dwFileAttributes.FILE_ATTRIBUTE_READONLY (1 (0x1))- dwFileAttributes.FILE_ATTRIBUTE_SYSTEM (4 (0x4))- dwFileAttributes.FILE_ATTRIBUTE_TEMPORARY (256 (0x100)) |
戻り値の型: BOOL
公式ドキュメント
ファイルまたはディレクトリの属性をトランザクション操作として設定します。(Unicode)
戻り値
関数が成功した場合、戻り値は 0 以外の値になります。
関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
次の表は、SetFileAttributesTransacted では設定できない属性を設定する方法を説明しています。これらはトランザクション操作ではない点に注意してください。
| 属性 | 設定方法 |
|---|---|
|
FILE_ATTRIBUTE_COMPRESSED
0x800 |
ファイルの圧縮状態を設定するには、FSCTL_SET_COMPRESSION 操作とともに DeviceIoControl 関数を使用します。 |
|
FILE_ATTRIBUTE_DEVICE
0x40 |
予約済み。使用しないでください。 |
|
FILE_ATTRIBUTE_DIRECTORY
0x10 |
ファイルをディレクトリに変換することはできません。ディレクトリを作成するには、 CreateDirectory 関数または CreateDirectoryEx 関数を使用します。 |
|
FILE_ATTRIBUTE_ENCRYPTED
0x4000 |
暗号化されたファイルを作成するには、FILE_ATTRIBUTE_ENCRYPTED 属性とともに CreateFile 関数を使用します。既存のファイルを暗号化されたファイルに変換するには、EncryptFile 関数を使用します。 |
|
FILE_ATTRIBUTE_REPARSE_POINT
0x400 |
再解析ポイントをファイルまたはディレクトリに関連付けるには、FSCTL_SET_REPARSE_POINT 操作とともに DeviceIoControl 関数を使用します。 |
|
FILE_ATTRIBUTE_SPARSE_FILE
0x200 |
ファイルのスパース属性を設定するには、FSCTL_SET_SPARSE 操作とともに DeviceIoControl 関数を使用します。 |
ファイルがトランザクション内で変更のために開かれている場合、トランザクションがコミットされるまで、他のスレッドはそのファイルを変更のために開くことができません。トランザクションスレッドが最初にファイルを開いた場合、トランザクションがコミットされる前にファイルを変更のために開こうとする後続のスレッドは、共有違反を受け取ります。非トランザクションスレッドがトランザクションスレッドより先にファイルを変更のために開き、トランザクションスレッドが開こうとした時点でまだ開いている場合、トランザクションは ERROR_TRANSACTIONAL_CONFLICT エラーを受け取ります。
トランザクションの詳細については、 Transactional NTFS を参照してください。
Windows 8 および Windows Server 2012 では、この関数は次のテクノロジによってサポートされます。
| テクノロジ | サポート |
|---|---|
| Server Message Block (SMB) 3.0 プロトコル | いいえ |
| SMB 3.0 Transparent Failover (TFO) | いいえ |
| SMB 3.0 with Scale-out File Shares (SO) | いいえ |
| Cluster Shared Volume File System (CsvFS) | いいえ |
| Resilient File System (ReFS) | いいえ |
SMB 3.0 は TxF をサポートしていません。
トランザクション操作
ファイルがトランザクション内で変更のために開かれている場合、トランザクションがコミットされるまで、他のスレッドはそのファイルを変更のために開くことができません。そのため、トランザクションスレッドが最初にファイルを開いた場合、トランザクションがコミットされる前にファイルを変更しようとする後続のスレッドは、共有違反を受け取ります。非トランザクションスレッドがトランザクションスレッドより先にファイルを変更し、トランザクションが開こうとした時点でファイルがまだ開いている場合、トランザクションは ERROR_TRANSACTIONAL_CONFLICT エラーを受け取ります。winbase.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして SetFileAttributesTransacted を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不整合が生じ、コンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL SetFileAttributesTransactedW(
LPCWSTR lpFileName,
DWORD dwFileAttributes,
HANDLE hTransaction
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetFileAttributesTransactedW(
[MarshalAs(UnmanagedType.LPWStr)] string lpFileName, // LPCWSTR
uint dwFileAttributes, // DWORD
IntPtr hTransaction // HANDLE
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetFileAttributesTransactedW(
<MarshalAs(UnmanagedType.LPWStr)> lpFileName As String, ' LPCWSTR
dwFileAttributes As UInteger, ' DWORD
hTransaction As IntPtr ' HANDLE
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpFileName : LPCWSTR
' dwFileAttributes : DWORD
' hTransaction : HANDLE
Declare PtrSafe Function SetFileAttributesTransactedW Lib "kernel32" ( _
ByVal lpFileName As LongPtr, _
ByVal dwFileAttributes As Long, _
ByVal hTransaction As LongPtr) 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
SetFileAttributesTransactedW = ctypes.windll.kernel32.SetFileAttributesTransactedW
SetFileAttributesTransactedW.restype = wintypes.BOOL
SetFileAttributesTransactedW.argtypes = [
wintypes.LPCWSTR, # lpFileName : LPCWSTR
wintypes.DWORD, # dwFileAttributes : DWORD
wintypes.HANDLE, # hTransaction : HANDLE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
SetFileAttributesTransactedW = Fiddle::Function.new(
lib['SetFileAttributesTransactedW'],
[
Fiddle::TYPE_VOIDP, # lpFileName : LPCWSTR
-Fiddle::TYPE_INT, # dwFileAttributes : DWORD
Fiddle::TYPE_VOIDP, # hTransaction : HANDLE
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn SetFileAttributesTransactedW(
lpFileName: *const u16, // LPCWSTR
dwFileAttributes: u32, // DWORD
hTransaction: *mut core::ffi::c_void // HANDLE
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetFileAttributesTransactedW([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, uint dwFileAttributes, IntPtr hTransaction);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetFileAttributesTransactedW' -Namespace Win32 -PassThru
# $api::SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)#uselib "KERNEL32.dll"
#func global SetFileAttributesTransactedW "SetFileAttributesTransactedW" wptr, wptr, wptr
; SetFileAttributesTransactedW lpFileName, dwFileAttributes, hTransaction ; 戻り値は stat
; lpFileName : LPCWSTR -> "wptr"
; dwFileAttributes : DWORD -> "wptr"
; hTransaction : HANDLE -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global SetFileAttributesTransactedW "SetFileAttributesTransactedW" wstr, int, sptr
; res = SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)
; lpFileName : LPCWSTR -> "wstr"
; dwFileAttributes : DWORD -> "int"
; hTransaction : HANDLE -> "sptr"; BOOL SetFileAttributesTransactedW(LPCWSTR lpFileName, DWORD dwFileAttributes, HANDLE hTransaction)
#uselib "KERNEL32.dll"
#cfunc global SetFileAttributesTransactedW "SetFileAttributesTransactedW" wstr, int, intptr
; res = SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)
; lpFileName : LPCWSTR -> "wstr"
; dwFileAttributes : DWORD -> "int"
; hTransaction : HANDLE -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procSetFileAttributesTransactedW = kernel32.NewProc("SetFileAttributesTransactedW")
)
// lpFileName (LPCWSTR), dwFileAttributes (DWORD), hTransaction (HANDLE)
r1, _, err := procSetFileAttributesTransactedW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFileName))),
uintptr(dwFileAttributes),
uintptr(hTransaction),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetFileAttributesTransactedW(
lpFileName: PWideChar; // LPCWSTR
dwFileAttributes: DWORD; // DWORD
hTransaction: THandle // HANDLE
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetFileAttributesTransactedW';result := DllCall("KERNEL32\SetFileAttributesTransactedW"
, "WStr", lpFileName ; LPCWSTR
, "UInt", dwFileAttributes ; DWORD
, "Ptr", hTransaction ; HANDLE
, "Int") ; return: BOOL●SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction) = DLL("KERNEL32.dll", "bool SetFileAttributesTransactedW(char*, dword, void*)")
# 呼び出し: SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)
# lpFileName : LPCWSTR -> "char*"
# dwFileAttributes : DWORD -> "dword"
# hTransaction : HANDLE -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn SetFileAttributesTransactedW(
lpFileName: [*c]const u16, // LPCWSTR
dwFileAttributes: u32, // DWORD
hTransaction: ?*anyopaque // HANDLE
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc SetFileAttributesTransactedW(
lpFileName: WideCString, # LPCWSTR
dwFileAttributes: uint32, # DWORD
hTransaction: pointer # HANDLE
): int32 {.importc: "SetFileAttributesTransactedW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
int SetFileAttributesTransactedW(
const(wchar)* lpFileName, // LPCWSTR
uint dwFileAttributes, // DWORD
void* hTransaction // HANDLE
);ccall((:SetFileAttributesTransactedW, "KERNEL32.dll"), stdcall, Int32,
(Cwstring, UInt32, Ptr{Cvoid}),
lpFileName, dwFileAttributes, hTransaction)
# lpFileName : LPCWSTR -> Cwstring
# dwFileAttributes : DWORD -> UInt32
# hTransaction : HANDLE -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t SetFileAttributesTransactedW(
const uint16_t* lpFileName,
uint32_t dwFileAttributes,
void* hTransaction);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)
-- lpFileName : LPCWSTR
-- dwFileAttributes : DWORD
-- hTransaction : HANDLE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetFileAttributesTransactedW = lib.func('__stdcall', 'SetFileAttributesTransactedW', 'int32_t', ['str16', 'uint32_t', 'void *']);
// SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)
// lpFileName : LPCWSTR -> 'str16'
// dwFileAttributes : DWORD -> 'uint32_t'
// hTransaction : HANDLE -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
SetFileAttributesTransactedW: { parameters: ["buffer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction)
// lpFileName : LPCWSTR -> "buffer"
// dwFileAttributes : DWORD -> "u32"
// hTransaction : HANDLE -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetFileAttributesTransactedW(
const uint16_t* lpFileName,
uint32_t dwFileAttributes,
void* hTransaction);
C, "KERNEL32.dll");
// $ffi->SetFileAttributesTransactedW(lpFileName, dwFileAttributes, hTransaction);
// lpFileName : LPCWSTR
// dwFileAttributes : DWORD
// hTransaction : HANDLE
// 構造体/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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
boolean SetFileAttributesTransactedW(
WString lpFileName, // LPCWSTR
int dwFileAttributes, // DWORD
Pointer hTransaction // HANDLE
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun SetFileAttributesTransactedW = SetFileAttributesTransactedW(
lpFileName : UInt16*, # LPCWSTR
dwFileAttributes : UInt32, # DWORD
hTransaction : Void* # HANDLE
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetFileAttributesTransactedWNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Void>);
typedef SetFileAttributesTransactedWDart = int Function(Pointer<Utf16>, int, Pointer<Void>);
final SetFileAttributesTransactedW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<SetFileAttributesTransactedWNative, SetFileAttributesTransactedWDart>('SetFileAttributesTransactedW');
// lpFileName : LPCWSTR -> Pointer<Utf16>
// dwFileAttributes : DWORD -> Uint32
// hTransaction : HANDLE -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetFileAttributesTransactedW(
lpFileName: PWideChar; // LPCWSTR
dwFileAttributes: DWORD; // DWORD
hTransaction: THandle // HANDLE
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetFileAttributesTransactedW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetFileAttributesTransactedW"
c_SetFileAttributesTransactedW :: CWString -> Word32 -> Ptr () -> IO CInt
-- lpFileName : LPCWSTR -> CWString
-- dwFileAttributes : DWORD -> Word32
-- hTransaction : HANDLE -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setfileattributestransactedw =
foreign "SetFileAttributesTransactedW"
((ptr uint16_t) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* dwFileAttributes : DWORD -> uint32_t *)
(* hTransaction : HANDLE -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("SetFileAttributesTransactedW" set-file-attributes-transacted-w :convention :stdcall) :int32
(lp-file-name (:string :encoding :utf-16le)) ; LPCWSTR
(dw-file-attributes :uint32) ; DWORD
(h-transaction :pointer)) ; HANDLE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetFileAttributesTransactedW = Win32::API::More->new('KERNEL32',
'BOOL SetFileAttributesTransactedW(LPCWSTR lpFileName, DWORD dwFileAttributes, HANDLE hTransaction)');
# my $ret = $SetFileAttributesTransactedW->Call($lpFileName, $dwFileAttributes, $hTransaction);
# lpFileName : LPCWSTR -> LPCWSTR
# dwFileAttributes : DWORD -> DWORD
# hTransaction : HANDLE -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f SetFileAttributesTransactedA (ANSI版) — トランザクション内でファイル属性を設定する(ANSI版)。
- f GetFileAttributesTransactedW — トランザクション内でファイル属性情報を取得する(Unicode版)。