Win32 API 日本語リファレンス
ホームUI.Shell › PathCchAddExtension

PathCchAddExtension

関数
パスに指定の拡張子を追加する。
DLLapi-ms-win-core-path-l1-1-0.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>

HRESULT PathCchAddExtension(
    LPWSTR pszPath,
    UINT_PTR cchPath,
    LPCWSTR pszExt
);

パラメーター

名前方向説明
pszPathLPWSTRinout

パス文字列へのポインター。この関数が正常に終了すると、バッファーには拡張子が追加された文字列が格納されます。この値は NULL であってはなりません。

注意 元の文字列に既にファイル名拡張子が存在する場合、新しい拡張子は追加されず、元の文字列は変更されません。
cchPathUINT_PTRinpszPath が指すバッファーのサイズ(文字数単位)。
pszExtLPCWSTRinファイル名拡張子文字列へのポインター。この文字列は、先頭にピリオドを付けても付けなくても指定できます(".ext" または "ext")。

戻り値の型: HRESULT

公式ドキュメント

パス文字列にファイル名拡張子を追加します。この関数は、"\"、"\?&quot;、"\?\UNC&quot; のプレフィックスを持つパスを受け付ける点で PathAddExtension とは異なります。

戻り値

この関数は、以下を含む HRESULT コードを返します。

戻り値 説明
S_OK
関数が成功しました。これには、ピリオドの後に文字が続かない場合など、拡張子が空である場合も含まれることに注意してください。その場合、元の文字列はそのまま返されます。
E_INVALIDARG
この値は、pszPath 引数が NULL に設定されている、cchPath が 0 または PATHCCH_MAX_CCH より大きい値に設定されている、拡張子文字列に不正な文字が含まれているか、それ以外の理由で有効な拡張子でない、などのいくつかの原因により発生する可能性があります。
S_FALSE
元の文字列には既に拡張子が存在します。
PATHCCH_E_FILENAME_TOO_LONG
バッファーが小さすぎて、返される文字列を格納できません。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>

