GetFileAttributesA
関数シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
DWORD GetFileAttributesA(
LPCSTR lpFileName
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFileName | LPCSTR | in | ファイルまたはディレクトリの名前。 既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳細については、ファイル、パス、および名前空間の名前付けを参照してください。 ヒント
Windows 10 バージョン 1607 以降では、"\\?\" を付加することなく MAX_PATH の制限を解除するオプトインが可能です。詳細については、ファイル、パス、および名前空間の名前付け の「最大パス長の制限」セクションを参照してください。 |
戻り値の型: DWORD
公式ドキュメント
指定したファイルまたはディレクトリのファイルシステム属性を取得します。(ANSI)
戻り値
関数が成功した場合、戻り値には指定したファイルまたはディレクトリの属性が格納されます。属性値とその説明の一覧については、 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 透過的フェールオーバー (TFO) | はい |
| SMB 3.0 スケールアウトファイル共有 (SO) | はい |
| クラスター共有ボリューム ファイル システム (CsvFS) | はい |
| 回復性ファイル システム (ReFS) | はい |
シンボリックリンクの動作 — パスがシンボリックリンクを指している場合、この関数はそのシンボリックリンクの属性を返します。
トランザクション処理操作
ファイルがトランザクション内で変更のために開かれている場合、そのトランザクションがコミットされるまで、他のスレッドはそのファイルを変更のために開くことができません。したがって、トランザクション化されたスレッドが最初にファイルを開いた場合、トランザクションがコミットされる前にファイルを変更しようとする後続のスレッドは共有違反を受け取ります。トランザクション化されていないスレッドがトランザクション化されたスレッドよりも先にファイルを変更し、トランザクションがファイルを開こうとした時点でそのファイルがまだ開かれている場合、トランザクションは ERROR_TRANSACTIONAL_CONFLICT エラーを受け取ります。例
例については、Retrieving and Changing File Attributes を参照してください。
fileapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetFileAttributes を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーまたは実行時エラーを引き起こす不一致につながる可能性があります。詳細については、関数プロトタイプの規則を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
DWORD GetFileAttributesA(
LPCSTR lpFileName
);[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern uint GetFileAttributesA(
[MarshalAs(UnmanagedType.LPStr)] string lpFileName // LPCSTR
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetFileAttributesA(
<MarshalAs(UnmanagedType.LPStr)> lpFileName As String ' LPCSTR
) As UInteger
End Function' lpFileName : LPCSTR
Declare PtrSafe Function GetFileAttributesA Lib "kernel32" ( _
ByVal lpFileName As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetFileAttributesA = ctypes.windll.kernel32.GetFileAttributesA
GetFileAttributesA.restype = wintypes.DWORD
GetFileAttributesA.argtypes = [
wintypes.LPCSTR, # lpFileName : LPCSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetFileAttributesA = Fiddle::Function.new(
lib['GetFileAttributesA'],
[
Fiddle::TYPE_VOIDP, # lpFileName : LPCSTR
],
-Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn GetFileAttributesA(
lpFileName: *const u8 // LPCSTR
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern uint GetFileAttributesA([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetFileAttributesA' -Namespace Win32 -PassThru
# $api::GetFileAttributesA(lpFileName)#uselib "KERNEL32.dll"
#func global GetFileAttributesA "GetFileAttributesA" sptr
; GetFileAttributesA lpFileName ; 戻り値は stat
; lpFileName : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global GetFileAttributesA "GetFileAttributesA" str
; res = GetFileAttributesA(lpFileName)
; lpFileName : LPCSTR -> "str"; DWORD GetFileAttributesA(LPCSTR lpFileName)
#uselib "KERNEL32.dll"
#cfunc global GetFileAttributesA "GetFileAttributesA" str
; res = GetFileAttributesA(lpFileName)
; lpFileName : LPCSTR -> "str"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetFileAttributesA = kernel32.NewProc("GetFileAttributesA")
)
// lpFileName (LPCSTR)
r1, _, err := procGetFileAttributesA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFileName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction GetFileAttributesA(
lpFileName: PAnsiChar // LPCSTR
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetFileAttributesA';result := DllCall("KERNEL32\GetFileAttributesA"
, "AStr", lpFileName ; LPCSTR
, "UInt") ; return: DWORD●GetFileAttributesA(lpFileName) = DLL("KERNEL32.dll", "dword GetFileAttributesA(char*)")
# 呼び出し: GetFileAttributesA(lpFileName)
# lpFileName : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn GetFileAttributesA(
lpFileName: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) u32;proc GetFileAttributesA(
lpFileName: cstring # LPCSTR
): uint32 {.importc: "GetFileAttributesA", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
uint GetFileAttributesA(
const(char)* lpFileName // LPCSTR
);ccall((:GetFileAttributesA, "KERNEL32.dll"), stdcall, UInt32,
(Cstring,),
lpFileName)
# lpFileName : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t GetFileAttributesA(
const char* lpFileName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetFileAttributesA(lpFileName)
-- lpFileName : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetFileAttributesA = lib.func('__stdcall', 'GetFileAttributesA', 'uint32_t', ['str']);
// GetFileAttributesA(lpFileName)
// lpFileName : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
GetFileAttributesA: { parameters: ["buffer"], result: "u32" },
});
// lib.symbols.GetFileAttributesA(lpFileName)
// lpFileName : LPCSTR -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t GetFileAttributesA(
const char* lpFileName);
C, "KERNEL32.dll");
// $ffi->GetFileAttributesA(lpFileName);
// lpFileName : LPCSTR
// 構造体/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.ASCII_OPTIONS);
int GetFileAttributesA(
String lpFileName // LPCSTR
);
}@[Link("kernel32")]
lib LibKERNEL32
fun GetFileAttributesA = GetFileAttributesA(
lpFileName : UInt8* # LPCSTR
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetFileAttributesANative = Uint32 Function(Pointer<Utf8>);
typedef GetFileAttributesADart = int Function(Pointer<Utf8>);
final GetFileAttributesA = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<GetFileAttributesANative, GetFileAttributesADart>('GetFileAttributesA');
// lpFileName : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetFileAttributesA(
lpFileName: PAnsiChar // LPCSTR
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetFileAttributesA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetFileAttributesA"
c_GetFileAttributesA :: CString -> IO Word32
-- lpFileName : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getfileattributesa =
foreign "GetFileAttributesA"
(string @-> returning uint32_t)
(* lpFileName : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("GetFileAttributesA" get-file-attributes-a :convention :stdcall) :uint32
(lp-file-name :string)) ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetFileAttributesA = Win32::API::More->new('KERNEL32',
'DWORD GetFileAttributesA(LPCSTR lpFileName)');
# my $ret = $GetFileAttributesA->Call($lpFileName);
# lpFileName : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f GetFileAttributesW (Unicode版) — 指定ファイルの属性を取得する。
- f GetFileAttributesExA — 指定ファイルの属性や時刻などの拡張情報を取得する(ANSI版)。
- f DeviceIoControl — デバイスドライバに制御コードを送って操作する。
- f FindFirstFileA — 条件に一致する最初のファイルを検索する(ANSI版)。
- f FindNextFileA — 前回の検索条件で次のファイルを取得する(ANSI版)。
- f GetFileAttributesTransactedA — トランザクション内でファイル属性情報を取得する(ANSI版)。
- f SetFileAttributesA — 指定ファイルの属性を設定する(ANSI版)。