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

GetLongPathNameTransactedW

関数
トランザクション内で短いパスを長いパス名へ変換する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

DWORD GetLongPathNameTransactedW(
    LPCWSTR lpszShortPath,
    LPWSTR lpszLongPath,   // optional
    DWORD cchBuffer,
    HANDLE hTransaction
);

パラメーター

名前方向説明
lpszShortPathLPCWSTRin

変換するパス。

パスはローカルコンピューター上に存在している必要があります。そうでない場合、関数は失敗し、最後のエラーコードに ERROR_TRANSACTIONS_UNSUPPORTED_REMOTE が設定されます。

既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳細については、ファイル、パス、および名前空間の名前付け を参照してください。

ヒント

Windows 10 バージョン 1607 以降では、"\\?\" を付加せずに MAX_PATH の制限を取り除くオプトインが可能です。詳細については、ファイル、パス、および名前空間の名前付け の「Maximum Path Length Limitation」セクションを参照してください。

lpszLongPathLPWSTRoutoptional

ロングパスを受け取るバッファーへのポインター。

lpszShortPath パラメーターに使用したものと同じバッファーを使用できます。

cchBufferDWORDinlpszLongPath が指すバッファーのサイズ( TCHAR 単位)。
hTransactionHANDLEinトランザクションへのハンドル。このハンドルは CreateTransaction 関数によって返されます。

戻り値の型: DWORD

公式ドキュメント

指定したパスを、トランザクション操作としてロング形式に変換します。(Unicode)

戻り値

関数が成功した場合、戻り値は lpszLongPath にコピーされた文字列の長さ( TCHAR 単位)であり、終端の null 文字は含まれません。

lpBuffer バッファーがパスを格納するには小さすぎる場合、戻り値はパスと終端の null 文字を保持するために必要なバッファーのサイズ( TCHAR 単位)になります。

ファイルが存在しないなど、その他の理由で関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

多くのファイルシステムでは、短いファイル名にはチルダ () 文字が含まれます。ただし、すべてのファイルシステムがこの規則に従うわけではありません。したがって、パスにチルダ () 文字が含まれていないからといって、 GetLongPathNameTransacted の呼び出しを省略できると想定しないでください。

ロングパスが見つからない場合、この関数は lpszShortPath パラメーターで指定された名前を lpszLongPath パラメーターに返します。

戻り値が cchBuffer で指定した値より大きい場合は、パスを保持するのに十分な大きさのバッファーを使用して、もう一度関数を呼び出すことができます。この場合の例については、 GetFullPathName の Example Code セクションを参照してください。

注意 この場合の戻り値は終端の null 文字を含む長さですが、成功時の戻り値はその数に終端の null 文字を含みません。
あるファイルまたはディレクトリにアクセスできても、そのファイルまたはディレクトリの親ディレクトリの一部にアクセスできない場合があります。その結果、 GetLongPathNameTransacted は、あるパスコンポーネントのロング名を判別するためにその親ディレクトリを照会できない場合に失敗することがあります。このチェックは、ファイル拡張子が 3 文字より長い、または合計の長さが 12 文字より長いディレクトリコンポーネントについては省略できます。詳細については、 ファイル、パス、および名前空間の名前付けShort vs. Long Names セクションを参照してください。

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 3.0 は TxF をサポートしません。

メモ

winbase.h ヘッダーは、GetLongPathNameTransacted を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーやランタイムエラーを引き起こす不一致につながる可能性があります。詳細については、関数プロトタイプの規則 を参照してください。

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

各言語での呼び出し定義

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

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

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetLongPathNameTransactedW = kernel32.NewProc("GetLongPathNameTransactedW")
)

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

