PathCchCombine
関数2つのパス文字列を安全に連結して正規化する。
シグネチャ
// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>
HRESULT PathCchCombine(
LPWSTR pszPathOut,
UINT_PTR cchPathOut,
LPCWSTR pszPathIn, // optional
LPCWSTR pszMore // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszPathOut | LPWSTR | out | この関数が正常に終了したときに、結合されたパス文字列を受け取るバッファーへのポインター。このパラメーターは、pszPathIn または pszMore と同じバッファーを指すことができます。 |
| cchPathOut | UINT_PTR | in | pszPathOut が指すバッファーのサイズ(文字数)。 |
| pszPathIn | LPCWSTR | inoptional | 1 番目のパス文字列へのポインター。この値は NULL にできます。 |
| pszMore | LPCWSTR | inoptional | 2 番目のパス文字列へのポインター。このパスが単一のバックスラッシュで始まる場合は、pszPathIn が指すパスのルートとのみ結合されます。このパスが完全修飾されている場合は、もう一方のパスと結合されず、そのまま出力バッファーにコピーされます。この値は NULL にできます。 |
戻り値の型: HRESULT
公式ドキュメント
2 つのパスフラグメントを 1 つのパスに結合します。(PathCchCombine)
戻り値
この関数は、以下を含む HRESULT コードを返します。
| 戻りコード | 説明 |
|---|---|
| 関数は成功しました。これには、ピリオドの後に文字が続かない空の拡張子のような場合も含まれることに注意してください。その場合、元の文字列は変更されずに返されます。 | |
| この値は、pszPathOut パラメーターが NULL に設定されている場合や、cchPathOut の値が 0 または PATHCCH_MAX_CCH より大きい値に設定されている場合など、いくつかの要因によって発生する可能性があります。 | |
| 関数は、操作を実行するのに十分なメモリを割り当てることができませんでした。 | |
|
元のパスの一方または両方のサイズが PATHCCH_MAX_CCH を超えました。 |
解説(Remarks)
pszPathIn と pszMore の両方が NULL であるか空の文字列を指している場合は、単一のバックスラッシュが pszPathOut が指すバッファーにコピーされます。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>
HRESULT PathCchCombine(
LPWSTR pszPathOut,
UINT_PTR cchPathOut,
LPCWSTR pszPathIn, // optional
LPCWSTR pszMore // optional
);[DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling = true)]
static extern int PathCchCombine(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPathOut, // LPWSTR out
UIntPtr cchPathOut, // UINT_PTR
[MarshalAs(UnmanagedType.LPWStr)] string pszPathIn, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string pszMore // LPCWSTR optional
);<DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling:=True)>
Public Shared Function PathCchCombine(
<MarshalAs(UnmanagedType.LPWStr)> pszPathOut As System.Text.StringBuilder, ' LPWSTR out
cchPathOut As UIntPtr, ' UINT_PTR
<MarshalAs(UnmanagedType.LPWStr)> pszPathIn As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> pszMore As String ' LPCWSTR optional
) As Integer
End Function' pszPathOut : LPWSTR out
' cchPathOut : UINT_PTR
' pszPathIn : LPCWSTR optional
' pszMore : LPCWSTR optional
Declare PtrSafe Function PathCchCombine Lib "api-ms-win-core-path-l1-1-0" ( _
ByVal pszPathOut As LongPtr, _
ByVal cchPathOut As LongPtr, _
ByVal pszPathIn As LongPtr, _
ByVal pszMore As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathCchCombine = ctypes.windll.LoadLibrary("api-ms-win-core-path-l1-1-0.dll").PathCchCombine
PathCchCombine.restype = ctypes.c_int
PathCchCombine.argtypes = [
wintypes.LPWSTR, # pszPathOut : LPWSTR out
ctypes.c_size_t, # cchPathOut : UINT_PTR
wintypes.LPCWSTR, # pszPathIn : LPCWSTR optional
wintypes.LPCWSTR, # pszMore : LPCWSTR optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('api-ms-win-core-path-l1-1-0.dll')
PathCchCombine = Fiddle::Function.new(
lib['PathCchCombine'],
[
Fiddle::TYPE_VOIDP, # pszPathOut : LPWSTR out
Fiddle::TYPE_UINTPTR_T, # cchPathOut : UINT_PTR
Fiddle::TYPE_VOIDP, # pszPathIn : LPCWSTR optional
Fiddle::TYPE_VOIDP, # pszMore : LPCWSTR optional
],
Fiddle::TYPE_INT)#[link(name = "api-ms-win-core-path-l1-1-0")]
extern "system" {
fn PathCchCombine(
pszPathOut: *mut u16, // LPWSTR out
cchPathOut: usize, // UINT_PTR
pszPathIn: *const u16, // LPCWSTR optional
pszMore: *const u16 // LPCWSTR optional
) -> 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 PathCchCombine([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPathOut, UIntPtr cchPathOut, [MarshalAs(UnmanagedType.LPWStr)] string pszPathIn, [MarshalAs(UnmanagedType.LPWStr)] string pszMore);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-path-l1-1-0_PathCchCombine' -Namespace Win32 -PassThru
# $api::PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore)#uselib "api-ms-win-core-path-l1-1-0.dll"
#func global PathCchCombine "PathCchCombine" sptr, sptr, sptr, sptr
; PathCchCombine varptr(pszPathOut), cchPathOut, pszPathIn, pszMore ; 戻り値は stat
; pszPathOut : LPWSTR out -> "sptr"
; cchPathOut : UINT_PTR -> "sptr"
; pszPathIn : LPCWSTR optional -> "sptr"
; pszMore : LPCWSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCombine "PathCchCombine" var, sptr, wstr, wstr ; res = PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore) ; pszPathOut : LPWSTR out -> "var" ; cchPathOut : UINT_PTR -> "sptr" ; pszPathIn : LPCWSTR optional -> "wstr" ; pszMore : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCombine "PathCchCombine" sptr, sptr, wstr, wstr ; res = PathCchCombine(varptr(pszPathOut), cchPathOut, pszPathIn, pszMore) ; pszPathOut : LPWSTR out -> "sptr" ; cchPathOut : UINT_PTR -> "sptr" ; pszPathIn : LPCWSTR optional -> "wstr" ; pszMore : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT PathCchCombine(LPWSTR pszPathOut, UINT_PTR cchPathOut, LPCWSTR pszPathIn, LPCWSTR pszMore) #uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCombine "PathCchCombine" var, intptr, wstr, wstr ; res = PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore) ; pszPathOut : LPWSTR out -> "var" ; cchPathOut : UINT_PTR -> "intptr" ; pszPathIn : LPCWSTR optional -> "wstr" ; pszMore : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT PathCchCombine(LPWSTR pszPathOut, UINT_PTR cchPathOut, LPCWSTR pszPathIn, LPCWSTR pszMore) #uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchCombine "PathCchCombine" intptr, intptr, wstr, wstr ; res = PathCchCombine(varptr(pszPathOut), cchPathOut, pszPathIn, pszMore) ; pszPathOut : LPWSTR out -> "intptr" ; cchPathOut : UINT_PTR -> "intptr" ; pszPathIn : LPCWSTR optional -> "wstr" ; pszMore : LPCWSTR optional -> "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")
procPathCchCombine = api_ms_win_core_path_l1_1_0.NewProc("PathCchCombine")
)
// pszPathOut (LPWSTR out), cchPathOut (UINT_PTR), pszPathIn (LPCWSTR optional), pszMore (LPCWSTR optional)
r1, _, err := procPathCchCombine.Call(
uintptr(pszPathOut),
uintptr(cchPathOut),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszPathIn))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszMore))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction PathCchCombine(
pszPathOut: PWideChar; // LPWSTR out
cchPathOut: NativeUInt; // UINT_PTR
pszPathIn: PWideChar; // LPCWSTR optional
pszMore: PWideChar // LPCWSTR optional
): Integer; stdcall;
external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchCombine';result := DllCall("api-ms-win-core-path-l1-1-0\PathCchCombine"
, "Ptr", pszPathOut ; LPWSTR out
, "UPtr", cchPathOut ; UINT_PTR
, "WStr", pszPathIn ; LPCWSTR optional
, "WStr", pszMore ; LPCWSTR optional
, "Int") ; return: HRESULT●PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore) = DLL("api-ms-win-core-path-l1-1-0.dll", "int PathCchCombine(char*, int, char*, char*)")
# 呼び出し: PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore)
# pszPathOut : LPWSTR out -> "char*"
# cchPathOut : UINT_PTR -> "int"
# pszPathIn : LPCWSTR optional -> "char*"
# pszMore : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "api-ms-win-core-path-l1-1-0" fn PathCchCombine(
pszPathOut: [*c]u16, // LPWSTR out
cchPathOut: usize, // UINT_PTR
pszPathIn: [*c]const u16, // LPCWSTR optional
pszMore: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;proc PathCchCombine(
pszPathOut: ptr uint16, # LPWSTR out
cchPathOut: uint, # UINT_PTR
pszPathIn: WideCString, # LPCWSTR optional
pszMore: WideCString # LPCWSTR optional
): int32 {.importc: "PathCchCombine", stdcall, dynlib: "api-ms-win-core-path-l1-1-0.dll".}pragma(lib, "api-ms-win-core-path-l1-1-0");
extern(Windows)
int PathCchCombine(
wchar* pszPathOut, // LPWSTR out
size_t cchPathOut, // UINT_PTR
const(wchar)* pszPathIn, // LPCWSTR optional
const(wchar)* pszMore // LPCWSTR optional
);ccall((:PathCchCombine, "api-ms-win-core-path-l1-1-0.dll"), stdcall, Int32,
(Ptr{UInt16}, Csize_t, Cwstring, Cwstring),
pszPathOut, cchPathOut, pszPathIn, pszMore)
# pszPathOut : LPWSTR out -> Ptr{UInt16}
# cchPathOut : UINT_PTR -> Csize_t
# pszPathIn : LPCWSTR optional -> Cwstring
# pszMore : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathCchCombine(
uint16_t* pszPathOut,
uintptr_t cchPathOut,
const uint16_t* pszPathIn,
const uint16_t* pszMore);
]]
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.PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore)
-- pszPathOut : LPWSTR out
-- cchPathOut : UINT_PTR
-- pszPathIn : LPCWSTR optional
-- pszMore : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-path-l1-1-0.dll');
const PathCchCombine = lib.func('__stdcall', 'PathCchCombine', 'int32_t', ['uint16_t *', 'uintptr_t', 'str16', 'str16']);
// PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore)
// pszPathOut : LPWSTR out -> 'uint16_t *'
// cchPathOut : UINT_PTR -> 'uintptr_t'
// pszPathIn : LPCWSTR optional -> 'str16'
// pszMore : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("api-ms-win-core-path-l1-1-0.dll", {
PathCchCombine: { parameters: ["buffer", "usize", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore)
// pszPathOut : LPWSTR out -> "buffer"
// cchPathOut : UINT_PTR -> "usize"
// pszPathIn : LPCWSTR optional -> "buffer"
// pszMore : LPCWSTR optional -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathCchCombine(
uint16_t* pszPathOut,
size_t cchPathOut,
const uint16_t* pszPathIn,
const uint16_t* pszMore);
C, "api-ms-win-core-path-l1-1-0.dll");
// $ffi->PathCchCombine(pszPathOut, cchPathOut, pszPathIn, pszMore);
// pszPathOut : LPWSTR out
// cchPathOut : UINT_PTR
// pszPathIn : LPCWSTR optional
// pszMore : LPCWSTR optional
// 構造体/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 PathCchCombine(
char[] pszPathOut, // LPWSTR out
long cchPathOut, // UINT_PTR
WString pszPathIn, // LPCWSTR optional
WString pszMore // LPCWSTR optional
);
}@[Link("api-ms-win-core-path-l1-1-0")]
lib Libapi-ms-win-core-path-l1-1-0
fun PathCchCombine = PathCchCombine(
pszPathOut : UInt16*, # LPWSTR out
cchPathOut : LibC::SizeT, # UINT_PTR
pszPathIn : UInt16*, # LPCWSTR optional
pszMore : UInt16* # LPCWSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCchCombineNative = Int32 Function(Pointer<Utf16>, UintPtr, Pointer<Utf16>, Pointer<Utf16>);
typedef PathCchCombineDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Utf16>);
final PathCchCombine = DynamicLibrary.open('api-ms-win-core-path-l1-1-0.dll')
.lookupFunction<PathCchCombineNative, PathCchCombineDart>('PathCchCombine');
// pszPathOut : LPWSTR out -> Pointer<Utf16>
// cchPathOut : UINT_PTR -> UintPtr
// pszPathIn : LPCWSTR optional -> Pointer<Utf16>
// pszMore : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCchCombine(
pszPathOut: PWideChar; // LPWSTR out
cchPathOut: NativeUInt; // UINT_PTR
pszPathIn: PWideChar; // LPCWSTR optional
pszMore: PWideChar // LPCWSTR optional
): Integer; stdcall;
external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchCombine';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCchCombine"
c_PathCchCombine :: CWString -> CUIntPtr -> CWString -> CWString -> IO Int32
-- pszPathOut : LPWSTR out -> CWString
-- cchPathOut : UINT_PTR -> CUIntPtr
-- pszPathIn : LPCWSTR optional -> CWString
-- pszMore : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcchcombine =
foreign "PathCchCombine"
((ptr uint16_t) @-> size_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* pszPathOut : LPWSTR out -> (ptr uint16_t) *)
(* cchPathOut : UINT_PTR -> size_t *)
(* pszPathIn : LPCWSTR optional -> (ptr uint16_t) *)
(* pszMore : LPCWSTR optional -> (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 ("PathCchCombine" path-cch-combine :convention :stdcall) :int32
(psz-path-out :pointer) ; LPWSTR out
(cch-path-out :uint64) ; UINT_PTR
(psz-path-in (:string :encoding :utf-16le)) ; LPCWSTR optional
(psz-more (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCchCombine = Win32::API::More->new('api-ms-win-core-path-l1-1-0',
'int PathCchCombine(LPWSTR pszPathOut, WPARAM cchPathOut, LPCWSTR pszPathIn, LPCWSTR pszMore)');
# my $ret = $PathCchCombine->Call($pszPathOut, $cchPathOut, $pszPathIn, $pszMore);
# pszPathOut : LPWSTR out -> LPWSTR
# cchPathOut : UINT_PTR -> WPARAM
# pszPathIn : LPCWSTR optional -> LPCWSTR
# pszMore : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
類似 API
- f PathCchCombineEx — オプション指定で2つのパスを安全に連結し正規化する。
公式の関連項目
- f PathCchCanonicalize — パス内の相対要素を解決して正規形に変換する。
- f PathCchCanonicalizeEx — オプション指定でパスを正規化し冗長な要素を解決する。