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

GetFileInformationByName

関数
ファイル名から指定クラスのファイル情報を取得する。
DLLKERNEL32.dll呼出規約winapi

シグネチャ

// KERNEL32.dll
#include <windows.h>

BOOL GetFileInformationByName(
    LPCWSTR FileName,
    FILE_INFO_BY_NAME_CLASS FileInformationClass,
    void* FileInfoBuffer,
    DWORD FileInfoBufferSize
);

パラメーター

名前方向説明
FileNameLPCWSTRin情報を取得する対象ファイルのUnicodeパスを指す。
FileInformationClassFILE_INFO_BY_NAME_CLASSin取得する情報種別を指定するFILE_INFO_BY_NAME_CLASS列挙値。
FileInfoBuffervoid*out要求した情報を受け取るバッファへのポインタ。
FileInfoBufferSizeDWORDinFileInfoBufferのサイズをバイト単位で指定する。

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

BOOL GetFileInformationByName(
    LPCWSTR FileName,
    FILE_INFO_BY_NAME_CLASS FileInformationClass,
    void* FileInfoBuffer,
    DWORD FileInfoBufferSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern bool GetFileInformationByName(
    [MarshalAs(UnmanagedType.LPWStr)] string FileName,   // LPCWSTR
    int FileInformationClass,   // FILE_INFO_BY_NAME_CLASS
    IntPtr FileInfoBuffer,   // void* out
    uint FileInfoBufferSize   // DWORD
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function GetFileInformationByName(
    <MarshalAs(UnmanagedType.LPWStr)> FileName As String,   ' LPCWSTR
    FileInformationClass As Integer,   ' FILE_INFO_BY_NAME_CLASS
    FileInfoBuffer As IntPtr,   ' void* out
    FileInfoBufferSize As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' FileName : LPCWSTR
' FileInformationClass : FILE_INFO_BY_NAME_CLASS
' FileInfoBuffer : void* out
' FileInfoBufferSize : DWORD
Declare PtrSafe Function GetFileInformationByName Lib "kernel32" ( _
    ByVal FileName As LongPtr, _
    ByVal FileInformationClass As Long, _
    ByVal FileInfoBuffer As LongPtr, _
    ByVal FileInfoBufferSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetFileInformationByName = ctypes.windll.kernel32.GetFileInformationByName
GetFileInformationByName.restype = wintypes.BOOL
GetFileInformationByName.argtypes = [
    wintypes.LPCWSTR,  # FileName : LPCWSTR
    ctypes.c_int,  # FileInformationClass : FILE_INFO_BY_NAME_CLASS
    ctypes.POINTER(None),  # FileInfoBuffer : void* out
    wintypes.DWORD,  # FileInfoBufferSize : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetFileInformationByName = Fiddle::Function.new(
  lib['GetFileInformationByName'],
  [
    Fiddle::TYPE_VOIDP,  # FileName : LPCWSTR
    Fiddle::TYPE_INT,  # FileInformationClass : FILE_INFO_BY_NAME_CLASS
    Fiddle::TYPE_VOIDP,  # FileInfoBuffer : void* out
    -Fiddle::TYPE_INT,  # FileInfoBufferSize : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn GetFileInformationByName(
        FileName: *const u16,  // LPCWSTR
        FileInformationClass: i32,  // FILE_INFO_BY_NAME_CLASS
        FileInfoBuffer: *mut (),  // void* out
        FileInfoBufferSize: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll")]
public static extern bool GetFileInformationByName([MarshalAs(UnmanagedType.LPWStr)] string FileName, int FileInformationClass, IntPtr FileInfoBuffer, uint FileInfoBufferSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetFileInformationByName' -Namespace Win32 -PassThru
# $api::GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
#uselib "KERNEL32.dll"
#func global GetFileInformationByName "GetFileInformationByName" sptr, sptr, sptr, sptr
; GetFileInformationByName FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize   ; 戻り値は stat
; FileName : LPCWSTR -> "sptr"
; FileInformationClass : FILE_INFO_BY_NAME_CLASS -> "sptr"
; FileInfoBuffer : void* out -> "sptr"
; FileInfoBufferSize : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global GetFileInformationByName "GetFileInformationByName" wstr, int, sptr, int
; res = GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
; FileName : LPCWSTR -> "wstr"
; FileInformationClass : FILE_INFO_BY_NAME_CLASS -> "int"
; FileInfoBuffer : void* out -> "sptr"
; FileInfoBufferSize : DWORD -> "int"
; BOOL GetFileInformationByName(LPCWSTR FileName, FILE_INFO_BY_NAME_CLASS FileInformationClass, void* FileInfoBuffer, DWORD FileInfoBufferSize)
#uselib "KERNEL32.dll"
#cfunc global GetFileInformationByName "GetFileInformationByName" wstr, int, intptr, int
; res = GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
; FileName : LPCWSTR -> "wstr"
; FileInformationClass : FILE_INFO_BY_NAME_CLASS -> "int"
; FileInfoBuffer : void* out -> "intptr"
; FileInfoBufferSize : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetFileInformationByName = kernel32.NewProc("GetFileInformationByName")
)

// FileName (LPCWSTR), FileInformationClass (FILE_INFO_BY_NAME_CLASS), FileInfoBuffer (void* out), FileInfoBufferSize (DWORD)
r1, _, err := procGetFileInformationByName.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(FileName))),
	uintptr(FileInformationClass),
	uintptr(FileInfoBuffer),
	uintptr(FileInfoBufferSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetFileInformationByName(
  FileName: PWideChar;   // LPCWSTR
  FileInformationClass: Integer;   // FILE_INFO_BY_NAME_CLASS
  FileInfoBuffer: Pointer;   // void* out
  FileInfoBufferSize: DWORD   // DWORD
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetFileInformationByName';
result := DllCall("KERNEL32\GetFileInformationByName"
    , "WStr", FileName   ; LPCWSTR
    , "Int", FileInformationClass   ; FILE_INFO_BY_NAME_CLASS
    , "Ptr", FileInfoBuffer   ; void* out
    , "UInt", FileInfoBufferSize   ; DWORD
    , "Int")   ; return: BOOL
●GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize) = DLL("KERNEL32.dll", "bool GetFileInformationByName(char*, int, void*, dword)")
# 呼び出し: GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
# FileName : LPCWSTR -> "char*"
# FileInformationClass : FILE_INFO_BY_NAME_CLASS -> "int"
# FileInfoBuffer : void* out -> "void*"
# FileInfoBufferSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn GetFileInformationByName(
    FileName: [*c]const u16, // LPCWSTR
    FileInformationClass: i32, // FILE_INFO_BY_NAME_CLASS
    FileInfoBuffer: ?*anyopaque, // void* out
    FileInfoBufferSize: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc GetFileInformationByName(
    FileName: WideCString,  # LPCWSTR
    FileInformationClass: int32,  # FILE_INFO_BY_NAME_CLASS
    FileInfoBuffer: pointer,  # void* out
    FileInfoBufferSize: uint32  # DWORD
): int32 {.importc: "GetFileInformationByName", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int GetFileInformationByName(
    const(wchar)* FileName,   // LPCWSTR
    int FileInformationClass,   // FILE_INFO_BY_NAME_CLASS
    void* FileInfoBuffer,   // void* out
    uint FileInfoBufferSize   // DWORD
);
ccall((:GetFileInformationByName, "KERNEL32.dll"), stdcall, Int32,
      (Cwstring, Int32, Ptr{Cvoid}, UInt32),
      FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
# FileName : LPCWSTR -> Cwstring
# FileInformationClass : FILE_INFO_BY_NAME_CLASS -> Int32
# FileInfoBuffer : void* out -> Ptr{Cvoid}
# FileInfoBufferSize : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetFileInformationByName(
    const uint16_t* FileName,
    int32_t FileInformationClass,
    void* FileInfoBuffer,
    uint32_t FileInfoBufferSize);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
-- FileName : LPCWSTR
-- FileInformationClass : FILE_INFO_BY_NAME_CLASS
-- FileInfoBuffer : void* out
-- FileInfoBufferSize : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetFileInformationByName = lib.func('__stdcall', 'GetFileInformationByName', 'int32_t', ['str16', 'int32_t', 'void *', 'uint32_t']);
// GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
// FileName : LPCWSTR -> 'str16'
// FileInformationClass : FILE_INFO_BY_NAME_CLASS -> 'int32_t'
// FileInfoBuffer : void* out -> 'void *'
// FileInfoBufferSize : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetFileInformationByName: { parameters: ["buffer", "i32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize)
// FileName : LPCWSTR -> "buffer"
// FileInformationClass : FILE_INFO_BY_NAME_CLASS -> "i32"
// FileInfoBuffer : void* out -> "pointer"
// FileInfoBufferSize : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetFileInformationByName(
    const uint16_t* FileName,
    int32_t FileInformationClass,
    void* FileInfoBuffer,
    uint32_t FileInfoBufferSize);
C, "KERNEL32.dll");
// $ffi->GetFileInformationByName(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize);
// FileName : LPCWSTR
// FileInformationClass : FILE_INFO_BY_NAME_CLASS
// FileInfoBuffer : void* out
// FileInfoBufferSize : DWORD
// 構造体/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);
    boolean GetFileInformationByName(
        WString FileName,   // LPCWSTR
        int FileInformationClass,   // FILE_INFO_BY_NAME_CLASS
        Pointer FileInfoBuffer,   // void* out
        int FileInfoBufferSize   // DWORD
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun GetFileInformationByName = GetFileInformationByName(
    FileName : UInt16*,   # LPCWSTR
    FileInformationClass : Int32,   # FILE_INFO_BY_NAME_CLASS
    FileInfoBuffer : Void*,   # void* out
    FileInfoBufferSize : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetFileInformationByNameNative = Int32 Function(Pointer<Utf16>, Int32, Pointer<Void>, Uint32);
typedef GetFileInformationByNameDart = int Function(Pointer<Utf16>, int, Pointer<Void>, int);
final GetFileInformationByName = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetFileInformationByNameNative, GetFileInformationByNameDart>('GetFileInformationByName');
// FileName : LPCWSTR -> Pointer<Utf16>
// FileInformationClass : FILE_INFO_BY_NAME_CLASS -> Int32
// FileInfoBuffer : void* out -> Pointer<Void>
// FileInfoBufferSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetFileInformationByName(
  FileName: PWideChar;   // LPCWSTR
  FileInformationClass: Integer;   // FILE_INFO_BY_NAME_CLASS
  FileInfoBuffer: Pointer;   // void* out
  FileInfoBufferSize: DWORD   // DWORD
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetFileInformationByName';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetFileInformationByName"
  c_GetFileInformationByName :: CWString -> Int32 -> Ptr () -> Word32 -> IO CInt
-- FileName : LPCWSTR -> CWString
-- FileInformationClass : FILE_INFO_BY_NAME_CLASS -> Int32
-- FileInfoBuffer : void* out -> Ptr ()
-- FileInfoBufferSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getfileinformationbyname =
  foreign "GetFileInformationByName"
    ((ptr uint16_t) @-> int32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* FileName : LPCWSTR -> (ptr uint16_t) *)
(* FileInformationClass : FILE_INFO_BY_NAME_CLASS -> int32_t *)
(* FileInfoBuffer : void* out -> (ptr void) *)
(* FileInfoBufferSize : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetFileInformationByName" get-file-information-by-name :convention :stdcall) :int32
  (file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (file-information-class :int32)   ; FILE_INFO_BY_NAME_CLASS
  (file-info-buffer :pointer)   ; void* out
  (file-info-buffer-size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetFileInformationByName = Win32::API::More->new('KERNEL32',
    'BOOL GetFileInformationByName(LPCWSTR FileName, int FileInformationClass, LPVOID FileInfoBuffer, DWORD FileInfoBufferSize)');
# my $ret = $GetFileInformationByName->Call($FileName, $FileInformationClass, $FileInfoBuffer, $FileInfoBufferSize);
# FileName : LPCWSTR -> LPCWSTR
# FileInformationClass : FILE_INFO_BY_NAME_CLASS -> int
# FileInfoBuffer : void* out -> LPVOID
# FileInfoBufferSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型