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

DefineDosDeviceA

関数
MS-DOSデバイス名を定義・変更・削除する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL DefineDosDeviceA(
    DEFINE_DOS_DEVICE_FLAGS dwFlags,
    LPCSTR lpDeviceName,
    LPCSTR lpTargetPath   // optional
);

パラメーター

名前方向説明
dwFlagsDEFINE_DOS_DEVICE_FLAGSin

DefineDosDevice 関数の制御可能な動作を指定します。このパラメーターには、次の値のうち 1 つ以上を指定できます。

意味
DDD_EXACT_MATCH_ON_REMOVE
0x00000004
この値を DDD_REMOVE_DEFINITION と共に指定すると、関数は完全一致によって削除するマッピングを決定します。定義していないものを誤って削除しないようにするには、この値を使用してください。
DDD_NO_BROADCAST_SYSTEM
0x00000008
WM_SETTINGCHANGE メッセージをブロードキャストしません。既定では、変更をシェルおよびアプリケーションに通知するために、このメッセージがブロードキャストされます。
DDD_RAW_TARGET_PATH
0x00000001
lpTargetPath 文字列をそのまま使用します。指定しない場合は、MS-DOS パスからパスへ変換されます。
DDD_REMOVE_DEFINITION
0x00000002
指定したデバイスについて、指定した定義を削除します。削除する定義を決定するために、関数はそのデバイスのマッピングの一覧をたどり、各マッピングのプレフィックスと lpTargetPath が一致するものを探します。最初に一致したマッピングが削除され、関数は処理を終了します。

lpTargetPathNULL または NULL 文字列へのポインターである場合、関数はそのデバイスに関連付けられた最初のマッピングを削除し、最後にプッシュされたものをポップします。ポップする対象が残っていない場合は、デバイス名が削除されます。

この値を指定しない場合、lpTargetPath パラメーターが指す文字列が、このデバイスの新しいマッピングになります。

lpDeviceNameLPCSTRin関数が定義、再定義、または削除するデバイスを指定する MS-DOS デバイス名文字列へのポインター。ドライブ文字を定義、再定義、または削除する場合を除き、デバイス名文字列の末尾の文字をコロンにすることはできません。たとえば、ドライブ C は文字列 "C:" になります。いかなる場合も、末尾のバックスラッシュ ("") は使用できません。
lpTargetPathLPCSTRinoptionalこのデバイスを実装するパス文字列へのポインター。DDD_RAW_TARGET_PATH フラグが指定されている場合を除き、この文字列は MS-DOS パス文字列です。指定されている場合は、この文字列はパス文字列です。

戻り値の型: BOOL

公式ドキュメント

MS-DOS デバイス名を定義、再定義、または削除します。(DefineDosDeviceA)

戻り値

関数が成功した場合、戻り値は 0 以外の値になります。

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

解説(Remarks)

MS-DOS デバイス名は、オブジェクト名前空間内にジャンクションとして格納されます。MS-DOS パスを対応するパスに変換するコードは、これらのジャンクションを使用して MS-DOS デバイスおよびドライブ文字をマップします。DefineDosDevice 関数を使用すると、アプリケーションは MS-DOS デバイス名前空間の実装に使用されるジャンクションを変更できます。

特定の MS-DOS デバイス名の現在のマッピングを取得したり、システムが認識しているすべての MS-DOS デバイスの一覧を取得したりするには、QueryDosDevice 関数を使用してください。

再起動後も保持され、ネットワーク共有ではないドライブ文字の割り当てを定義するには、SetVolumeMountPoint 関数を使用してください。マウントするボリュームに既にドライブ文字が割り当てられている場合は、DeleteVolumeMountPoint 関数を使用してその割り当てを削除してください。

システム起動時に定義されたドライブ文字およびデバイス名は、ユーザーが管理者でない限り、再定義や削除から保護されます。

Windows XP 以降では、この関数は、"LocalSystem" コンテキストで実行されていない呼び出し元に対して、そのローカル MS-DOS デバイス名前空間内にデバイス名を作成します。呼び出し元が "LocalSystem" コンテキストで実行されている場合、関数はグローバル MS-DOS デバイス名前空間にデバイス名を作成します。詳細については、Defining an MS DOS Device Name および File Names, Paths, and Namespaces を参照してください。

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 はボリューム管理関数をサポートしていません。CsvFs の場合、新しい名前はクラスター内の他のノードに複製されません。

