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

GetFullPathNameW

関数
相対パスを絶対パスに変換して取得する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD GetFullPathNameW(
    LPCWSTR lpFileName,
    DWORD nBufferLength,
    LPWSTR lpBuffer,   // optional
    LPWSTR* lpFilePart   // optional
);

パラメーター

名前方向説明
lpFileNameLPCWSTRin

ファイルの名前。

このパラメーターには、短い名前(8.3 形式)または長いファイル名を指定できます。この文字列は、共有名またはボリューム名にすることもできます。

既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳細については、Naming Files, Paths, and Namespaces を参照してください。

ヒント

Windows 10 バージョン 1607 以降では、"\\?\" を付加せずに MAX_PATH の制限を解除するオプトインが可能です。詳細については、Naming Files, Paths, and Namespaces の「Maximum Path Length Limitation」セクションを参照してください。

nBufferLengthDWORDinドライブとパスの null 終端文字列を受け取るバッファーのサイズ(TCHAR 単位)。
lpBufferLPWSTRoutoptionalドライブとパスの null 終端文字列を受け取るバッファーへのポインター。
lpFilePartLPWSTR*outoptional

パス内の最後のファイル名コンポーネントのアドレス(lpBuffer 内の位置)を受け取るバッファーへのポインター。

このパラメーターには NULL を指定できます。

lpBuffer がファイルではなくディレクトリを指す場合、lpFilePart はゼロを受け取ります。

戻り値の型: DWORD

公式ドキュメント

指定したファイルのフルパスとファイル名を取得します。(Unicode)

戻り値

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

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

その他の理由で関数が失敗した場合、戻り値はゼロです。拡張エラー情報を取得するには、GetLastError を呼び出してください。

解説(Remarks)

GetFullPathName は、現在のドライブとディレクトリの名前を指定したファイル名と結合し、指定したファイルのフルパスとファイル名を判別します。また、フルパスおよびファイル名のうちファイル名部分のアドレスも計算します。

この関数は、結果として得られるパスとファイル名が有効であるか、または対象のボリューム上に既存のファイルを指しているかを検証しません。

lpFilePart パラメーターには文字列バッファー領域は不要で、単一のアドレスを格納できるだけの領域があれば十分である点に注意してください。これは、lpBuffer 用にすでに存在するバッファー内のアドレスを返すだけだからです。

共有名およびボリューム名は lpFileName の有効な入力です。たとえば、test-2 がリモートコンピューターで、U: が現在のディレクトリがボリュームのルートであるネットワークマップドライブである場合、次の一覧は返されるパスとファイル名を示します:

GetFullPathName は、指定されたファイル名 lpFileName を変換しません。指定したファイル名が存在する場合は、GetLongPathName または GetShortPathName を使用して、それぞれ長いパス名または短いパス名に変換できます。

戻り値が nBufferLength で指定した値以上の場合、パスを保持するのに十分な大きさのバッファーを用意して、関数を再度呼び出すことができます。このケースの例、および動的割り当てのために長さ 0 のバッファーを使用する例については、サンプルコードのセクションを参照してください。

このケースでの戻り値は終端の null 文字を含む長さですが、成功時の戻り値はこのカウントに終端の null 文字を含みません。

GetFullPathName 関数に渡される相対パスは、プロセスの現在のディレクトリからの相対として解釈されます。SetCurrentDirectory 関数によって書き込まれる現在のディレクトリの状態は、プロセス全体でグローバルであり、任意のスレッドがいつでも変更できます。アプリケーションは、相対パスを指定した GetFullPathName 関数の連続した呼び出しの間に現在のディレクトリが変化すると、異なる結果が生じる可能性があることに注意してください。

一貫性のない結果による問題を回避するため、マルチスレッドアプリケーションおよび共有ライブラリのコードでは、相対パスの使用を避けるべきです。相対パスを受け取った場合は、ちょうど 1 回だけ消費すべきです。すなわち、その相対パスを CreateFile のような関数に直接渡すか、絶対パスに変換してそれ以降は絶対パスを使用してください。

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) はい

次の C++ の例では、GetFullPathNameGetLongPathName、および GetShortPathName の基本的な使用方法を示します。動的割り当てを使用する別の例については、GetShortPathName を参照してください。

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define BUFSIZE 4096
#define LONG_DIR_NAME TEXT("c:\\longdirectoryname")

