DragQueryFileW
関数ドロップされたファイル名(Unicode)や個数を取得する。
シグネチャ
// SHELL32.dll (Unicode / -W)
#include <windows.h>
DWORD DragQueryFileW(
HDROP hDrop,
DWORD iFile,
LPWSTR lpszFile, // optional
DWORD cch
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hDrop | HDROP | in | ドロップされたファイルのファイル名を格納する構造体の識別子です。 |
| iFile | DWORD | in | 問い合わせるファイルのインデックスです。このパラメーターの値が 0xFFFFFFFF の場合、DragQueryFile はドロップされたファイルの数を返します。このパラメーターの値が 0 からドロップされたファイルの総数までの範囲にある場合、DragQueryFile は対応する値のファイル名を lpszFile パラメーターが指すバッファーにコピーします。 |
| lpszFile | LPWSTR | outoptional | 関数が戻るときに、ドロップされたファイルのファイル名を受け取るバッファーのアドレスです。このファイル名は null 終端文字列です。このパラメーターが NULL の場合、DragQueryFile はこのバッファーに必要なサイズ(文字数)を返します。 |
| cch | DWORD | in | lpszFile バッファーのサイズ(文字数)です。 |
戻り値の型: DWORD
公式ドキュメント
ドラッグアンドドロップ操作が成功した結果としてドロップされたファイルの名前を取得します。(Unicode)
戻り値
型: UINT
0 以外の値は呼び出しが成功したことを示します。
関数がファイル名をバッファーにコピーした場合、戻り値はコピーされた文字数であり、終端の null 文字は含まれません。
インデックス値が 0xFFFFFFFF の場合、戻り値はドロップされたファイルの数です。インデックス変数自体は変更されずに返されるため、0xFFFFFFFF のままである点に注意してください。
インデックス値が 0 からドロップされたファイルの総数までの範囲にあり、lpszFile バッファーのアドレスが NULL の場合、戻り値はバッファーに必要なサイズ(文字数)であり、終端の null 文字は含まれません。
解説(Remarks)
メモ
shellapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして DragQueryFile を定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存ではないコードと混在させると、コンパイルエラーまたは実行時エラーを引き起こす不一致が発生する可能性があります。詳細については、「Conventions for Function Prototypes」を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHELL32.dll (Unicode / -W)
#include <windows.h>
DWORD DragQueryFileW(
HDROP hDrop,
DWORD iFile,
LPWSTR lpszFile, // optional
DWORD cch
);[DllImport("SHELL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DragQueryFileW(
IntPtr hDrop, // HDROP
uint iFile, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpszFile, // LPWSTR optional, out
uint cch // DWORD
);<DllImport("SHELL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DragQueryFileW(
hDrop As IntPtr, ' HDROP
iFile As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPWStr)> lpszFile As System.Text.StringBuilder, ' LPWSTR optional, out
cch As UInteger ' DWORD
) As UInteger
End Function' hDrop : HDROP
' iFile : DWORD
' lpszFile : LPWSTR optional, out
' cch : DWORD
Declare PtrSafe Function DragQueryFileW Lib "shell32" ( _
ByVal hDrop As LongPtr, _
ByVal iFile As Long, _
ByVal lpszFile As LongPtr, _
ByVal cch As Long) 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
DragQueryFileW = ctypes.windll.shell32.DragQueryFileW
DragQueryFileW.restype = wintypes.DWORD
DragQueryFileW.argtypes = [
wintypes.HANDLE, # hDrop : HDROP
wintypes.DWORD, # iFile : DWORD
wintypes.LPWSTR, # lpszFile : LPWSTR optional, out
wintypes.DWORD, # cch : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHELL32.dll')
DragQueryFileW = Fiddle::Function.new(
lib['DragQueryFileW'],
[
Fiddle::TYPE_VOIDP, # hDrop : HDROP
-Fiddle::TYPE_INT, # iFile : DWORD
Fiddle::TYPE_VOIDP, # lpszFile : LPWSTR optional, out
-Fiddle::TYPE_INT, # cch : DWORD
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shell32")]
extern "system" {
fn DragQueryFileW(
hDrop: *mut core::ffi::c_void, // HDROP
iFile: u32, // DWORD
lpszFile: *mut u16, // LPWSTR optional, out
cch: u32 // DWORD
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHELL32.dll", CharSet = CharSet.Unicode)]
public static extern uint DragQueryFileW(IntPtr hDrop, uint iFile, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpszFile, uint cch);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_DragQueryFileW' -Namespace Win32 -PassThru
# $api::DragQueryFileW(hDrop, iFile, lpszFile, cch)#uselib "SHELL32.dll"
#func global DragQueryFileW "DragQueryFileW" wptr, wptr, wptr, wptr
; DragQueryFileW hDrop, iFile, varptr(lpszFile), cch ; 戻り値は stat
; hDrop : HDROP -> "wptr"
; iFile : DWORD -> "wptr"
; lpszFile : LPWSTR optional, out -> "wptr"
; cch : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHELL32.dll" #cfunc global DragQueryFileW "DragQueryFileW" sptr, int, var, int ; res = DragQueryFileW(hDrop, iFile, lpszFile, cch) ; hDrop : HDROP -> "sptr" ; iFile : DWORD -> "int" ; lpszFile : LPWSTR optional, out -> "var" ; cch : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHELL32.dll" #cfunc global DragQueryFileW "DragQueryFileW" sptr, int, sptr, int ; res = DragQueryFileW(hDrop, iFile, varptr(lpszFile), cch) ; hDrop : HDROP -> "sptr" ; iFile : DWORD -> "int" ; lpszFile : LPWSTR optional, out -> "sptr" ; cch : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD DragQueryFileW(HDROP hDrop, DWORD iFile, LPWSTR lpszFile, DWORD cch) #uselib "SHELL32.dll" #cfunc global DragQueryFileW "DragQueryFileW" intptr, int, var, int ; res = DragQueryFileW(hDrop, iFile, lpszFile, cch) ; hDrop : HDROP -> "intptr" ; iFile : DWORD -> "int" ; lpszFile : LPWSTR optional, out -> "var" ; cch : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD DragQueryFileW(HDROP hDrop, DWORD iFile, LPWSTR lpszFile, DWORD cch) #uselib "SHELL32.dll" #cfunc global DragQueryFileW "DragQueryFileW" intptr, int, intptr, int ; res = DragQueryFileW(hDrop, iFile, varptr(lpszFile), cch) ; hDrop : HDROP -> "intptr" ; iFile : DWORD -> "int" ; lpszFile : LPWSTR optional, out -> "intptr" ; cch : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shell32 = windows.NewLazySystemDLL("SHELL32.dll")
procDragQueryFileW = shell32.NewProc("DragQueryFileW")
)
// hDrop (HDROP), iFile (DWORD), lpszFile (LPWSTR optional, out), cch (DWORD)
r1, _, err := procDragQueryFileW.Call(
uintptr(hDrop),
uintptr(iFile),
uintptr(lpszFile),
uintptr(cch),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction DragQueryFileW(
hDrop: THandle; // HDROP
iFile: DWORD; // DWORD
lpszFile: PWideChar; // LPWSTR optional, out
cch: DWORD // DWORD
): DWORD; stdcall;
external 'SHELL32.dll' name 'DragQueryFileW';result := DllCall("SHELL32\DragQueryFileW"
, "Ptr", hDrop ; HDROP
, "UInt", iFile ; DWORD
, "Ptr", lpszFile ; LPWSTR optional, out
, "UInt", cch ; DWORD
, "UInt") ; return: DWORD●DragQueryFileW(hDrop, iFile, lpszFile, cch) = DLL("SHELL32.dll", "dword DragQueryFileW(void*, dword, char*, dword)")
# 呼び出し: DragQueryFileW(hDrop, iFile, lpszFile, cch)
# hDrop : HDROP -> "void*"
# iFile : DWORD -> "dword"
# lpszFile : LPWSTR optional, out -> "char*"
# cch : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shell32" fn DragQueryFileW(
hDrop: ?*anyopaque, // HDROP
iFile: u32, // DWORD
lpszFile: [*c]u16, // LPWSTR optional, out
cch: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc DragQueryFileW(
hDrop: pointer, # HDROP
iFile: uint32, # DWORD
lpszFile: ptr uint16, # LPWSTR optional, out
cch: uint32 # DWORD
): uint32 {.importc: "DragQueryFileW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shell32");
extern(Windows)
uint DragQueryFileW(
void* hDrop, // HDROP
uint iFile, // DWORD
wchar* lpszFile, // LPWSTR optional, out
uint cch // DWORD
);ccall((:DragQueryFileW, "SHELL32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, UInt32, Ptr{UInt16}, UInt32),
hDrop, iFile, lpszFile, cch)
# hDrop : HDROP -> Ptr{Cvoid}
# iFile : DWORD -> UInt32
# lpszFile : LPWSTR optional, out -> Ptr{UInt16}
# cch : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t DragQueryFileW(
void* hDrop,
uint32_t iFile,
uint16_t* lpszFile,
uint32_t cch);
]]
local shell32 = ffi.load("shell32")
-- shell32.DragQueryFileW(hDrop, iFile, lpszFile, cch)
-- hDrop : HDROP
-- iFile : DWORD
-- lpszFile : LPWSTR optional, out
-- cch : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const DragQueryFileW = lib.func('__stdcall', 'DragQueryFileW', 'uint32_t', ['void *', 'uint32_t', 'uint16_t *', 'uint32_t']);
// DragQueryFileW(hDrop, iFile, lpszFile, cch)
// hDrop : HDROP -> 'void *'
// iFile : DWORD -> 'uint32_t'
// lpszFile : LPWSTR optional, out -> 'uint16_t *'
// cch : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHELL32.dll", {
DragQueryFileW: { parameters: ["pointer", "u32", "buffer", "u32"], result: "u32" },
});
// lib.symbols.DragQueryFileW(hDrop, iFile, lpszFile, cch)
// hDrop : HDROP -> "pointer"
// iFile : DWORD -> "u32"
// lpszFile : LPWSTR optional, out -> "buffer"
// cch : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t DragQueryFileW(
void* hDrop,
uint32_t iFile,
uint16_t* lpszFile,
uint32_t cch);
C, "SHELL32.dll");
// $ffi->DragQueryFileW(hDrop, iFile, lpszFile, cch);
// hDrop : HDROP
// iFile : DWORD
// lpszFile : LPWSTR 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.UNICODE_OPTIONS);
int DragQueryFileW(
Pointer hDrop, // HDROP
int iFile, // DWORD
char[] lpszFile, // LPWSTR optional, out
int cch // DWORD
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shell32")]
lib LibSHELL32
fun DragQueryFileW = DragQueryFileW(
hDrop : Void*, # HDROP
iFile : UInt32, # DWORD
lpszFile : UInt16*, # LPWSTR 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 DragQueryFileWNative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Utf16>, Uint32);
typedef DragQueryFileWDart = int Function(Pointer<Void>, int, Pointer<Utf16>, int);
final DragQueryFileW = DynamicLibrary.open('SHELL32.dll')
.lookupFunction<DragQueryFileWNative, DragQueryFileWDart>('DragQueryFileW');
// hDrop : HDROP -> Pointer<Void>
// iFile : DWORD -> Uint32
// lpszFile : LPWSTR optional, out -> Pointer<Utf16>
// cch : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function DragQueryFileW(
hDrop: THandle; // HDROP
iFile: DWORD; // DWORD
lpszFile: PWideChar; // LPWSTR optional, out
cch: DWORD // DWORD
): DWORD; stdcall;
external 'SHELL32.dll' name 'DragQueryFileW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "DragQueryFileW"
c_DragQueryFileW :: Ptr () -> Word32 -> CWString -> Word32 -> IO Word32
-- hDrop : HDROP -> Ptr ()
-- iFile : DWORD -> Word32
-- lpszFile : LPWSTR optional, out -> CWString
-- cch : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let dragqueryfilew =
foreign "DragQueryFileW"
((ptr void) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* hDrop : HDROP -> (ptr void) *)
(* iFile : DWORD -> uint32_t *)
(* lpszFile : LPWSTR optional, out -> (ptr uint16_t) *)
(* 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 ("DragQueryFileW" drag-query-file-w :convention :stdcall) :uint32
(h-drop :pointer) ; HDROP
(i-file :uint32) ; DWORD
(lpsz-file :pointer) ; LPWSTR optional, out
(cch :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $DragQueryFileW = Win32::API::More->new('SHELL32',
'DWORD DragQueryFileW(HANDLE hDrop, DWORD iFile, LPWSTR lpszFile, DWORD cch)');
# my $ret = $DragQueryFileW->Call($hDrop, $iFile, $lpszFile, $cch);
# hDrop : HDROP -> HANDLE
# iFile : DWORD -> DWORD
# lpszFile : LPWSTR optional, out -> LPWSTR
# cch : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f DragQueryFileA (ANSI版) — ドロップされたファイル名(ANSI)や個数を取得する。
公式の関連項目
- f DragQueryPoint — ファイルがドロップされた位置の座標を取得する。