例については、Editing Drive Letter Assignments を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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>

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

DefineDosDeviceA = ctypes.windll.kernel32.DefineDosDeviceA
DefineDosDeviceA.restype = wintypes.BOOL
DefineDosDeviceA.argtypes = [
    wintypes.DWORD,  # dwFlags : DEFINE_DOS_DEVICE_FLAGS
    wintypes.LPCSTR,  # lpDeviceName : LPCSTR
    wintypes.LPCSTR,  # lpTargetPath : LPCSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
DefineDosDeviceA = Fiddle::Function.new(
  lib['DefineDosDeviceA'],
  [
    -Fiddle::TYPE_INT,  # dwFlags : DEFINE_DOS_DEVICE_FLAGS
    Fiddle::TYPE_VOIDP,  # lpDeviceName : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpTargetPath : LPCSTR optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn DefineDosDeviceA(
        dwFlags: u32,  // DEFINE_DOS_DEVICE_FLAGS
        lpDeviceName: *const u8,  // LPCSTR
        lpTargetPath: *const u8  // LPCSTR optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool DefineDosDeviceA(uint dwFlags, [MarshalAs(UnmanagedType.LPStr)] string lpDeviceName, [MarshalAs(UnmanagedType.LPStr)] string lpTargetPath);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_DefineDosDeviceA' -Namespace Win32 -PassThru
# $api::DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath)
#uselib "KERNEL32.dll"
#func global DefineDosDeviceA "DefineDosDeviceA" sptr, sptr, sptr
; DefineDosDeviceA dwFlags, lpDeviceName, lpTargetPath   ; 戻り値は stat
; dwFlags : DEFINE_DOS_DEVICE_FLAGS -> "sptr"
; lpDeviceName : LPCSTR -> "sptr"
; lpTargetPath : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global DefineDosDeviceA "DefineDosDeviceA" int, str, str
; res = DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath)
; dwFlags : DEFINE_DOS_DEVICE_FLAGS -> "int"
; lpDeviceName : LPCSTR -> "str"
; lpTargetPath : LPCSTR optional -> "str"
; BOOL DefineDosDeviceA(DEFINE_DOS_DEVICE_FLAGS dwFlags, LPCSTR lpDeviceName, LPCSTR lpTargetPath)
#uselib "KERNEL32.dll"
#cfunc global DefineDosDeviceA "DefineDosDeviceA" int, str, str
; res = DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath)
; dwFlags : DEFINE_DOS_DEVICE_FLAGS -> "int"
; lpDeviceName : LPCSTR -> "str"
; lpTargetPath : LPCSTR optional -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procDefineDosDeviceA = kernel32.NewProc("DefineDosDeviceA")
)

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

extern "kernel32" fn DefineDosDeviceA(
    dwFlags: u32, // DEFINE_DOS_DEVICE_FLAGS
    lpDeviceName: [*c]const u8, // LPCSTR
    lpTargetPath: [*c]const u8 // LPCSTR optional
) callconv(std.os.windows.WINAPI) i32;
proc DefineDosDeviceA(
    dwFlags: uint32,  # DEFINE_DOS_DEVICE_FLAGS
    lpDeviceName: cstring,  # LPCSTR
    lpTargetPath: cstring  # LPCSTR optional
): int32 {.importc: "DefineDosDeviceA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int DefineDosDeviceA(
    uint dwFlags,   // DEFINE_DOS_DEVICE_FLAGS
    const(char)* lpDeviceName,   // LPCSTR
    const(char)* lpTargetPath   // LPCSTR optional
);
ccall((:DefineDosDeviceA, "KERNEL32.dll"), stdcall, Int32,
      (UInt32, Cstring, Cstring),
      dwFlags, lpDeviceName, lpTargetPath)
# dwFlags : DEFINE_DOS_DEVICE_FLAGS -> UInt32
# lpDeviceName : LPCSTR -> Cstring
# lpTargetPath : LPCSTR optional -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t DefineDosDeviceA(
    uint32_t dwFlags,
    const char* lpDeviceName,
    const char* lpTargetPath);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath)