HRESULT PathCchAddExtension(
    LPWSTR pszPath,
    UINT_PTR cchPath,
    LPCWSTR pszExt
);
[DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling = true)]
static extern int PathCchAddExtension(
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath,   // LPWSTR in/out
    UIntPtr cchPath,   // UINT_PTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszExt   // LPCWSTR
);
<DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling:=True)>
Public Shared Function PathCchAddExtension(
    <MarshalAs(UnmanagedType.LPWStr)> pszPath As System.Text.StringBuilder,   ' LPWSTR in/out
    cchPath As UIntPtr,   ' UINT_PTR
    <MarshalAs(UnmanagedType.LPWStr)> pszExt As String   ' LPCWSTR
) As Integer
End Function
' pszPath : LPWSTR in/out
' cchPath : UINT_PTR
' pszExt : LPCWSTR
Declare PtrSafe Function PathCchAddExtension Lib "api-ms-win-core-path-l1-1-0" ( _
    ByVal pszPath As LongPtr, _
    ByVal cchPath As LongPtr, _
    ByVal pszExt As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PathCchAddExtension = ctypes.windll.LoadLibrary("api-ms-win-core-path-l1-1-0.dll").PathCchAddExtension
PathCchAddExtension.restype = ctypes.c_int
PathCchAddExtension.argtypes = [
    wintypes.LPWSTR,  # pszPath : LPWSTR in/out
    ctypes.c_size_t,  # cchPath : UINT_PTR
    wintypes.LPCWSTR,  # pszExt : LPCWSTR
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('api-ms-win-core-path-l1-1-0.dll')
PathCchAddExtension = Fiddle::Function.new(
  lib['PathCchAddExtension'],
  [
    Fiddle::TYPE_VOIDP,  # pszPath : LPWSTR in/out
    Fiddle::TYPE_UINTPTR_T,  # cchPath : UINT_PTR
    Fiddle::TYPE_VOIDP,  # pszExt : LPCWSTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "api-ms-win-core-path-l1-1-0")]
extern "system" {
    fn PathCchAddExtension(
        pszPath: *mut u16,  // LPWSTR in/out
        cchPath: usize,  // UINT_PTR
        pszExt: *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 PathCchAddExtension([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, UIntPtr cchPath, [MarshalAs(UnmanagedType.LPWStr)] string pszExt);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-path-l1-1-0_PathCchAddExtension' -Namespace Win32 -PassThru
# $api::PathCchAddExtension(pszPath, cchPath, pszExt)
#uselib "api-ms-win-core-path-l1-1-0.dll"
#func global PathCchAddExtension "PathCchAddExtension" sptr, sptr, sptr
; PathCchAddExtension varptr(pszPath), cchPath, pszExt   ; 戻り値は stat
; pszPath : LPWSTR in/out -> "sptr"
; cchPath : UINT_PTR -> "sptr"
; pszExt : LPCWSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "api-ms-win-core-path-l1-1-0.dll"
#cfunc global PathCchAddExtension "PathCchAddExtension" var, sptr, wstr
; res = PathCchAddExtension(pszPath, cchPath, pszExt)
; pszPath : LPWSTR in/out -> "var"
; cchPath : UINT_PTR -> "sptr"
; pszExt : LPCWSTR -> "wstr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT PathCchAddExtension(LPWSTR pszPath, UINT_PTR cchPath, LPCWSTR pszExt)
#uselib "api-ms-win-core-path-l1-1-0.dll"
#cfunc global PathCchAddExtension "PathCchAddExtension" var, intptr, wstr
; res = PathCchAddExtension(pszPath, cchPath, pszExt)
; pszPath : LPWSTR in/out -> "var"
; cchPath : UINT_PTR -> "intptr"
; pszExt : LPCWSTR -> "wstr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。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")
	procPathCchAddExtension = api_ms_win_core_path_l1_1_0.NewProc("PathCchAddExtension")
)

// pszPath (LPWSTR in/out), cchPath (UINT_PTR), pszExt (LPCWSTR)
r1, _, err := procPathCchAddExtension.Call(
	uintptr(pszPath),
	uintptr(cchPath),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszExt))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function PathCchAddExtension(
  pszPath: PWideChar;   // LPWSTR in/out
  cchPath: NativeUInt;   // UINT_PTR
  pszExt: PWideChar   // LPCWSTR
): Integer; stdcall;
  external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchAddExtension';
result := DllCall("api-ms-win-core-path-l1-1-0\PathCchAddExtension"
    , "Ptr", pszPath   ; LPWSTR in/out
    , "UPtr", cchPath   ; UINT_PTR
    , "WStr", pszExt   ; LPCWSTR
    , "Int")   ; return: HRESULT
●PathCchAddExtension(pszPath, cchPath, pszExt) = DLL("api-ms-win-core-path-l1-1-0.dll", "int PathCchAddExtension(char*, int, char*)")
# 呼び出し: PathCchAddExtension(pszPath, cchPath, pszExt)
# pszPath : LPWSTR in/out -> "char*"
# cchPath : UINT_PTR -> "int"
# pszExt : 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 PathCchAddExtension(
    pszPath: [*c]u16, // LPWSTR in/out
    cchPath: usize, // UINT_PTR
    pszExt: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) i32;
proc PathCchAddExtension(
    pszPath: ptr uint16,  # LPWSTR in/out
    cchPath: uint,  # UINT_PTR
    pszExt: WideCString  # LPCWSTR
): int32 {.importc: "PathCchAddExtension", stdcall, dynlib: "api-ms-win-core-path-l1-1-0.dll".}
pragma(lib, "api-ms-win-core-path-l1-1-0");
extern(Windows)
int PathCchAddExtension(
    wchar* pszPath,   // LPWSTR in/out
    size_t cchPath,   // UINT_PTR
    const(wchar)* pszExt   // LPCWSTR
);
ccall((:PathCchAddExtension, "api-ms-win-core-path-l1-1-0.dll"), stdcall, Int32,
      (Ptr{UInt16}, Csize_t, Cwstring),
      pszPath, cchPath, pszExt)
# pszPath : LPWSTR in/out -> Ptr{UInt16}
# cchPath : UINT_PTR -> Csize_t
# pszExt : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t PathCchAddExtension(
    uint16_t* pszPath,
    uintptr_t cchPath,
    const uint16_t* pszExt);
]]
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.PathCchAddExtension(pszPath, cchPath, pszExt)
-- pszPath : LPWSTR in/out
-- cchPath : UINT_PTR
-- pszExt : 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 PathCchAddExtension = lib.func('__stdcall', 'PathCchAddExtension', 'int32_t', ['uint16_t *', 'uintptr_t', 'str16']);
// PathCchAddExtension(pszPath, cchPath, pszExt)
// pszPath : LPWSTR in/out -> 'uint16_t *'
// cchPath : UINT_PTR -> 'uintptr_t'
// pszExt : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("api-ms-win-core-path-l1-1-0.dll", {
  PathCchAddExtension: { parameters: ["buffer", "usize", "buffer"], result: "i32" },
});
// lib.symbols.PathCchAddExtension(pszPath, cchPath, pszExt)
// pszPath : LPWSTR in/out -> "buffer"
// cchPath : UINT_PTR -> "usize"
// pszExt : LPCWSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t PathCchAddExtension(
    uint16_t* pszPath,
    size_t cchPath,
    const uint16_t* pszExt);
