Win32 API 日本語リファレンス
ホームUI.Shell › DragQueryFileA

DragQueryFileA

関数
ドロップされたファイル名(ANSI)や個数を取得する。
DLLSHELL32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHELL32.dll  (ANSI / -A)
#include <windows.h>

DWORD DragQueryFileA(
    HDROP hDrop,
    DWORD iFile,
    LPSTR lpszFile,   // optional
    DWORD cch
);

パラメーター

名前方向説明
hDropHDROPinドロップされたファイルのファイル名を含む構造体の識別子です。
iFileDWORDin照会するファイルのインデックスです。このパラメーターの値が 0xFFFFFFFF の場合、DragQueryFile はドロップされたファイルの数を返します。このパラメーターの値が 0 からドロップされたファイルの総数までの範囲にある場合、DragQueryFile は対応する値を持つファイル名を lpszFile パラメーターが指すバッファーにコピーします。
lpszFileLPSTRoutoptional関数が戻るときに、ドロップされたファイルのファイル名を受け取るバッファーのアドレスです。このファイル名は null で終わる文字列です。このパラメーターが NULL の場合、DragQueryFile はこのバッファーに必要なサイズを文字数で返します。
cchDWORDinlpszFile バッファーのサイズ(文字数)です。

戻り値の型: DWORD

公式ドキュメント

ドラッグアンドドロップ操作が成功した結果としてドロップされたファイルの名前を取得します。(ANSI)

戻り値

型: UINT

0 以外の値は、呼び出しが成功したことを示します。

関数がファイル名をバッファーにコピーする場合、戻り値はコピーされた文字数であり、終端の null 文字は含まれません。

インデックス値が 0xFFFFFFFF の場合、戻り値はドロップされたファイルの数です。インデックス変数自体は変更されずに返されるため、0xFFFFFFFF のままになることに注意してください。

インデックス値が 0 からドロップされたファイルの総数までの範囲にあり、lpszFile バッファーのアドレスが NULL の場合、戻り値はバッファーに必要なサイズ(文字数)であり、終端の null 文字は含まれません

解説(Remarks)

メモ

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

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

各言語での呼び出し定義

// SHELL32.dll  (ANSI / -A)
#include <windows.h>

