Win32 API 日本語リファレンス
ホームStorage.FileSystem › GetFileAttributesExW

GetFileAttributesExW

関数
指定ファイルの属性や時刻などの拡張情報を取得する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL GetFileAttributesExW(
    LPCWSTR lpFileName,
    GET_FILEEX_INFO_LEVELS fInfoLevelId,
    void* lpFileInformation
);

パラメーター

名前方向説明
lpFileNameLPCWSTRin

ファイルまたはディレクトリの名前です。

既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字に拡張するには、パスの先頭に "\\?\" を付加します。詳細については、ファイル、パス、および名前空間の名前付けを参照してください。

ヒント

Windows 10 バージョン 1607 以降では、"\\?\" を付加せずに MAX_PATH の制限を解除するようにオプトインできます。詳細については、ファイル、パス、および名前空間の名前付けの「最大パス長の制限」セクションを参照してください。

fInfoLevelIdGET_FILEEX_INFO_LEVELSin

取得する属性情報のクラスです。

このパラメーターには、 GET_FILEEX_INFO_LEVELS 列挙体の次の値を指定できます。

意味
GetFileExInfoStandard
lpFileInformation パラメーターは WIN32_FILE_ATTRIBUTE_DATA 構造体です。
lpFileInformationvoid*out

属性情報を受け取るバッファーへのポインターです。

このバッファーに格納される属性情報の種類は、 fInfoLevelId の値によって決まります。

戻り値の型: BOOL

公式ドキュメント

指定されたファイルまたはディレクトリの属性を取得します。(Unicode)

戻り値

関数が成功すると、戻り値は 0 以外の値になります。

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

解説(Remarks)

GetFileAttributes 関数は、ファイル システム属性情報を取得します。GetFileAttributesEx は、他の組み合わせのファイルまたはディレクトリの属性情報を取得できます。現在のところ、 GetFileAttributesEx は、ファイル システム属性情報のスーパーセットである標準 属性の組み合わせを取得します。

GetFileAttributesEx 関数を、 マウントされたフォルダーであるディレクトリに対して呼び出した場合、そのディレクトリの属性が返されます。マウントされたフォルダーがディレクトリに関連付けているボリューム内のルート ディレクトリの属性は返されません。関連付けられたボリュームの属性を取得するには、 GetVolumeNameForVolumeMountPoint を呼び出して 関連付けられたボリュームの名前を取得します。次に、その結果として得られた名前を使用して GetFileAttributesEx を呼び出します。その結果は、関連付けられたボリューム上のルート ディレクトリの属性 になります。

Windows 8 および Windows Server 2012 では、この関数は次のテクノロジでサポートされています。

テクノロジ サポート
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 トランスペアレント フェールオーバー (TFO) はい
SMB 3.0 スケールアウト ファイル共有 (SO) はい
クラスター共有ボリューム ファイル システム (CsvFS) はい
回復性ファイル システム (ReFS) はい

シンボリック リンクの動作—パスがシンボリック リンクを指している場合、この関数は そのシンボリック リンクの属性を返します。

トランザクション操作

ファイルがトランザクション内で変更のために開かれている場合、そのトランザクションがコミットされるまで、他のスレッドはそのファイルを変更のために開くことができません。したがって、トランザクション スレッドが最初にファイルを開いた場合、トランザクションがコミットされる前にそのファイルを 変更しようとする後続のスレッドは、共有違反を受け取ります。トランザクション スレッドがファイルを変更する前に、非トランザクション スレッドが そのファイルを変更し、トランザクションがファイルを開こうとした時点でそのファイルがまだ開かれている場合、 トランザクションは ERROR_TRANSACTIONAL_CONFLICT エラーを受け取ります。
メモ

fileapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetFileAttributesEx を定義しています。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードを混在させると、コンパイル エラーまたは実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、関数プロトタイプの規則を参照してください。

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

各言語での呼び出し定義

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