C, "api-ms-win-core-path-l1-1-0.dll");
// $ffi->PathCchAddExtension(pszPath, cchPath, pszExt);
// pszPath : LPWSTR in/out
// cchPath : UINT_PTR
// pszExt : 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 PathCchAddExtension(
        char[] pszPath,   // LPWSTR in/out
        long cchPath,   // UINT_PTR
        WString pszExt   // LPCWSTR
    );
}
@[Link("api-ms-win-core-path-l1-1-0")]
lib Libapi-ms-win-core-path-l1-1-0
  fun PathCchAddExtension = PathCchAddExtension(
    pszPath : UInt16*,   # LPWSTR in/out
    cchPath : LibC::SizeT,   # UINT_PTR
    pszExt : 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 PathCchAddExtensionNative = Int32 Function(Pointer<Utf16>, UintPtr, Pointer<Utf16>);
typedef PathCchAddExtensionDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>);
final PathCchAddExtension = DynamicLibrary.open('api-ms-win-core-path-l1-1-0.dll')
    .lookupFunction<PathCchAddExtensionNative, PathCchAddExtensionDart>('PathCchAddExtension');
// pszPath : LPWSTR in/out -> Pointer<Utf16>
// cchPath : UINT_PTR -> UintPtr
// pszExt : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PathCchAddExtension(
  pszPath: PWideChar;   // LPWSTR in/out
  cchPath: NativeUInt;   // UINT_PTR
  pszExt: PWideChar   // LPCWSTR
): Integer; stdcall;
  external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchAddExtension';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PathCchAddExtension"
  c_PathCchAddExtension :: CWString -> CUIntPtr -> CWString -> IO Int32
-- pszPath : LPWSTR in/out -> CWString
-- cchPath : UINT_PTR -> CUIntPtr
-- pszExt : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pathcchaddextension =
  foreign "PathCchAddExtension"
    ((ptr uint16_t) @-> size_t @-> (ptr uint16_t) @-> returning int32_t)
(* pszPath : LPWSTR in/out -> (ptr uint16_t) *)
(* cchPath : UINT_PTR -> size_t *)
(* pszExt : 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 ("PathCchAddExtension" path-cch-add-extension :convention :stdcall) :int32
  (psz-path :pointer)   ; LPWSTR in/out
  (cch-path :uint64)   ; UINT_PTR
  (psz-ext (:string :encoding :utf-16le)))   ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PathCchAddExtension = Win32::API::More->new('api-ms-win-core-path-l1-1-0',
    'int PathCchAddExtension(LPWSTR pszPath, WPARAM cchPath, LPCWSTR pszExt)');
# my $ret = $PathCchAddExtension->Call($pszPath, $cchPath, $pszExt);
# pszPath : LPWSTR in/out -> LPWSTR
# cchPath : UINT_PTR -> WPARAM
# pszExt : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。