DWORD DragQueryFileA(
    HDROP hDrop,
    DWORD iFile,
    LPSTR lpszFile,   // optional
    DWORD cch
);
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint DragQueryFileA(
    IntPtr hDrop,   // HDROP
    uint iFile,   // DWORD
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszFile,   // LPSTR optional, out
    uint cch   // DWORD
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function DragQueryFileA(
    hDrop As IntPtr,   ' HDROP
    iFile As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPStr)> lpszFile As System.Text.StringBuilder,   ' LPSTR optional, out
    cch As UInteger   ' DWORD
) As UInteger
End Function
' hDrop : HDROP
' iFile : DWORD
' lpszFile : LPSTR optional, out
' cch : DWORD
Declare PtrSafe Function DragQueryFileA Lib "shell32" ( _
    ByVal hDrop As LongPtr, _
    ByVal iFile As Long, _
    ByVal lpszFile As String, _
    ByVal cch As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

DragQueryFileA = ctypes.windll.shell32.DragQueryFileA
DragQueryFileA.restype = wintypes.DWORD
DragQueryFileA.argtypes = [
    wintypes.HANDLE,  # hDrop : HDROP
    wintypes.DWORD,  # iFile : DWORD
    wintypes.LPSTR,  # lpszFile : LPSTR optional, out
    wintypes.DWORD,  # cch : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
DragQueryFileA = Fiddle::Function.new(
  lib['DragQueryFileA'],
  [
    Fiddle::TYPE_VOIDP,  # hDrop : HDROP
    -Fiddle::TYPE_INT,  # iFile : DWORD
    Fiddle::TYPE_VOIDP,  # lpszFile : LPSTR optional, out
    -Fiddle::TYPE_INT,  # cch : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "shell32")]
extern "system" {
    fn DragQueryFileA(
        hDrop: *mut core::ffi::c_void,  // HDROP
        iFile: u32,  // DWORD
        lpszFile: *mut u8,  // LPSTR optional, out
        cch: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi)]
public static extern uint DragQueryFileA(IntPtr hDrop, uint iFile, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszFile, uint cch);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_DragQueryFileA' -Namespace Win32 -PassThru
# $api::DragQueryFileA(hDrop, iFile, lpszFile, cch)
#uselib "SHELL32.dll"
#func global DragQueryFileA "DragQueryFileA" sptr, sptr, sptr, sptr
; DragQueryFileA hDrop, iFile, varptr(lpszFile), cch   ; 戻り値は stat
; hDrop : HDROP -> "sptr"
; iFile : DWORD -> "sptr"
; lpszFile : LPSTR optional, out -> "sptr"
; cch : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHELL32.dll"
#cfunc global DragQueryFileA "DragQueryFileA" sptr, int, var, int
; res = DragQueryFileA(hDrop, iFile, lpszFile, cch)
; hDrop : HDROP -> "sptr"
; iFile : DWORD -> "int"
; lpszFile : LPSTR optional, out -> "var"
; cch : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD DragQueryFileA(HDROP hDrop, DWORD iFile, LPSTR lpszFile, DWORD cch)
#uselib "SHELL32.dll"
#cfunc global DragQueryFileA "DragQueryFileA" intptr, int, var, int
; res = DragQueryFileA(hDrop, iFile, lpszFile, cch)
; hDrop : HDROP -> "intptr"
; iFile : DWORD -> "int"
; lpszFile : LPSTR optional, out -> "var"
; cch : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procDragQueryFileA = shell32.NewProc("DragQueryFileA")
)

// hDrop (HDROP), iFile (DWORD), lpszFile (LPSTR optional, out), cch (DWORD)
r1, _, err := procDragQueryFileA.Call(
	uintptr(hDrop),
	uintptr(iFile),
	uintptr(lpszFile),
	uintptr(cch),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function DragQueryFileA(
  hDrop: THandle;   // HDROP
  iFile: DWORD;   // DWORD
  lpszFile: PAnsiChar;   // LPSTR optional, out
  cch: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHELL32.dll' name 'DragQueryFileA';
result := DllCall("SHELL32\DragQueryFileA"
    , "Ptr", hDrop   ; HDROP
    , "UInt", iFile   ; DWORD
    , "Ptr", lpszFile   ; LPSTR optional, out
    , "UInt", cch   ; DWORD
    , "UInt")   ; return: DWORD
●DragQueryFileA(hDrop, iFile, lpszFile, cch) = DLL("SHELL32.dll", "dword DragQueryFileA(void*, dword, char*, dword)")
# 呼び出し: DragQueryFileA(hDrop, iFile, lpszFile, cch)
# hDrop : HDROP -> "void*"
# iFile : DWORD -> "dword"
# lpszFile : LPSTR optional, out -> "char*"
# cch : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shell32" fn DragQueryFileA(
    hDrop: ?*anyopaque, // HDROP
    iFile: u32, // DWORD
    lpszFile: [*c]u8, // LPSTR optional, out
    cch: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc DragQueryFileA(
    hDrop: pointer,  # HDROP
    iFile: uint32,  # DWORD
    lpszFile: ptr char,  # LPSTR optional, out
    cch: uint32  # DWORD
): uint32 {.importc: "DragQueryFileA", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
uint DragQueryFileA(
    void* hDrop,   // HDROP
    uint iFile,   // DWORD
    char* lpszFile,   // LPSTR optional, out
    uint cch   // DWORD
);
ccall((:DragQueryFileA, "SHELL32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, UInt32, Ptr{UInt8}, UInt32),
      hDrop, iFile, lpszFile, cch)
# hDrop : HDROP -> Ptr{Cvoid}
# iFile : DWORD -> UInt32
# lpszFile : LPSTR optional, out -> Ptr{UInt8}
# cch : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DragQueryFileA(
    void* hDrop,
    uint32_t iFile,
    char* lpszFile,
    uint32_t cch);
]]
local shell32 = ffi.load("shell32")
-- shell32.DragQueryFileA(hDrop, iFile, lpszFile, cch)
-- hDrop : HDROP
-- iFile : DWORD
-- lpszFile : LPSTR optional, out
-- cch : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const DragQueryFileA = lib.func('__stdcall', 'DragQueryFileA', 'uint32_t', ['void *', 'uint32_t', 'char *', 'uint32_t']);
// DragQueryFileA(hDrop, iFile, lpszFile, cch)
// hDrop : HDROP -> 'void *'
// iFile : DWORD -> 'uint32_t'
// lpszFile : LPSTR optional, out -> 'char *'
// cch : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  DragQueryFileA: { parameters: ["pointer", "u32", "buffer", "u32"], result: "u32" },
});
// lib.symbols.DragQueryFileA(hDrop, iFile, lpszFile, cch)
// hDrop : HDROP -> "pointer"
// iFile : DWORD -> "u32"
// lpszFile : LPSTR optional, out -> "buffer"
// cch : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DragQueryFileA(
    void* hDrop,
    uint32_t iFile,
    char* lpszFile,
    uint32_t cch);
C, "SHELL32.dll");
// $ffi->DragQueryFileA(hDrop, iFile, lpszFile, cch);
// hDrop : HDROP
// iFile : DWORD
// lpszFile : LPSTR optional, out
// cch : 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 Shell32 extends StdCallLibrary {
    Shell32 INSTANCE = Native.load("shell32", Shell32.class, W32APIOptions.ASCII_OPTIONS);
    int DragQueryFileA(
        Pointer hDrop,   // HDROP
        int iFile,   // DWORD
        byte[] lpszFile,   // LPSTR optional, out
        int cch   // DWORD
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun DragQueryFileA = DragQueryFileA(
    hDrop : Void*,   # HDROP
    iFile : UInt32,   # DWORD
    lpszFile : UInt8*,   # LPSTR optional, out
    cch : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DragQueryFileANative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Utf8>, Uint32);
typedef DragQueryFileADart = int Function(Pointer<Void>, int, Pointer<Utf8>, int);
final DragQueryFileA = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<DragQueryFileANative, DragQueryFileADart>('DragQueryFileA');
// hDrop : HDROP -> Pointer<Void>
// iFile : DWORD -> Uint32
// lpszFile : LPSTR optional, out -> Pointer<Utf8>
// cch : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DragQueryFileA(
  hDrop: THandle;   // HDROP
  iFile: DWORD;   // DWORD
  lpszFile: PAnsiChar;   // LPSTR optional, out
  cch: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHELL32.dll' name 'DragQueryFileA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DragQueryFileA"
  c_DragQueryFileA :: Ptr () -> Word32 -> CString -> Word32 -> IO Word32
-- hDrop : HDROP -> Ptr ()
-- iFile : DWORD -> Word32
-- lpszFile : LPSTR optional, out -> CString
-- cch : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dragqueryfilea =
  foreign "DragQueryFileA"
    ((ptr void) @-> uint32_t @-> string @-> uint32_t @-> returning uint32_t)
(* hDrop : HDROP -> (ptr void) *)
(* iFile : DWORD -> uint32_t *)
(* lpszFile : LPSTR optional, out -> string *)
(* cch : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("DragQueryFileA" drag-query-file-a :convention :stdcall) :uint32
  (h-drop :pointer)   ; HDROP
  (i-file :uint32)   ; DWORD
  (lpsz-file :pointer)   ; LPSTR optional, out
  (cch :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DragQueryFileA = Win32::API::More->new('SHELL32',
    'DWORD DragQueryFileA(HANDLE hDrop, DWORD iFile, LPSTR lpszFile, DWORD cch)');
# my $ret = $DragQueryFileA->Call($hDrop, $iFile, $lpszFile, $cch);
# hDrop : HDROP -> HANDLE
# iFile : DWORD -> DWORD
# lpszFile : LPSTR optional, out -> LPSTR
# cch : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目