GetFileAttributesW
関数シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
DWORD GetFileAttributesW(
LPCWSTR lpFileName
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFileName | LPCWSTR | in | ファイルまたはディレクトリの名前。 既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳細については、Naming Files, Paths, and Namespaces を参照してください。 ヒント
Windows 10 バージョン 1607 以降では、"\\?\" を付加しなくても MAX_PATH の制限を解除するオプトインが可能です。詳細については、Naming Files, Paths, and Namespaces の「Maximum Path Length Limitation」セクションを参照してください。 |
戻り値の型: DWORD
公式ドキュメント
指定されたファイルまたはディレクトリのファイルシステム属性を取得します。(Unicode)
戻り値
関数が成功した場合、戻り値には指定されたファイルまたはディレクトリの属性が格納されます。属性値の一覧とその説明については、 File Attribute Constants を参照してください。
関数が失敗した場合、戻り値は INVALID_FILE_ATTRIBUTES です。拡張エラー情報を取得するには、GetLastError を呼び出します。
解説(Remarks)
GetFileAttributes をマウントされたフォルダーであるディレクトリに対して呼び出すと、そのディレクトリのファイルシステム属性を返し、そのマウントされたフォルダーがディレクトリに関連付けているボリューム内のルートディレクトリの属性は返しません。関連付けられたボリュームのファイル属性を取得するには、 GetVolumeNameForVolumeMountPoint を呼び出して、関連付けられたボリュームの名前を取得します。次に、得られた名前を GetFileAttributes の呼び出しで使用します。その結果は、関連付けられたボリューム上のルートディレクトリの属性になります。
ネットワーク共有に対して GetFileAttributes を呼び出すと、関数は失敗し、GetLastError は ERROR_BAD_NETPATH を返します。その共有上のサブフォルダーへのパスを指定する必要があります。
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) | はい |
シンボリックリンクの動作—パスがシンボリックリンクを指している場合、関数はそのシンボリックリンクの属性を返します。
トランザクション操作
ファイルがトランザクション内で変更のために開かれている場合、トランザクションがコミットされるまで、他のスレッドはそのファイルを変更のために開くことができません。したがって、トランザクション処理されたスレッドが最初にファイルを開いた場合、トランザクションがコミットされる前にファイルを変更しようとする後続のスレッドは共有違反を受け取ります。トランザクション処理されていないスレッドがトランザクション処理されたスレッドより先にファイルを変更し、トランザクションがファイルを開こうとした時点でそのファイルがまだ開かれている場合、トランザクションは ERROR_TRANSACTIONAL_CONFLICT エラーを受け取ります。例
例については、 Retrieving and Changing File Attributes を参照してください。
fileapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetFileAttributes を定義します。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
DWORD GetFileAttributesW(
LPCWSTR lpFileName
);[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetFileAttributesW(
[MarshalAs(UnmanagedType.LPWStr)] string lpFileName // LPCWSTR
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetFileAttributesW(
<MarshalAs(UnmanagedType.LPWStr)> lpFileName As String ' LPCWSTR
) As UInteger
End Function' lpFileName : LPCWSTR
Declare PtrSafe Function GetFileAttributesW Lib "kernel32" ( _
ByVal lpFileName 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
GetFileAttributesW = ctypes.windll.kernel32.GetFileAttributesW
GetFileAttributesW.restype = wintypes.DWORD
GetFileAttributesW.argtypes = [
wintypes.LPCWSTR, # lpFileName : LPCWSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetFileAttributesW = Fiddle::Function.new(
lib['GetFileAttributesW'],
[
Fiddle::TYPE_VOIDP, # lpFileName : LPCWSTR
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn GetFileAttributesW(
lpFileName: *const u16 // LPCWSTR
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetFileAttributesW([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetFileAttributesW' -Namespace Win32 -PassThru
# $api::GetFileAttributesW(lpFileName)#uselib "KERNEL32.dll"
#func global GetFileAttributesW "GetFileAttributesW" wptr
; GetFileAttributesW lpFileName ; 戻り値は stat
; lpFileName : LPCWSTR -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global GetFileAttributesW "GetFileAttributesW" wstr
; res = GetFileAttributesW(lpFileName)
; lpFileName : LPCWSTR -> "wstr"; DWORD GetFileAttributesW(LPCWSTR lpFileName)
#uselib "KERNEL32.dll"
#cfunc global GetFileAttributesW "GetFileAttributesW" wstr
; res = GetFileAttributesW(lpFileName)
; lpFileName : LPCWSTR -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetFileAttributesW = kernel32.NewProc("GetFileAttributesW")
)
// lpFileName (LPCWSTR)
r1, _, err := procGetFileAttributesW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFileName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction GetFileAttributesW(
lpFileName: PWideChar // LPCWSTR
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetFileAttributesW';result := DllCall("KERNEL32\GetFileAttributesW"
, "WStr", lpFileName ; LPCWSTR
, "UInt") ; return: DWORD●GetFileAttributesW(lpFileName) = DLL("KERNEL32.dll", "dword GetFileAttributesW(char*)")
# 呼び出し: GetFileAttributesW(lpFileName)
# lpFileName : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn GetFileAttributesW(
lpFileName: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc GetFileAttributesW(
lpFileName: WideCString # LPCWSTR
): uint32 {.importc: "GetFileAttributesW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
uint GetFileAttributesW(
const(wchar)* lpFileName // LPCWSTR
);ccall((:GetFileAttributesW, "KERNEL32.dll"), stdcall, UInt32,
(Cwstring,),
lpFileName)
# lpFileName : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t GetFileAttributesW(
const uint16_t* lpFileName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetFileAttributesW(lpFileName)
-- lpFileName : LPCWSTR
-- 構造体/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 GetFileAttributesW = lib.func('__stdcall', 'GetFileAttributesW', 'uint32_t', ['str16']);
// GetFileAttributesW(lpFileName)
// lpFileName : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
GetFileAttributesW: { parameters: ["buffer"], result: "u32" },
});
// lib.symbols.GetFileAttributesW(lpFileName)
// lpFileName : LPCWSTR -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t GetFileAttributesW(
const uint16_t* lpFileName);
C, "KERNEL32.dll");
// $ffi->GetFileAttributesW(lpFileName);
// lpFileName : LPCWSTR
// 構造体/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);
int GetFileAttributesW(
WString lpFileName // LPCWSTR
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun GetFileAttributesW = GetFileAttributesW(
lpFileName : UInt16* # LPCWSTR
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetFileAttributesWNative = Uint32 Function(Pointer<Utf16>);
typedef GetFileAttributesWDart = int Function(Pointer<Utf16>);
final GetFileAttributesW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<GetFileAttributesWNative, GetFileAttributesWDart>('GetFileAttributesW');
// lpFileName : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetFileAttributesW(
lpFileName: PWideChar // LPCWSTR
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetFileAttributesW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetFileAttributesW"
c_GetFileAttributesW :: CWString -> IO Word32
-- lpFileName : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getfileattributesw =
foreign "GetFileAttributesW"
((ptr uint16_t) @-> returning uint32_t)
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("GetFileAttributesW" get-file-attributes-w :convention :stdcall) :uint32
(lp-file-name (:string :encoding :utf-16le))) ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetFileAttributesW = Win32::API::More->new('KERNEL32',
'DWORD GetFileAttributesW(LPCWSTR lpFileName)');
# my $ret = $GetFileAttributesW->Call($lpFileName);
# lpFileName : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f GetFileAttributesA (ANSI版) — 指定ファイルの属性を取得する(ANSI版)。
- f GetFileAttributesExW — 指定ファイルの属性や時刻などの拡張情報を取得する。
- f DeviceIoControl — デバイスドライバに制御コードを送って操作する。
- f FindFirstFileW — 条件に一致する最初のファイルを検索する。
- f FindNextFileW — 前回の検索条件で次のファイルを取得する。
- f GetFileAttributesTransactedW — トランザクション内でファイル属性情報を取得する(Unicode版)。
- f SetFileAttributesW — 指定ファイルの属性を設定する。