-- dwFlags : DEFINE_DOS_DEVICE_FLAGS
-- lpDeviceName : LPCSTR
-- lpTargetPath : LPCSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const DefineDosDeviceA = lib.func('__stdcall', 'DefineDosDeviceA', 'int32_t', ['uint32_t', 'str', 'str']);
// DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath)
// dwFlags : DEFINE_DOS_DEVICE_FLAGS -> 'uint32_t'
// lpDeviceName : LPCSTR -> 'str'
// lpTargetPath : LPCSTR optional -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  DefineDosDeviceA: { parameters: ["u32", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath)
// dwFlags : DEFINE_DOS_DEVICE_FLAGS -> "u32"
// lpDeviceName : LPCSTR -> "buffer"
// lpTargetPath : LPCSTR optional -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t DefineDosDeviceA(
    uint32_t dwFlags,
    const char* lpDeviceName,
    const char* lpTargetPath);
C, "KERNEL32.dll");
// $ffi->DefineDosDeviceA(dwFlags, lpDeviceName, lpTargetPath);
// dwFlags : DEFINE_DOS_DEVICE_FLAGS
// lpDeviceName : LPCSTR
// lpTargetPath : LPCSTR optional
// 構造体/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);
    boolean DefineDosDeviceA(
        int dwFlags,   // DEFINE_DOS_DEVICE_FLAGS
        String lpDeviceName,   // LPCSTR
        String lpTargetPath   // LPCSTR optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun DefineDosDeviceA = DefineDosDeviceA(
    dwFlags : UInt32,   # DEFINE_DOS_DEVICE_FLAGS
    lpDeviceName : UInt8*,   # LPCSTR
    lpTargetPath : UInt8*   # LPCSTR optional
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DefineDosDeviceANative = Int32 Function(Uint32, Pointer<Utf8>, Pointer<Utf8>);
typedef DefineDosDeviceADart = int Function(int, Pointer<Utf8>, Pointer<Utf8>);
final DefineDosDeviceA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<DefineDosDeviceANative, DefineDosDeviceADart>('DefineDosDeviceA');
// dwFlags : DEFINE_DOS_DEVICE_FLAGS -> Uint32
// lpDeviceName : LPCSTR -> Pointer<Utf8>
// lpTargetPath : LPCSTR optional -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DefineDosDeviceA(
  dwFlags: DWORD;   // DEFINE_DOS_DEVICE_FLAGS
  lpDeviceName: PAnsiChar;   // LPCSTR
  lpTargetPath: PAnsiChar   // LPCSTR optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'DefineDosDeviceA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DefineDosDeviceA"
  c_DefineDosDeviceA :: Word32 -> CString -> CString -> IO CInt
-- dwFlags : DEFINE_DOS_DEVICE_FLAGS -> Word32
-- lpDeviceName : LPCSTR -> CString
-- lpTargetPath : LPCSTR optional -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let definedosdevicea =
  foreign "DefineDosDeviceA"
    (uint32_t @-> string @-> string @-> returning int32_t)
(* dwFlags : DEFINE_DOS_DEVICE_FLAGS -> uint32_t *)
(* lpDeviceName : LPCSTR -> string *)
(* lpTargetPath : LPCSTR optional -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("DefineDosDeviceA" define-dos-device-a :convention :stdcall) :int32
  (dw-flags :uint32)   ; DEFINE_DOS_DEVICE_FLAGS
  (lp-device-name :string)   ; LPCSTR
  (lp-target-path :string))   ; LPCSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DefineDosDeviceA = Win32::API::More->new('KERNEL32',
    'BOOL DefineDosDeviceA(DWORD dwFlags, LPCSTR lpDeviceName, LPCSTR lpTargetPath)');
# my $ret = $DefineDosDeviceA->Call($dwFlags, $lpDeviceName, $lpTargetPath);
# dwFlags : DEFINE_DOS_DEVICE_FLAGS -> DWORD
# lpDeviceName : LPCSTR -> LPCSTR
# lpTargetPath : LPCSTR optional -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目
使用する型