PathAddExtensionA
関数パスに指定した拡張子を付加する。
シグネチャ
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
BOOL PathAddExtensionA(
LPSTR pszPath,
LPCSTR pszExt // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszPath | LPSTR | inout | ファイル名拡張子が追加される、null 終端された文字列を含むバッファーへのポインターです。返される文字列を格納するのに十分な大きさを確保するため、このバッファーのサイズは MAX_PATH に設定する必要があります。 |
| pszExt | LPCSTR | inoptional | ファイル名拡張子を含む、null 終端された文字列へのポインターです。この値は NULL でもかまいません。 |
戻り値の型: BOOL
公式ドキュメント
パス文字列にファイル名拡張子を追加します。(ANSI)
戻り値
型: BOOL
拡張子が追加された場合は TRUE を、それ以外の場合は FALSE を返します。
解説(Remarks)
既にファイル名拡張子が存在する場合、拡張子は追加されません。pszPath が NULL 文字列を指している場合、結果はファイル名拡張子のみになります。pszExtension が NULL 文字列を指している場合、".exe" 拡張子が追加されます。
使用例
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String for path name without file name extension.
char buffer_1[MAX_PATH] = "file";
char *lpStr1;
lpStr1 = buffer_1;
// String for path name with file name extension.
char buffer_2[ ] = "file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// String for extension name.
char F_Ext[MAX_PATH] = ".txt";
char *lpStr3;
lpStr3 = F_Ext;
// Null string as path.
char N_String[MAX_PATH] = "\0";
char *lpStr4;
lpStr4 = N_String;
// Path 1 without the file name extension.
cout << "The original path string 1 is " << lpStr1 << endl;
int ret_1 = PathAddExtension(lpStr1,lpStr3);
cout << "The modified path string 1 is " << lpStr1 << endl;
// Path 2 with the file name extension already there.
cout << "The original path string 2 is " << lpStr2 << endl;
int ret_2 = PathAddExtension(lpStr2,lpStr3);
cout << "The modified path string 2 is " << lpStr2<< endl;
// Path 3 null string as a path.
int ret_3 = PathAddExtension(lpStr4,lpStr3);
cout << "The return value is " << ret_3<< endl;
cout << "The modified path on a null string is " << lpStr4<< endl;
}
OUTPUT:
-----------------------
The original path string 1 is file
The modified path string 1 is file.txt
The original path string 2 is file.doc
The modified path string 2 is file.doc
The return value is 1
The modified path on a null string is .txt
The return value is 1
メモ
shlwapi.h ヘッダーは PathAddExtension を、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)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
BOOL PathAddExtensionA(
LPSTR pszPath,
LPCSTR pszExt // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool PathAddExtensionA(
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszPath, // LPSTR in/out
[MarshalAs(UnmanagedType.LPStr)] string pszExt // LPCSTR optional
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function PathAddExtensionA(
<MarshalAs(UnmanagedType.LPStr)> pszPath As System.Text.StringBuilder, ' LPSTR in/out
<MarshalAs(UnmanagedType.LPStr)> pszExt As String ' LPCSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pszPath : LPSTR in/out
' pszExt : LPCSTR optional
Declare PtrSafe Function PathAddExtensionA Lib "shlwapi" ( _
ByVal pszPath As String, _
ByVal pszExt As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathAddExtensionA = ctypes.windll.shlwapi.PathAddExtensionA
PathAddExtensionA.restype = wintypes.BOOL
PathAddExtensionA.argtypes = [
wintypes.LPSTR, # pszPath : LPSTR in/out
wintypes.LPCSTR, # pszExt : LPCSTR optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathAddExtensionA = Fiddle::Function.new(
lib['PathAddExtensionA'],
[
Fiddle::TYPE_VOIDP, # pszPath : LPSTR in/out
Fiddle::TYPE_VOIDP, # pszExt : LPCSTR optional
],
Fiddle::TYPE_INT)#[link(name = "shlwapi")]
extern "system" {
fn PathAddExtensionA(
pszPath: *mut u8, // LPSTR in/out
pszExt: *const u8 // LPCSTR optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern bool PathAddExtensionA([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszPath, [MarshalAs(UnmanagedType.LPStr)] string pszExt);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathAddExtensionA' -Namespace Win32 -PassThru
# $api::PathAddExtensionA(pszPath, pszExt)#uselib "SHLWAPI.dll"
#func global PathAddExtensionA "PathAddExtensionA" sptr, sptr
; PathAddExtensionA varptr(pszPath), pszExt ; 戻り値は stat
; pszPath : LPSTR in/out -> "sptr"
; pszExt : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathAddExtensionA "PathAddExtensionA" var, str ; res = PathAddExtensionA(pszPath, pszExt) ; pszPath : LPSTR in/out -> "var" ; pszExt : LPCSTR optional -> "str" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathAddExtensionA "PathAddExtensionA" sptr, str ; res = PathAddExtensionA(varptr(pszPath), pszExt) ; pszPath : LPSTR in/out -> "sptr" ; pszExt : LPCSTR optional -> "str" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL PathAddExtensionA(LPSTR pszPath, LPCSTR pszExt) #uselib "SHLWAPI.dll" #cfunc global PathAddExtensionA "PathAddExtensionA" var, str ; res = PathAddExtensionA(pszPath, pszExt) ; pszPath : LPSTR in/out -> "var" ; pszExt : LPCSTR optional -> "str" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL PathAddExtensionA(LPSTR pszPath, LPCSTR pszExt) #uselib "SHLWAPI.dll" #cfunc global PathAddExtensionA "PathAddExtensionA" intptr, str ; res = PathAddExtensionA(varptr(pszPath), pszExt) ; pszPath : LPSTR in/out -> "intptr" ; pszExt : LPCSTR optional -> "str" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathAddExtensionA = shlwapi.NewProc("PathAddExtensionA")
)
// pszPath (LPSTR in/out), pszExt (LPCSTR optional)
r1, _, err := procPathAddExtensionA.Call(
uintptr(pszPath),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszExt))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction PathAddExtensionA(
pszPath: PAnsiChar; // LPSTR in/out
pszExt: PAnsiChar // LPCSTR optional
): BOOL; stdcall;
external 'SHLWAPI.dll' name 'PathAddExtensionA';result := DllCall("SHLWAPI\PathAddExtensionA"
, "Ptr", pszPath ; LPSTR in/out
, "AStr", pszExt ; LPCSTR optional
, "Int") ; return: BOOL●PathAddExtensionA(pszPath, pszExt) = DLL("SHLWAPI.dll", "bool PathAddExtensionA(char*, char*)")
# 呼び出し: PathAddExtensionA(pszPath, pszExt)
# pszPath : LPSTR in/out -> "char*"
# pszExt : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn PathAddExtensionA(
pszPath: [*c]u8, // LPSTR in/out
pszExt: [*c]const u8 // LPCSTR optional
) callconv(std.os.windows.WINAPI) i32;proc PathAddExtensionA(
pszPath: ptr char, # LPSTR in/out
pszExt: cstring # LPCSTR optional
): int32 {.importc: "PathAddExtensionA", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
int PathAddExtensionA(
char* pszPath, // LPSTR in/out
const(char)* pszExt // LPCSTR optional
);ccall((:PathAddExtensionA, "SHLWAPI.dll"), stdcall, Int32,
(Ptr{UInt8}, Cstring),
pszPath, pszExt)
# pszPath : LPSTR in/out -> Ptr{UInt8}
# pszExt : LPCSTR optional -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathAddExtensionA(
char* pszPath,
const char* pszExt);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathAddExtensionA(pszPath, pszExt)
-- pszPath : LPSTR in/out
-- pszExt : LPCSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathAddExtensionA = lib.func('__stdcall', 'PathAddExtensionA', 'int32_t', ['char *', 'str']);
// PathAddExtensionA(pszPath, pszExt)
// pszPath : LPSTR in/out -> 'char *'
// pszExt : LPCSTR optional -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathAddExtensionA: { parameters: ["buffer", "buffer"], result: "i32" },
});
// lib.symbols.PathAddExtensionA(pszPath, pszExt)
// pszPath : LPSTR in/out -> "buffer"
// pszExt : LPCSTR optional -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathAddExtensionA(
char* pszPath,
const char* pszExt);
C, "SHLWAPI.dll");
// $ffi->PathAddExtensionA(pszPath, pszExt);
// pszPath : LPSTR in/out
// pszExt : LPCSTR 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 Shlwapi extends StdCallLibrary {
Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.ASCII_OPTIONS);
boolean PathAddExtensionA(
byte[] pszPath, // LPSTR in/out
String pszExt // LPCSTR optional
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun PathAddExtensionA = PathAddExtensionA(
pszPath : UInt8*, # LPSTR in/out
pszExt : UInt8* # LPCSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathAddExtensionANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>);
typedef PathAddExtensionADart = int Function(Pointer<Utf8>, Pointer<Utf8>);
final PathAddExtensionA = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathAddExtensionANative, PathAddExtensionADart>('PathAddExtensionA');
// pszPath : LPSTR in/out -> Pointer<Utf8>
// pszExt : LPCSTR optional -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathAddExtensionA(
pszPath: PAnsiChar; // LPSTR in/out
pszExt: PAnsiChar // LPCSTR optional
): BOOL; stdcall;
external 'SHLWAPI.dll' name 'PathAddExtensionA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathAddExtensionA"
c_PathAddExtensionA :: CString -> CString -> IO CInt
-- pszPath : LPSTR in/out -> CString
-- pszExt : LPCSTR optional -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathaddextensiona =
foreign "PathAddExtensionA"
(string @-> string @-> returning int32_t)
(* pszPath : LPSTR in/out -> string *)
(* pszExt : LPCSTR optional -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("PathAddExtensionA" path-add-extension-a :convention :stdcall) :int32
(psz-path :pointer) ; LPSTR in/out
(psz-ext :string)) ; LPCSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathAddExtensionA = Win32::API::More->new('SHLWAPI',
'BOOL PathAddExtensionA(LPSTR pszPath, LPCSTR pszExt)');
# my $ret = $PathAddExtensionA->Call($pszPath, $pszExt);
# pszPath : LPSTR in/out -> LPSTR
# pszExt : LPCSTR optional -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f PathAddExtensionW (Unicode版) — パスに指定した拡張子を付加する。