BOOL GetFileAttributesExW(
    LPCWSTR lpFileName,
    GET_FILEEX_INFO_LEVELS fInfoLevelId,
    void* lpFileInformation
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool GetFileAttributesExW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,   // LPCWSTR
    int fInfoLevelId,   // GET_FILEEX_INFO_LEVELS
    IntPtr lpFileInformation   // void* out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetFileAttributesExW(
    <MarshalAs(UnmanagedType.LPWStr)> lpFileName As String,   ' LPCWSTR
    fInfoLevelId As Integer,   ' GET_FILEEX_INFO_LEVELS
    lpFileInformation As IntPtr   ' void* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpFileName : LPCWSTR
' fInfoLevelId : GET_FILEEX_INFO_LEVELS
' lpFileInformation : void* out
Declare PtrSafe Function GetFileAttributesExW Lib "kernel32" ( _
    ByVal lpFileName As LongPtr, _
    ByVal fInfoLevelId As Long, _
    ByVal lpFileInformation 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

GetFileAttributesExW = ctypes.windll.kernel32.GetFileAttributesExW
GetFileAttributesExW.restype = wintypes.BOOL
GetFileAttributesExW.argtypes = [
    wintypes.LPCWSTR,  # lpFileName : LPCWSTR
    ctypes.c_int,  # fInfoLevelId : GET_FILEEX_INFO_LEVELS
    ctypes.POINTER(None),  # lpFileInformation : void* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetFileAttributesExW = Fiddle::Function.new(
  lib['GetFileAttributesExW'],
  [
    Fiddle::TYPE_VOIDP,  # lpFileName : LPCWSTR
    Fiddle::TYPE_INT,  # fInfoLevelId : GET_FILEEX_INFO_LEVELS
    Fiddle::TYPE_VOIDP,  # lpFileInformation : void* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn GetFileAttributesExW(
        lpFileName: *const u16,  // LPCWSTR
        fInfoLevelId: i32,  // GET_FILEEX_INFO_LEVELS
        lpFileInformation: *mut ()  // void* out
    ) -> 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 GetFileAttributesExW([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, int fInfoLevelId, IntPtr lpFileInformation);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetFileAttributesExW' -Namespace Win32 -PassThru
# $api::GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation)
#uselib "KERNEL32.dll"
#func global GetFileAttributesExW "GetFileAttributesExW" wptr, wptr, wptr
; GetFileAttributesExW lpFileName, fInfoLevelId, lpFileInformation   ; 戻り値は stat
; lpFileName : LPCWSTR -> "wptr"
; fInfoLevelId : GET_FILEEX_INFO_LEVELS -> "wptr"
; lpFileInformation : void* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global GetFileAttributesExW "GetFileAttributesExW" wstr, int, sptr
; res = GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation)
; lpFileName : LPCWSTR -> "wstr"
; fInfoLevelId : GET_FILEEX_INFO_LEVELS -> "int"
; lpFileInformation : void* out -> "sptr"
; BOOL GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, void* lpFileInformation)
#uselib "KERNEL32.dll"
#cfunc global GetFileAttributesExW "GetFileAttributesExW" wstr, int, intptr
; res = GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation)
; lpFileName : LPCWSTR -> "wstr"
; fInfoLevelId : GET_FILEEX_INFO_LEVELS -> "int"
; lpFileInformation : void* out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetFileAttributesExW = kernel32.NewProc("GetFileAttributesExW")
)

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

extern "kernel32" fn GetFileAttributesExW(
    lpFileName: [*c]const u16, // LPCWSTR
    fInfoLevelId: i32, // GET_FILEEX_INFO_LEVELS
    lpFileInformation: ?*anyopaque // void* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetFileAttributesExW(
    lpFileName: WideCString,  # LPCWSTR
    fInfoLevelId: int32,  # GET_FILEEX_INFO_LEVELS
    lpFileInformation: pointer  # void* out
): int32 {.importc: "GetFileAttributesExW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "kernel32");
extern(Windows)
int GetFileAttributesExW(
    const(wchar)* lpFileName,   // LPCWSTR
    int fInfoLevelId,   // GET_FILEEX_INFO_LEVELS
    void* lpFileInformation   // void* out
);
ccall((:GetFileAttributesExW, "KERNEL32.dll"), stdcall, Int32,
      (Cwstring, Int32, Ptr{Cvoid}),
      lpFileName, fInfoLevelId, lpFileInformation)
# lpFileName : LPCWSTR -> Cwstring
# fInfoLevelId : GET_FILEEX_INFO_LEVELS -> Int32
# lpFileInformation : void* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetFileAttributesExW(
    const uint16_t* lpFileName,
    int32_t fInfoLevelId,
    void* lpFileInformation);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation)
