PathCchCanonicalize
関数シグネチャ
// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>
HRESULT PathCchCanonicalize(
LPWSTR pszPathOut,
UINT_PTR cchPathOut,
LPCWSTR pszPathIn
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszPathOut | LPWSTR | out | この関数が正常に終了したときに、正規化されたパス文字列を受け取るバッファーへのポインター。 |
| cchPathOut | UINT_PTR | in | pszPathOut が指すバッファーのサイズ(文字数)。 |
| pszPathIn | LPCWSTR | in | 元のパス文字列へのポインター。この値が空の文字列を指している場合、または "." 要素と ".." 要素を削除した結果が空の文字列になる場合は、pszPathOut が指すバッファーに 1 つのバックスラッシュがコピーされます。 |
戻り値の型: HRESULT
公式ドキュメント
パス文字列を正規化された形式に変換します。この関数は、最終的なパスの長さが MAX_PATH に制限される点で PathCchCanonicalizeEx と異なります。この関数は、呼び出し元が返される文字列のサイズを宣言し、その文字列がスタックに格納される点で PathAllocCanonicalize と異なります。この関数は、"\"、"\?"、"\?\UNC" のプレフィックスを持つパスを受け付ける点で PathCanonicalize と異なります。
戻り値
この関数が成功した場合は S_OK を返します。それ以外の場合は、次のものを含む HRESULT コードを返します。
| 戻り値 | 説明 |
|---|---|
| cchPathOut の値が PATHCCH_MAX_CCH より大きいです。 | |
|
パスセグメントが、標準のパスセグメント長の上限である 256 文字を超えています。 |
| 必要なサイズのバッファーを割り当てられませんでした。 |
解説(Remarks)
この関数は、パスに埋め込まれた "." および ".." の文字列に応答します。".." の文字列は、直前のパスセグメントを削除することを示します。"." の文字列は、次のパスセグメントをスキップすることを示します。パスのルートセグメントは削除できないことに注意してください。パスセグメントの数よりも多くの ".." 文字列がある場合、この関数は S_OK を返し、pszPathOut が指すバッファーには 1 つのバックスラッシュ "\" が格納されます。
パスの末尾のピリオドはすべて削除されます。ただし、"" ワイルドカード文字の直後にある場合を除きます。その場合、'' 文字の後に 1 つのピリオドが残され、それ以外の末尾のピリオドはすべて削除されます。
結果として得られるパスがルートドライブ ("x:") である場合は、バックスラッシュが追加されます ("x:\")。
この関数は、スラッシュ (/) をバックスラッシュ (\) に変換しません。信頼できない入力に対して、この関数単独では、パスをサブパスや同一性の比較が可能な形式に変換することはできません。その機能が必要な呼び出し元は、この関数を使用する前にスラッシュをバックスラッシュに変換してください。
次の例は、これらの文字列の効果を示しています。
| 元の文字列 | 正規化された文字列 |
|---|---|
| C:\name_1\.\name_2\..\name_3 | C:\name_1\name_3 |
| C:\name_1\..\name_2\.\name_3 | C:\name_2\name_3 |
| C:\name_1\name_2\.\name_3\..\name_4 | C:\name_1\name_2\name_4 |
| C:\name_1\.\name_2\.\name_3\..\name_4\.. | C:\name_1\name_2 |
| C:\name_1\*... | C:\name_1\*. |
| C:\.. | C:\ |
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>
HRESULT PathCchCanonicalize(
LPWSTR pszPathOut,
UINT_PTR cchPathOut,
LPCWSTR pszPathIn
);[DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling = true)]
static extern int PathCchCanonicalize(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPathOut, // LPWSTR out
UIntPtr cchPathOut, // UINT_PTR
[MarshalAs(UnmanagedType.LPWStr)] string pszPathIn // LPCWSTR
);<DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling:=True)>
Public Shared Function PathCchCanonicalize(
<MarshalAs(UnmanagedType.LPWStr)> pszPathOut As System.Text.StringBuilder, ' LPWSTR out
cchPathOut As UIntPtr, ' UINT_PTR
<MarshalAs(UnmanagedType.LPWStr)> pszPathIn As String ' LPCWSTR
) As Integer
End Function' pszPathOut : LPWSTR out
' cchPathOut : UINT_PTR
' pszPathIn : LPCWSTR
Declare PtrSafe Function PathCchCanonicalize Lib "api-ms-win-core-path-l1-1-0" ( _
ByVal pszPathOut As LongPtr, _
ByVal cchPathOut As LongPtr, _
ByVal pszPathIn As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathCchCanonicalize = ctypes.windll.LoadLibrary("api-ms-win-core-path-l1-1-0.dll").PathCchCanonicalize
PathCchCanonicalize.restype = ctypes.c_int
PathCchCanonicalize.argtypes = [
wintypes.LPWSTR, # pszPathOut : LPWSTR out
ctypes.c_size_t, # cchPathOut : UINT_PTR
wintypes.LPCWSTR, # pszPathIn : LPCWSTR
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('api-ms-win-core-path-l1-1-0.dll')
PathCchCanonicalize = Fiddle::Function.new(
lib['PathCchCanonicalize'],
[
Fiddle::TYPE_VOIDP, # pszPathOut : LPWSTR out
Fiddle::TYPE_UINTPTR_T, # cchPathOut : UINT_PTR
Fiddle::TYPE_VOIDP, # pszPathIn : LPCWSTR
],
Fiddle::TYPE_INT)#[link(name = "api-ms-win-core-path-l1-1-0")]
extern "system" {
fn PathCchCanonicalize(
pszPathOut: *mut u16, // LPWSTR out
cchPathOut: usize, // UINT_PTR
pszPathIn: *const u16 // LPCWSTR
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("api-ms-win-core-path-l1-1-0.dll")]
public static extern int PathCchCanonicalize([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPathOut, UIntPtr cchPathOut, [MarshalAs(UnmanagedType.LPWStr)] string pszPathIn);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-path-l1-1-0_PathCchCanonicalize' -Namespace Win32 -PassThru
# $api::PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn)#uselib "api-ms-win-core-path-l1-1-0.dll"
#func global PathCchCanonicalize "PathCchCanonicalize" sptr, sptr, sptr
; PathCchCanonicalize varptr(pszPathOut), cchPathOut, pszPathIn ; 戻り値は stat
; pszPathOut : LPWSTR out -> "sptr"
; cchPathOut : UINT_PTR -> "sptr"
; pszPathIn : LPCWSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCanonicalize "PathCchCanonicalize" var, sptr, wstr ; res = PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn) ; pszPathOut : LPWSTR out -> "var" ; cchPathOut : UINT_PTR -> "sptr" ; pszPathIn : LPCWSTR -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCanonicalize "PathCchCanonicalize" sptr, sptr, wstr ; res = PathCchCanonicalize(varptr(pszPathOut), cchPathOut, pszPathIn) ; pszPathOut : LPWSTR out -> "sptr" ; cchPathOut : UINT_PTR -> "sptr" ; pszPathIn : LPCWSTR -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; HRESULT PathCchCanonicalize(LPWSTR pszPathOut, UINT_PTR cchPathOut, LPCWSTR pszPathIn) #uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCanonicalize "PathCchCanonicalize" var, intptr, wstr ; res = PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn) ; pszPathOut : LPWSTR out -> "var" ; cchPathOut : UINT_PTR -> "intptr" ; pszPathIn : LPCWSTR -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT PathCchCanonicalize(LPWSTR pszPathOut, UINT_PTR cchPathOut, LPCWSTR pszPathIn) #uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCanonicalize "PathCchCanonicalize" intptr, intptr, wstr ; res = PathCchCanonicalize(varptr(pszPathOut), cchPathOut, pszPathIn) ; pszPathOut : LPWSTR out -> "intptr" ; cchPathOut : UINT_PTR -> "intptr" ; pszPathIn : LPCWSTR -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
api_ms_win_core_path_l1_1_0 = windows.NewLazySystemDLL("api-ms-win-core-path-l1-1-0.dll")
procPathCchCanonicalize = api_ms_win_core_path_l1_1_0.NewProc("PathCchCanonicalize")
)
// pszPathOut (LPWSTR out), cchPathOut (UINT_PTR), pszPathIn (LPCWSTR)
r1, _, err := procPathCchCanonicalize.Call(
uintptr(pszPathOut),
uintptr(cchPathOut),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszPathIn))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction PathCchCanonicalize(
pszPathOut: PWideChar; // LPWSTR out
cchPathOut: NativeUInt; // UINT_PTR
pszPathIn: PWideChar // LPCWSTR
): Integer; stdcall;
external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchCanonicalize';result := DllCall("api-ms-win-core-path-l1-1-0\PathCchCanonicalize"
, "Ptr", pszPathOut ; LPWSTR out
, "UPtr", cchPathOut ; UINT_PTR
, "WStr", pszPathIn ; LPCWSTR
, "Int") ; return: HRESULT●PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn) = DLL("api-ms-win-core-path-l1-1-0.dll", "int PathCchCanonicalize(char*, int, char*)")
# 呼び出し: PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn)
# pszPathOut : LPWSTR out -> "char*"
# cchPathOut : UINT_PTR -> "int"
# pszPathIn : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "api-ms-win-core-path-l1-1-0" fn PathCchCanonicalize(
pszPathOut: [*c]u16, // LPWSTR out
cchPathOut: usize, // UINT_PTR
pszPathIn: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) i32;proc PathCchCanonicalize(
pszPathOut: ptr uint16, # LPWSTR out
cchPathOut: uint, # UINT_PTR
pszPathIn: WideCString # LPCWSTR
): int32 {.importc: "PathCchCanonicalize", stdcall, dynlib: "api-ms-win-core-path-l1-1-0.dll".}pragma(lib, "api-ms-win-core-path-l1-1-0");
extern(Windows)
int PathCchCanonicalize(
wchar* pszPathOut, // LPWSTR out
size_t cchPathOut, // UINT_PTR
const(wchar)* pszPathIn // LPCWSTR
);ccall((:PathCchCanonicalize, "api-ms-win-core-path-l1-1-0.dll"), stdcall, Int32,
(Ptr{UInt16}, Csize_t, Cwstring),
pszPathOut, cchPathOut, pszPathIn)
# pszPathOut : LPWSTR out -> Ptr{UInt16}
# cchPathOut : UINT_PTR -> Csize_t
# pszPathIn : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathCchCanonicalize(
uint16_t* pszPathOut,
uintptr_t cchPathOut,
const uint16_t* pszPathIn);
]]
local api-ms-win-core-path-l1-1-0 = ffi.load("api-ms-win-core-path-l1-1-0")
-- api-ms-win-core-path-l1-1-0.PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn)
-- pszPathOut : LPWSTR out
-- cchPathOut : UINT_PTR
-- pszPathIn : LPCWSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-path-l1-1-0.dll');
const PathCchCanonicalize = lib.func('__stdcall', 'PathCchCanonicalize', 'int32_t', ['uint16_t *', 'uintptr_t', 'str16']);
// PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn)
// pszPathOut : LPWSTR out -> 'uint16_t *'
// cchPathOut : UINT_PTR -> 'uintptr_t'
// pszPathIn : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("api-ms-win-core-path-l1-1-0.dll", {
PathCchCanonicalize: { parameters: ["buffer", "usize", "buffer"], result: "i32" },
});
// lib.symbols.PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn)
// pszPathOut : LPWSTR out -> "buffer"
// cchPathOut : UINT_PTR -> "usize"
// pszPathIn : LPCWSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathCchCanonicalize(
uint16_t* pszPathOut,
size_t cchPathOut,
const uint16_t* pszPathIn);
C, "api-ms-win-core-path-l1-1-0.dll");
// $ffi->PathCchCanonicalize(pszPathOut, cchPathOut, pszPathIn);
// pszPathOut : LPWSTR out
// cchPathOut : UINT_PTR
// pszPathIn : LPCWSTR
// 構造体/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 Api-ms-win-core-path-l1-1-0 extends StdCallLibrary {
Api-ms-win-core-path-l1-1-0 INSTANCE = Native.load("api-ms-win-core-path-l1-1-0", Api-ms-win-core-path-l1-1-0.class);
int PathCchCanonicalize(
char[] pszPathOut, // LPWSTR out
long cchPathOut, // UINT_PTR
WString pszPathIn // LPCWSTR
);
}@[Link("api-ms-win-core-path-l1-1-0")]
lib Libapi-ms-win-core-path-l1-1-0
fun PathCchCanonicalize = PathCchCanonicalize(
pszPathOut : UInt16*, # LPWSTR out
cchPathOut : LibC::SizeT, # UINT_PTR
pszPathIn : UInt16* # LPCWSTR
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCchCanonicalizeNative = Int32 Function(Pointer<Utf16>, UintPtr, Pointer<Utf16>);
typedef PathCchCanonicalizeDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>);
final PathCchCanonicalize = DynamicLibrary.open('api-ms-win-core-path-l1-1-0.dll')
.lookupFunction<PathCchCanonicalizeNative, PathCchCanonicalizeDart>('PathCchCanonicalize');
// pszPathOut : LPWSTR out -> Pointer<Utf16>
// cchPathOut : UINT_PTR -> UintPtr
// pszPathIn : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCchCanonicalize(
pszPathOut: PWideChar; // LPWSTR out
cchPathOut: NativeUInt; // UINT_PTR
pszPathIn: PWideChar // LPCWSTR
): Integer; stdcall;
external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchCanonicalize';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCchCanonicalize"
c_PathCchCanonicalize :: CWString -> CUIntPtr -> CWString -> IO Int32
-- pszPathOut : LPWSTR out -> CWString
-- cchPathOut : UINT_PTR -> CUIntPtr
-- pszPathIn : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcchcanonicalize =
foreign "PathCchCanonicalize"
((ptr uint16_t) @-> size_t @-> (ptr uint16_t) @-> returning int32_t)
(* pszPathOut : LPWSTR out -> (ptr uint16_t) *)
(* cchPathOut : UINT_PTR -> size_t *)
(* pszPathIn : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library api-ms-win-core-path-l1-1-0 (t "api-ms-win-core-path-l1-1-0.dll"))
(cffi:use-foreign-library api-ms-win-core-path-l1-1-0)
(cffi:defcfun ("PathCchCanonicalize" path-cch-canonicalize :convention :stdcall) :int32
(psz-path-out :pointer) ; LPWSTR out
(cch-path-out :uint64) ; UINT_PTR
(psz-path-in (:string :encoding :utf-16le))) ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCchCanonicalize = Win32::API::More->new('api-ms-win-core-path-l1-1-0',
'int PathCchCanonicalize(LPWSTR pszPathOut, WPARAM cchPathOut, LPCWSTR pszPathIn)');
# my $ret = $PathCchCanonicalize->Call($pszPathOut, $cchPathOut, $pszPathIn);
# pszPathOut : LPWSTR out -> LPWSTR
# cchPathOut : UINT_PTR -> WPARAM
# pszPathIn : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f PathCchCanonicalizeEx — オプション指定でパスを正規化し冗長な要素を解決する。