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

GetShortPathNameW

関数
長い形式のパスを8.3形式の短いパスに変換する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD GetShortPathNameW(
    LPCWSTR lpszLongPath,
    LPWSTR lpszShortPath,   // optional
    DWORD cchBuffer
);

パラメーター

名前方向
lpszLongPathLPCWSTRin
lpszShortPathLPWSTRoutoptional
cchBufferDWORDin

戻り値の型: DWORD

各言語での呼び出し定義

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

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

GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
GetShortPathNameW.restype = wintypes.DWORD
GetShortPathNameW.argtypes = [
    wintypes.LPCWSTR,  # lpszLongPath : LPCWSTR
    wintypes.LPWSTR,  # lpszShortPath : LPWSTR optional, out
    wintypes.DWORD,  # cchBuffer : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetShortPathNameW = kernel32.NewProc("GetShortPathNameW")
)

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