void _tmain(int argc, TCHAR *argv[])
{
    DWORD  retval=0;
    BOOL   success; 
    TCHAR  buffer[BUFSIZE]=TEXT(""); 
    TCHAR  buf[BUFSIZE]=TEXT(""); 
    TCHAR** lppPart={NULL};

   if( argc != 2 )
   {
      _tprintf(TEXT("Usage: %s [file]\n"), argv[0]);
      return;
   }

// Retrieve the full path name for a file. 
// The file does not need to exist.

    retval = GetFullPathName(argv[1],
                 BUFSIZE,
                 buffer,
                 lppPart);
    
    if (retval == 0) 
    {
        // Handle an error condition.
        printf ("GetFullPathName failed (%d)\n", GetLastError());
        return;
    }
    else 
    {
        _tprintf(TEXT("The full path name is:  %s\n"), buffer);
        if (lppPart != NULL && *lppPart != 0)
        {
            _tprintf(TEXT("The final component in the path name is:  %s\n"), *lppPart);
        }
    }


// Create a long directory name for use with the next two examples.

    success = CreateDirectory(LONG_DIR_NAME,
                              NULL);

    if (!success)
    {
        // Handle an error condition.
        printf ("CreateDirectory failed (%d)\n", GetLastError());
        return;
    }


// Retrieve the short path name.  

    retval = GetShortPathName(LONG_DIR_NAME,
                  buf,
                  BUFSIZE);

    if (retval == 0) 
    {
        // Handle an error condition.
         printf ("GetShortPathName failed (%d)\n", GetLastError());
         return;
    }
    else _tprintf(TEXT("The short name for %s is %s\n"), 
                  LONG_DIR_NAME, buf);


// Retrieve the long path name.  

    retval = GetLongPathName(buf,
              buffer,
              BUFSIZE);

    if (retval == 0) 
    {
        // Handle an error condition.
         printf ("GetLongPathName failed (%d)\n", GetLastError());
         return;
    }
    else _tprintf(TEXT("The long name for %s is %s\n"), buf, buffer);

// Clean up the directory.

    success = RemoveDirectory(LONG_DIR_NAME);
    if (!success)
    {
        // Handle an error condition.
        printf ("RemoveDirectory failed (%d)\n", GetLastError());
        return;
    }
}
メモ

fileapi.h ヘッダーは、GetFullPathName を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不一致が生じ、コンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 GetFullPathNameW(
    LPCWSTR lpFileName,
    DWORD nBufferLength,
    LPWSTR lpBuffer,   // optional
    LPWSTR* lpFilePart   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetFullPathNameW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,   // LPCWSTR
    uint nBufferLength,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer,   // LPWSTR optional, out
    IntPtr lpFilePart   // LPWSTR* optional, out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetFullPathNameW(
    <MarshalAs(UnmanagedType.LPWStr)> lpFileName As String,   ' LPCWSTR
    nBufferLength As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder,   ' LPWSTR optional, out
    lpFilePart As IntPtr   ' LPWSTR* optional, out
) As UInteger
End Function
' lpFileName : LPCWSTR
' nBufferLength : DWORD
' lpBuffer : LPWSTR optional, out
' lpFilePart : LPWSTR* optional, out
Declare PtrSafe Function GetFullPathNameW Lib "kernel32" ( _
    ByVal lpFileName As LongPtr, _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As LongPtr, _
    ByVal lpFilePart 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

GetFullPathNameW = ctypes.windll.kernel32.GetFullPathNameW
GetFullPathNameW.restype = wintypes.DWORD
GetFullPathNameW.argtypes = [
    wintypes.LPCWSTR,  # lpFileName : LPCWSTR
    wintypes.DWORD,  # nBufferLength : DWORD
    wintypes.LPWSTR,  # lpBuffer : LPWSTR optional, out
    ctypes.c_void_p,  # lpFilePart : LPWSTR* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetFullPathNameW = kernel32.NewProc("GetFullPathNameW")
)

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

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

typedef GetFullPathNameWNative = Uint32 Function(Pointer<Utf16>, Uint32, Pointer<Utf16>, Pointer<Pointer<Utf16>>);
typedef GetFullPathNameWDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Pointer<Utf16>>);
final GetFullPathNameW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetFullPathNameWNative, GetFullPathNameWDart>('GetFullPathNameW');
// lpFileName : LPCWSTR -> Pointer<Utf16>
// nBufferLength : DWORD -> Uint32
// lpBuffer : LPWSTR optional, out -> Pointer<Utf16>
// lpFilePart : LPWSTR* optional, out -> Pointer<Pointer<Utf16>>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetFullPathNameW(
  lpFileName: PWideChar;   // LPCWSTR
  nBufferLength: DWORD;   // DWORD
  lpBuffer: PWideChar;   // LPWSTR optional, out
  lpFilePart: PPWideChar   // LPWSTR* optional, out
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetFullPathNameW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetFullPathNameW"
  c_GetFullPathNameW :: CWString -> Word32 -> CWString -> Ptr CWString -> IO Word32
-- lpFileName : LPCWSTR -> CWString
-- nBufferLength : DWORD -> Word32
-- lpBuffer : LPWSTR optional, out -> CWString
-- lpFilePart : LPWSTR* optional, out -> Ptr CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getfullpathnamew =
  foreign "GetFullPathNameW"
    ((ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> (ptr (ptr uint16_t)) @-> returning uint32_t)
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* nBufferLength : DWORD -> uint32_t *)
(* lpBuffer : LPWSTR optional, out -> (ptr uint16_t) *)
(* lpFilePart : LPWSTR* optional, out -> (ptr (ptr uint16_t)) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetFullPathNameW" get-full-path-name-w :convention :stdcall) :uint32
  (lp-file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (n-buffer-length :uint32)   ; DWORD
  (lp-buffer :pointer)   ; LPWSTR optional, out
  (lp-file-part :pointer))   ; LPWSTR* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetFullPathNameW = Win32::API::More->new('KERNEL32',
    'DWORD GetFullPathNameW(LPCWSTR lpFileName, DWORD nBufferLength, LPWSTR lpBuffer, LPVOID lpFilePart)');
# my $ret = $GetFullPathNameW->Call($lpFileName, $nBufferLength, $lpBuffer, $lpFilePart);
# lpFileName : LPCWSTR -> LPCWSTR
# nBufferLength : DWORD -> DWORD
# lpBuffer : LPWSTR optional, out -> LPWSTR
# lpFilePart : LPWSTR* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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