-- lpFileName : LPCWSTR
-- fInfoLevelId : GET_FILEEX_INFO_LEVELS
-- lpFileInformation : void* 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('KERNEL32.dll');
const GetFileAttributesExW = lib.func('__stdcall', 'GetFileAttributesExW', 'int32_t', ['str16', 'int32_t', 'void *']);
// GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation)
// lpFileName : LPCWSTR -> 'str16'
// fInfoLevelId : GET_FILEEX_INFO_LEVELS -> 'int32_t'
// lpFileInformation : void* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetFileAttributesExW: { parameters: ["buffer", "i32", "pointer"], result: "i32" },
});
// lib.symbols.GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation)
// lpFileName : LPCWSTR -> "buffer"
// fInfoLevelId : GET_FILEEX_INFO_LEVELS -> "i32"
// lpFileInformation : void* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetFileAttributesExW(
    const uint16_t* lpFileName,
    int32_t fInfoLevelId,
    void* lpFileInformation);
C, "KERNEL32.dll");
// $ffi->GetFileAttributesExW(lpFileName, fInfoLevelId, lpFileInformation);
// lpFileName : LPCWSTR
// fInfoLevelId : GET_FILEEX_INFO_LEVELS
// lpFileInformation : void* 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 Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetFileAttributesExW(
        WString lpFileName,   // LPCWSTR
        int fInfoLevelId,   // GET_FILEEX_INFO_LEVELS
        Pointer lpFileInformation   // void* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("kernel32")]
lib LibKERNEL32
  fun GetFileAttributesExW = GetFileAttributesExW(
    lpFileName : UInt16*,   # LPCWSTR
    fInfoLevelId : Int32,   # GET_FILEEX_INFO_LEVELS
    lpFileInformation : Void*   # void* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetFileAttributesExWNative = Int32 Function(Pointer<Utf16>, Int32, Pointer<Void>);
typedef GetFileAttributesExWDart = int Function(Pointer<Utf16>, int, Pointer<Void>);
final GetFileAttributesExW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetFileAttributesExWNative, GetFileAttributesExWDart>('GetFileAttributesExW');
// lpFileName : LPCWSTR -> Pointer<Utf16>
// fInfoLevelId : GET_FILEEX_INFO_LEVELS -> Int32
// lpFileInformation : void* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetFileAttributesExW(
  lpFileName: PWideChar;   // LPCWSTR
  fInfoLevelId: Integer;   // GET_FILEEX_INFO_LEVELS
  lpFileInformation: Pointer   // void* out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetFileAttributesExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetFileAttributesExW"
  c_GetFileAttributesExW :: CWString -> Int32 -> Ptr () -> IO CInt
-- lpFileName : LPCWSTR -> CWString
-- fInfoLevelId : GET_FILEEX_INFO_LEVELS -> Int32
-- lpFileInformation : void* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getfileattributesexw =
  foreign "GetFileAttributesExW"
    ((ptr uint16_t) @-> int32_t @-> (ptr void) @-> returning int32_t)
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* fInfoLevelId : GET_FILEEX_INFO_LEVELS -> int32_t *)
(* lpFileInformation : void* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetFileAttributesExW" get-file-attributes-ex-w :convention :stdcall) :int32
  (lp-file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (f-info-level-id :int32)   ; GET_FILEEX_INFO_LEVELS
  (lp-file-information :pointer))   ; void* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetFileAttributesExW = Win32::API::More->new('KERNEL32',
    'BOOL GetFileAttributesExW(LPCWSTR lpFileName, int fInfoLevelId, LPVOID lpFileInformation)');
# my $ret = $GetFileAttributesExW->Call($lpFileName, $fInfoLevelId, $lpFileInformation);
# lpFileName : LPCWSTR -> LPCWSTR
# fInfoLevelId : GET_FILEEX_INFO_LEVELS -> int
# lpFileInformation : void* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
公式の関連項目
使用する型