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

GetVolumeNameForVolumeMountPointW

関数
マウントポイントから一意なボリューム名を取得する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetVolumeNameForVolumeMountPointW(
    LPCWSTR lpszVolumeMountPoint,
    LPWSTR lpszVolumeName,
    DWORD cchBufferLength
);

パラメーター

名前方向
lpszVolumeMountPointLPCWSTRin
lpszVolumeNameLPWSTRout
cchBufferLengthDWORDin

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetVolumeNameForVolumeMountPointW(
    LPCWSTR lpszVolumeMountPoint,
    LPWSTR lpszVolumeName,
    DWORD cchBufferLength
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool GetVolumeNameForVolumeMountPointW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpszVolumeName,   // LPWSTR out
    uint cchBufferLength   // DWORD
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetVolumeNameForVolumeMountPointW(
    <MarshalAs(UnmanagedType.LPWStr)> lpszVolumeMountPoint As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpszVolumeName As System.Text.StringBuilder,   ' LPWSTR out
    cchBufferLength As UInteger   ' DWORD
) As Boolean
End Function
' lpszVolumeMountPoint : LPCWSTR
' lpszVolumeName : LPWSTR out
' cchBufferLength : DWORD
Declare PtrSafe Function GetVolumeNameForVolumeMountPointW Lib "kernel32" ( _
    ByVal lpszVolumeMountPoint As LongPtr, _
    ByVal lpszVolumeName As LongPtr, _
    ByVal cchBufferLength 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

GetVolumeNameForVolumeMountPointW = ctypes.windll.kernel32.GetVolumeNameForVolumeMountPointW
GetVolumeNameForVolumeMountPointW.restype = wintypes.BOOL
GetVolumeNameForVolumeMountPointW.argtypes = [
    wintypes.LPCWSTR,  # lpszVolumeMountPoint : LPCWSTR
    wintypes.LPWSTR,  # lpszVolumeName : LPWSTR out
    wintypes.DWORD,  # cchBufferLength : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetVolumeNameForVolumeMountPointW = Fiddle::Function.new(
  lib['GetVolumeNameForVolumeMountPointW'],
  [
    Fiddle::TYPE_VOIDP,  # lpszVolumeMountPoint : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpszVolumeName : LPWSTR out
    -Fiddle::TYPE_INT,  # cchBufferLength : DWORD
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn GetVolumeNameForVolumeMountPointW(
        lpszVolumeMountPoint: *const u16,  // LPCWSTR
        lpszVolumeName: *mut u16,  // LPWSTR out
        cchBufferLength: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetVolumeNameForVolumeMountPointW([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpszVolumeName, uint cchBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetVolumeNameForVolumeMountPointW' -Namespace Win32 -PassThru
# $api::GetVolumeNameForVolumeMountPointW(lpszVolumeMountPoint, lpszVolumeName, cchBufferLength)
#uselib "KERNEL32.dll"
#func global GetVolumeNameForVolumeMountPointW "GetVolumeNameForVolumeMountPointW" wptr, wptr, wptr
; GetVolumeNameForVolumeMountPointW lpszVolumeMountPoint, varptr(lpszVolumeName), cchBufferLength   ; 戻り値は stat
; lpszVolumeMountPoint : LPCWSTR -> "wptr"
; lpszVolumeName : LPWSTR out -> "wptr"
; cchBufferLength : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetVolumeNameForVolumeMountPointW "GetVolumeNameForVolumeMountPointW" wstr, var, int
; res = GetVolumeNameForVolumeMountPointW(lpszVolumeMountPoint, lpszVolumeName, cchBufferLength)
; lpszVolumeMountPoint : LPCWSTR -> "wstr"
; lpszVolumeName : LPWSTR out -> "var"
; cchBufferLength : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetVolumeNameForVolumeMountPointW(LPCWSTR lpszVolumeMountPoint, LPWSTR lpszVolumeName, DWORD cchBufferLength)
#uselib "KERNEL32.dll"
#cfunc global GetVolumeNameForVolumeMountPointW "GetVolumeNameForVolumeMountPointW" wstr, var, int
; res = GetVolumeNameForVolumeMountPointW(lpszVolumeMountPoint, lpszVolumeName, cchBufferLength)
; lpszVolumeMountPoint : LPCWSTR -> "wstr"
; lpszVolumeName : LPWSTR out -> "var"
; cchBufferLength : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetVolumeNameForVolumeMountPointW = kernel32.NewProc("GetVolumeNameForVolumeMountPointW")
)

// lpszVolumeMountPoint (LPCWSTR), lpszVolumeName (LPWSTR out), cchBufferLength (DWORD)
r1, _, err := procGetVolumeNameForVolumeMountPointW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszVolumeMountPoint))),
	uintptr(lpszVolumeName),
	uintptr(cchBufferLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetVolumeNameForVolumeMountPointW(
  lpszVolumeMountPoint: PWideChar;   // LPCWSTR
  lpszVolumeName: PWideChar;   // LPWSTR out
  cchBufferLength: DWORD   // DWORD
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetVolumeNameForVolumeMountPointW';
result := DllCall("KERNEL32\GetVolumeNameForVolumeMountPointW"
    , "WStr", lpszVolumeMountPoint   ; LPCWSTR
    , "Ptr", lpszVolumeName   ; LPWSTR out
    , "UInt", cchBufferLength   ; DWORD
    , "Int")   ; return: BOOL
●GetVolumeNameForVolumeMountPointW(lpszVolumeMountPoint, lpszVolumeName, cchBufferLength) = DLL("KERNEL32.dll", "bool GetVolumeNameForVolumeMountPointW(char*, char*, dword)")
# 呼び出し: GetVolumeNameForVolumeMountPointW(lpszVolumeMountPoint, lpszVolumeName, cchBufferLength)
# lpszVolumeMountPoint : LPCWSTR -> "char*"
# lpszVolumeName : LPWSTR out -> "char*"
# cchBufferLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。