extern "kernel32" fn GetLongPathNameTransactedW(
    lpszShortPath: [*c]const u16, // LPCWSTR
    lpszLongPath: [*c]u16, // LPWSTR optional, out
    cchBuffer: u32, // DWORD
    hTransaction: ?*anyopaque // HANDLE
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetLongPathNameTransactedW(
    lpszShortPath: WideCString,  # LPCWSTR
    lpszLongPath: ptr uint16,  # LPWSTR optional, out
    cchBuffer: uint32,  # DWORD
    hTransaction: pointer  # HANDLE
): uint32 {.importc: "GetLongPathNameTransactedW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "kernel32");
extern(Windows)
uint GetLongPathNameTransactedW(
    const(wchar)* lpszShortPath,   // LPCWSTR
    wchar* lpszLongPath,   // LPWSTR optional, out
    uint cchBuffer,   // DWORD
    void* hTransaction   // HANDLE
);
ccall((:GetLongPathNameTransactedW, "KERNEL32.dll"), stdcall, UInt32,
      (Cwstring, Ptr{UInt16}, UInt32, Ptr{Cvoid}),
      lpszShortPath, lpszLongPath, cchBuffer, hTransaction)
# lpszShortPath : LPCWSTR -> Cwstring
# lpszLongPath : LPWSTR optional, out -> Ptr{UInt16}
# cchBuffer : DWORD -> UInt32
# hTransaction : HANDLE -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetLongPathNameTransactedW(
    const uint16_t* lpszShortPath,
    uint16_t* lpszLongPath,
    uint32_t cchBuffer,
    void* hTransaction);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetLongPathNameTransactedW(lpszShortPath, lpszLongPath, cchBuffer, hTransaction)
-- lpszShortPath : LPCWSTR
-- lpszLongPath : LPWSTR optional, out
-- cchBuffer : DWORD
-- hTransaction : HANDLE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetLongPathNameTransactedW = lib.func('__stdcall', 'GetLongPathNameTransactedW', 'uint32_t', ['str16', 'uint16_t *', 'uint32_t', 'void *']);
// GetLongPathNameTransactedW(lpszShortPath, lpszLongPath, cchBuffer, hTransaction)
// lpszShortPath : LPCWSTR -> 'str16'
// lpszLongPath : LPWSTR optional, out -> 'uint16_t *'
// cchBuffer : DWORD -> 'uint32_t'
// hTransaction : HANDLE -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetLongPathNameTransactedW: { parameters: ["buffer", "buffer", "u32", "pointer"], result: "u32" },
});
// lib.symbols.GetLongPathNameTransactedW(lpszShortPath, lpszLongPath, cchBuffer, hTransaction)
// lpszShortPath : LPCWSTR -> "buffer"
// lpszLongPath : LPWSTR optional, out -> "buffer"
// cchBuffer : DWORD -> "u32"
// hTransaction : HANDLE -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetLongPathNameTransactedW(
    const uint16_t* lpszShortPath,
    uint16_t* lpszLongPath,
    uint32_t cchBuffer,
    void* hTransaction);
C, "KERNEL32.dll");
// $ffi->GetLongPathNameTransactedW(lpszShortPath, lpszLongPath, cchBuffer, hTransaction);
// lpszShortPath : LPCWSTR
// lpszLongPath : LPWSTR optional, out
// cchBuffer : DWORD
// hTransaction : HANDLE
// 構造体/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.UNICODE_OPTIONS);
    int GetLongPathNameTransactedW(
        WString lpszShortPath,   // LPCWSTR
        char[] lpszLongPath,   // LPWSTR optional, out
        int cchBuffer,   // DWORD
        Pointer hTransaction   // HANDLE
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("kernel32")]
lib LibKERNEL32
  fun GetLongPathNameTransactedW = GetLongPathNameTransactedW(
    lpszShortPath : UInt16*,   # LPCWSTR
    lpszLongPath : UInt16*,   # LPWSTR optional, out
    cchBuffer : UInt32,   # DWORD
    hTransaction : Void*   # HANDLE
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetLongPathNameTransactedWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Void>);
typedef GetLongPathNameTransactedWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Void>);
final GetLongPathNameTransactedW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetLongPathNameTransactedWNative, GetLongPathNameTransactedWDart>('GetLongPathNameTransactedW');
// lpszShortPath : LPCWSTR -> Pointer<Utf16>
// lpszLongPath : LPWSTR optional, out -> Pointer<Utf16>
// cchBuffer : DWORD -> Uint32
// hTransaction : HANDLE -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetLongPathNameTransactedW(
  lpszShortPath: PWideChar;   // LPCWSTR
  lpszLongPath: PWideChar;   // LPWSTR optional, out
  cchBuffer: DWORD;   // DWORD
  hTransaction: THandle   // HANDLE
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetLongPathNameTransactedW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetLongPathNameTransactedW"
  c_GetLongPathNameTransactedW :: CWString -> CWString -> Word32 -> Ptr () -> IO Word32
-- lpszShortPath : LPCWSTR -> CWString
-- lpszLongPath : LPWSTR optional, out -> CWString
-- cchBuffer : DWORD -> Word32
-- hTransaction : HANDLE -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getlongpathnametransactedw =
  foreign "GetLongPathNameTransactedW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr void) @-> returning uint32_t)
(* lpszShortPath : LPCWSTR -> (ptr uint16_t) *)
(* lpszLongPath : LPWSTR optional, out -> (ptr uint16_t) *)
(* cchBuffer : DWORD -> uint32_t *)
(* hTransaction : HANDLE -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetLongPathNameTransactedW" get-long-path-name-transacted-w :convention :stdcall) :uint32
  (lpsz-short-path (:string :encoding :utf-16le))   ; LPCWSTR
  (lpsz-long-path :pointer)   ; LPWSTR optional, out
  (cch-buffer :uint32)   ; DWORD
  (h-transaction :pointer))   ; HANDLE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetLongPathNameTransactedW = Win32::API::More->new('KERNEL32',
    'DWORD GetLongPathNameTransactedW(LPCWSTR lpszShortPath, LPWSTR lpszLongPath, DWORD cchBuffer, HANDLE hTransaction)');
# my $ret = $GetLongPathNameTransactedW->Call($lpszShortPath, $lpszLongPath, $cchBuffer, $hTransaction);
# lpszShortPath : LPCWSTR -> LPCWSTR
# lpszLongPath : LPWSTR optional, out -> LPWSTR
# cchBuffer : DWORD -> DWORD
# hTransaction : HANDLE -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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