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

GetShortPathNameA

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

シグネチャ

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

DWORD GetShortPathNameA(
    LPCSTR lpszLongPath,
    LPSTR lpszShortPath,   // optional
    DWORD cchBuffer
);

パラメーター

名前方向
lpszLongPathLPCSTRin
lpszShortPathLPSTRoutoptional
cchBufferDWORDin

戻り値の型: DWORD

各言語での呼び出し定義

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

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

GetShortPathNameA = ctypes.windll.kernel32.GetShortPathNameA
GetShortPathNameA.restype = wintypes.DWORD
GetShortPathNameA.argtypes = [
    wintypes.LPCSTR,  # lpszLongPath : LPCSTR
    wintypes.LPSTR,  # lpszShortPath : LPSTR 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')
GetShortPathNameA = Fiddle::Function.new(
  lib['GetShortPathNameA'],
  [
    Fiddle::TYPE_VOIDP,  # lpszLongPath : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpszShortPath : LPSTR optional, out
    -Fiddle::TYPE_INT,  # cchBuffer : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn GetShortPathNameA(
        lpszLongPath: *const u8,  // LPCSTR
        lpszShortPath: *mut u8,  // LPSTR optional, out
        cchBuffer: 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 GetShortPathNameA([MarshalAs(UnmanagedType.LPStr)] string lpszLongPath, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszShortPath, uint cchBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetShortPathNameA' -Namespace Win32 -PassThru
# $api::GetShortPathNameA(lpszLongPath, lpszShortPath, cchBuffer)
#uselib "KERNEL32.dll"
#func global GetShortPathNameA "GetShortPathNameA" sptr, sptr, sptr
; GetShortPathNameA lpszLongPath, varptr(lpszShortPath), cchBuffer   ; 戻り値は stat
; lpszLongPath : LPCSTR -> "sptr"
; lpszShortPath : LPSTR optional, out -> "sptr"
; cchBuffer : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetShortPathNameA "GetShortPathNameA" str, var, int
; res = GetShortPathNameA(lpszLongPath, lpszShortPath, cchBuffer)
; lpszLongPath : LPCSTR -> "str"
; lpszShortPath : LPSTR optional, out -> "var"
; cchBuffer : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD GetShortPathNameA(LPCSTR lpszLongPath, LPSTR lpszShortPath, DWORD cchBuffer)
#uselib "KERNEL32.dll"
#cfunc global GetShortPathNameA "GetShortPathNameA" str, var, int
; res = GetShortPathNameA(lpszLongPath, lpszShortPath, cchBuffer)
; lpszLongPath : LPCSTR -> "str"
; lpszShortPath : LPSTR optional, out -> "var"
; cchBuffer : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetShortPathNameA = kernel32.NewProc("GetShortPathNameA")
)

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