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

QueryDosDeviceA

関数
MS-DOSデバイス名に対応するパス情報を取得する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD QueryDosDeviceA(
    LPCSTR lpDeviceName,   // optional
    LPSTR lpTargetPath,   // optional
    DWORD ucchMax
);

パラメーター

名前方向説明
lpDeviceNameLPCSTRinoptional

クエリの対象を指定する MS-DOS デバイス名の文字列です。デバイス名の末尾に バックスラッシュを付けることはできません。たとえば、"C:\" ではなく "C:" を使用します。

このパラメーターは NULL にできます。その場合、 QueryDosDevice 関数は、既存のすべての MS-DOS デバイス名の一覧を lpTargetPath が指す バッファーに格納します。

lpTargetPathLPSTRoutoptional

クエリの結果を受け取るバッファーへのポインターです。関数はこのバッファーに、1 つ以上の NULL 終端文字列を格納します。最後の NULL 終端文字列の後には、さらに NULL が続きます。

lpDeviceNameNULL 以外の場合、関数は lpDeviceName で指定された 特定の MS-DOS デバイスに関する情報を取得します。バッファーに格納される最初の NULL 終端文字列は、そのデバイスの現在のマッピングです。それ以外の NULL 終端 文字列は、そのデバイスに対する未削除の以前のマッピングを表します。

lpDeviceNameNULL の場合、関数は既存のすべての MS-DOS デバイス名の 一覧を取得します。バッファーに格納される各 NULL 終端文字列は、既存の MS-DOS デバイスの名前です。たとえば \Device\HarddiskVolume1 や \Device\Floppy0 などです。

ucchMaxDWORDinlpTargetPath が指すバッファーに格納できる TCHAR の最大数です。

戻り値の型: DWORD

公式ドキュメント

MS-DOS デバイス名に関する情報を取得します。(QueryDosDeviceA)

戻り値

関数が成功した場合、戻り値は lpTargetPath が指すバッファーに格納された TCHAR の数です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

バッファーが小さすぎる場合、関数は失敗し、最後のエラーコードは ERROR_INSUFFICIENT_BUFFER になります。

解説(Remarks)

DefineDosDevice 関数を使用すると、アプリケーションは MS-DOS デバイス名前空間を実装するために使用されるジャンクションを作成および変更できます。

Windows Server 2003 および Windows XP: QueryDosDevice は、指定されたデバイス名について最初に Local MS-DOS Device 名前空間を検索します。デバイス名が見つからない場合、関数は次に Global MS-DOS Device 名前空間を検索します。

既存のすべての MS-DOS デバイス名を照会する場合、返されるデバイス名の一覧は、 "LocalSystem" コンテキストで実行されているかどうかに依存します。実行されている場合は、Global MS-DOS Device 名前空間に含まれるデバイス名のみが返されます。実行されていない場合は、Global および Local MS-DOS Device 名前空間のデバイス名を連結したものが返されます。デバイス名が両方の名前空間に存在する場合、 QueryDosDevice は Local MS-DOS Device 名前空間のエントリを返します。

Global および Local MS-DOS Device 名前空間と、MS-DOS デバイス名のアクセス可能性に対する変更の 詳細については、 Defining an MS DOS Device Name を参照してください。

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) はい

SMB はボリューム管理機能をサポートしていません。

例については、 Obtaining a File Name From a File Handle または Displaying Volume Paths を参照してください。

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

各言語での呼び出し定義

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

DWORD QueryDosDeviceA(
    LPCSTR lpDeviceName,   // optional
    LPSTR lpTargetPath,   // optional
    DWORD ucchMax
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern uint QueryDosDeviceA(
    [MarshalAs(UnmanagedType.LPStr)] string lpDeviceName,   // LPCSTR optional
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpTargetPath,   // LPSTR optional, out
    uint ucchMax   // DWORD
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function QueryDosDeviceA(
    <MarshalAs(UnmanagedType.LPStr)> lpDeviceName As String,   ' LPCSTR optional
    <MarshalAs(UnmanagedType.LPStr)> lpTargetPath As System.Text.StringBuilder,   ' LPSTR optional, out
    ucchMax As UInteger   ' DWORD
) As UInteger
End Function
' lpDeviceName : LPCSTR optional
' lpTargetPath : LPSTR optional, out
' ucchMax : DWORD
Declare PtrSafe Function QueryDosDeviceA Lib "kernel32" ( _
    ByVal lpDeviceName As String, _
    ByVal lpTargetPath As String, _
    ByVal ucchMax As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

QueryDosDeviceA = ctypes.windll.kernel32.QueryDosDeviceA
QueryDosDeviceA.restype = wintypes.DWORD
QueryDosDeviceA.argtypes = [
    wintypes.LPCSTR,  # lpDeviceName : LPCSTR optional
    wintypes.LPSTR,  # lpTargetPath : LPSTR optional, out
    wintypes.DWORD,  # ucchMax : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procQueryDosDeviceA = kernel32.NewProc("QueryDosDeviceA")
)

// lpDeviceName (LPCSTR optional), lpTargetPath (LPSTR optional, out), ucchMax (DWORD)
r1, _, err := procQueryDosDeviceA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpDeviceName))),
	uintptr(lpTargetPath),
	uintptr(ucchMax),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function QueryDosDeviceA(
  lpDeviceName: PAnsiChar;   // LPCSTR optional
  lpTargetPath: PAnsiChar;   // LPSTR optional, out
  ucchMax: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'QueryDosDeviceA';
result := DllCall("KERNEL32\QueryDosDeviceA"
    , "AStr", lpDeviceName   ; LPCSTR optional
    , "Ptr", lpTargetPath   ; LPSTR optional, out
    , "UInt", ucchMax   ; DWORD
    , "UInt")   ; return: DWORD
●QueryDosDeviceA(lpDeviceName, lpTargetPath, ucchMax) = DLL("KERNEL32.dll", "dword QueryDosDeviceA(char*, char*, dword)")
# 呼び出し: QueryDosDeviceA(lpDeviceName, lpTargetPath, ucchMax)
# lpDeviceName : LPCSTR optional -> "char*"
# lpTargetPath : LPSTR optional, out -> "char*"
# ucchMax : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn QueryDosDeviceA(
    lpDeviceName: [*c]const u8, // LPCSTR optional
    lpTargetPath: [*c]u8, // LPSTR optional, out
    ucchMax: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc QueryDosDeviceA(
    lpDeviceName: cstring,  # LPCSTR optional
    lpTargetPath: ptr char,  # LPSTR optional, out
    ucchMax: uint32  # DWORD
): uint32 {.importc: "QueryDosDeviceA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
uint QueryDosDeviceA(
    const(char)* lpDeviceName,   // LPCSTR optional
    char* lpTargetPath,   // LPSTR optional, out
    uint ucchMax   // DWORD
);
ccall((:QueryDosDeviceA, "KERNEL32.dll"), stdcall, UInt32,
      (Cstring, Ptr{UInt8}, UInt32),
      lpDeviceName, lpTargetPath, ucchMax)
# lpDeviceName : LPCSTR optional -> Cstring
# lpTargetPath : LPSTR optional, out -> Ptr{UInt8}
# ucchMax : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t QueryDosDeviceA(
    const char* lpDeviceName,
    char* lpTargetPath,
    uint32_t ucchMax);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.QueryDosDeviceA(lpDeviceName, lpTargetPath, ucchMax)
-- lpDeviceName : LPCSTR optional
-- lpTargetPath : LPSTR optional, out
-- ucchMax : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const QueryDosDeviceA = lib.func('__stdcall', 'QueryDosDeviceA', 'uint32_t', ['str', 'char *', 'uint32_t']);
// QueryDosDeviceA(lpDeviceName, lpTargetPath, ucchMax)
// lpDeviceName : LPCSTR optional -> 'str'
// lpTargetPath : LPSTR optional, out -> 'char *'
// ucchMax : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  QueryDosDeviceA: { parameters: ["buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.QueryDosDeviceA(lpDeviceName, lpTargetPath, ucchMax)
// lpDeviceName : LPCSTR optional -> "buffer"
// lpTargetPath : LPSTR optional, out -> "buffer"
// ucchMax : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t QueryDosDeviceA(
    const char* lpDeviceName,
    char* lpTargetPath,
    uint32_t ucchMax);
C, "KERNEL32.dll");
// $ffi->QueryDosDeviceA(lpDeviceName, lpTargetPath, ucchMax);
// lpDeviceName : LPCSTR optional
// lpTargetPath : LPSTR optional, out
// ucchMax : 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, W32APIOptions.ASCII_OPTIONS);
    int QueryDosDeviceA(
        String lpDeviceName,   // LPCSTR optional
        byte[] lpTargetPath,   // LPSTR optional, out
        int ucchMax   // DWORD
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun QueryDosDeviceA = QueryDosDeviceA(
    lpDeviceName : UInt8*,   # LPCSTR optional
    lpTargetPath : UInt8*,   # LPSTR optional, out
    ucchMax : 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 QueryDosDeviceANative = Uint32 Function(Pointer<Utf8>, Pointer<Utf8>, Uint32);
typedef QueryDosDeviceADart = int Function(Pointer<Utf8>, Pointer<Utf8>, int);
final QueryDosDeviceA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<QueryDosDeviceANative, QueryDosDeviceADart>('QueryDosDeviceA');
// lpDeviceName : LPCSTR optional -> Pointer<Utf8>
// lpTargetPath : LPSTR optional, out -> Pointer<Utf8>
// ucchMax : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function QueryDosDeviceA(
  lpDeviceName: PAnsiChar;   // LPCSTR optional
  lpTargetPath: PAnsiChar;   // LPSTR optional, out
  ucchMax: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'QueryDosDeviceA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "QueryDosDeviceA"
  c_QueryDosDeviceA :: CString -> CString -> Word32 -> IO Word32
-- lpDeviceName : LPCSTR optional -> CString
-- lpTargetPath : LPSTR optional, out -> CString
-- ucchMax : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let querydosdevicea =
  foreign "QueryDosDeviceA"
    (string @-> string @-> uint32_t @-> returning uint32_t)
(* lpDeviceName : LPCSTR optional -> string *)
(* lpTargetPath : LPSTR optional, out -> string *)
(* ucchMax : 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 ("QueryDosDeviceA" query-dos-device-a :convention :stdcall) :uint32
  (lp-device-name :string)   ; LPCSTR optional
  (lp-target-path :pointer)   ; LPSTR optional, out
  (ucch-max :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $QueryDosDeviceA = Win32::API::More->new('KERNEL32',
    'DWORD QueryDosDeviceA(LPCSTR lpDeviceName, LPSTR lpTargetPath, DWORD ucchMax)');
# my $ret = $QueryDosDeviceA->Call($lpDeviceName, $lpTargetPath, $ucchMax);
# lpDeviceName : LPCSTR optional -> LPCSTR
# lpTargetPath : LPSTR optional, out -> LPSTR
# ucchMax : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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