PathCombineA
関数ディレクトリとファイル名を結合して完全なパスを作る。
シグネチャ
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
LPSTR PathCombineA(
LPSTR pszDest,
LPCSTR pszDir, // optional
LPCSTR pszFile // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszDest | LPSTR | out | 関数が正常に終了したときに、連結後のパス文字列を受け取るバッファーへのポインターです。返される文字列を保持するのに十分な大きさを確保するため、このバッファーのサイズは MAX_PATH に設定する必要があります。 |
| pszDir | LPCSTR | inoptional | 最初のパスを格納する、最大長 MAX_PATH の null 終端文字列へのポインターです。この値は NULL でもかまいません。 |
| pszFile | LPCSTR | inoptional | 2 番目のパスを格納する、最大長 MAX_PATH の null 終端文字列へのポインターです。この値は NULL でもかまいません。 |
戻り値の型: LPSTR
公式ドキュメント
正しく構成された 2 つのパスを表す文字列を 1 つのパスに連結します。相対パス要素も連結します。(ANSI)
戻り値
型: LPTSTR
関数が正常に終了したときに、連結後のパス文字列を受け取るバッファーへのポインターです。これは pszPathOut が指す文字列と同じものです。関数が正常に終了しなかった場合、この値は NULL になります。
解説(Remarks)
ディレクトリパスは A:、B:、...、Z: の形式である必要があります。ファイルパスは、パスのファイル名部分を表す正しい形式である必要があります。ディレクトリパスがバックスラッシュで終わっている場合、そのバックスラッシュは保持されます。lpszDir と lpszFile はどちらも省略可能なパラメーターですが、両方を同時に NULL にすることはできない点に注意してください。
例
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Buffer to hold combined path.
char buffer_1[MAX_PATH] = "";
char *lpStr1;
lpStr1 = buffer_1;
// String for balance of path name.
char buffer_2[ ] = "One\\Two\\Three";
char *lpStr2;
lpStr2 = buffer_2;
// String for directory name.
char buffer_3[ ] = "C:";
char *lpStr3;
lpStr3 = buffer_3;
cout << "The file path to be combined is "
<< lpStr2 << endl;
cout << "The directory name path is "
<< lpStr3 << endl;
cout << "The combined path is "
<< PathCombine(lpStr1,lpStr3,lpStr2) << endl;
}
------------
INPUT:
------------
Path for directory part: "C:"
Path for file part: "One\Two\Three"
------------
OUTPUT:
------------
The file path to be combined is One\Two\Three
The directory name path is C:
The combined path is C:\One\Two\Three
メモ
shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして PathCombine を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不整合が生じてコンパイルエラーやランタイムエラーの原因となることがあります。詳細については、関数プロトタイプの規約 を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は 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>
LPSTR PathCombineA(
LPSTR pszDest,
LPCSTR pszDir, // optional
LPCSTR pszFile // optional
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr PathCombineA(
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszDest, // LPSTR out
[MarshalAs(UnmanagedType.LPStr)] string pszDir, // LPCSTR optional
[MarshalAs(UnmanagedType.LPStr)] string pszFile // LPCSTR optional
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function PathCombineA(
<MarshalAs(UnmanagedType.LPStr)> pszDest As System.Text.StringBuilder, ' LPSTR out
<MarshalAs(UnmanagedType.LPStr)> pszDir As String, ' LPCSTR optional
<MarshalAs(UnmanagedType.LPStr)> pszFile As String ' LPCSTR optional
) As IntPtr
End Function' pszDest : LPSTR out
' pszDir : LPCSTR optional
' pszFile : LPCSTR optional
Declare PtrSafe Function PathCombineA Lib "shlwapi" ( _
ByVal pszDest As String, _
ByVal pszDir As String, _
ByVal pszFile As String) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathCombineA = ctypes.windll.shlwapi.PathCombineA
PathCombineA.restype = wintypes.LPSTR
PathCombineA.argtypes = [
wintypes.LPSTR, # pszDest : LPSTR out
wintypes.LPCSTR, # pszDir : LPCSTR optional
wintypes.LPCSTR, # pszFile : LPCSTR optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathCombineA = Fiddle::Function.new(
lib['PathCombineA'],
[
Fiddle::TYPE_VOIDP, # pszDest : LPSTR out
Fiddle::TYPE_VOIDP, # pszDir : LPCSTR optional
Fiddle::TYPE_VOIDP, # pszFile : LPCSTR optional
],
Fiddle::TYPE_VOIDP)#[link(name = "shlwapi")]
extern "system" {
fn PathCombineA(
pszDest: *mut u8, // LPSTR out
pszDir: *const u8, // LPCSTR optional
pszFile: *const u8 // LPCSTR optional
) -> *mut u8;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr PathCombineA([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszDest, [MarshalAs(UnmanagedType.LPStr)] string pszDir, [MarshalAs(UnmanagedType.LPStr)] string pszFile);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathCombineA' -Namespace Win32 -PassThru
# $api::PathCombineA(pszDest, pszDir, pszFile)#uselib "SHLWAPI.dll"
#func global PathCombineA "PathCombineA" sptr, sptr, sptr
; PathCombineA varptr(pszDest), pszDir, pszFile ; 戻り値は stat
; pszDest : LPSTR out -> "sptr"
; pszDir : LPCSTR optional -> "sptr"
; pszFile : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathCombineA "PathCombineA" var, str, str ; res = PathCombineA(pszDest, pszDir, pszFile) ; pszDest : LPSTR out -> "var" ; pszDir : LPCSTR optional -> "str" ; pszFile : LPCSTR optional -> "str" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathCombineA "PathCombineA" sptr, str, str ; res = PathCombineA(varptr(pszDest), pszDir, pszFile) ; pszDest : LPSTR out -> "sptr" ; pszDir : LPCSTR optional -> "str" ; pszFile : LPCSTR optional -> "str" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; LPSTR PathCombineA(LPSTR pszDest, LPCSTR pszDir, LPCSTR pszFile) #uselib "SHLWAPI.dll" #cfunc global PathCombineA "PathCombineA" var, str, str ; res = PathCombineA(pszDest, pszDir, pszFile) ; pszDest : LPSTR out -> "var" ; pszDir : LPCSTR optional -> "str" ; pszFile : LPCSTR optional -> "str" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; LPSTR PathCombineA(LPSTR pszDest, LPCSTR pszDir, LPCSTR pszFile) #uselib "SHLWAPI.dll" #cfunc global PathCombineA "PathCombineA" intptr, str, str ; res = PathCombineA(varptr(pszDest), pszDir, pszFile) ; pszDest : LPSTR out -> "intptr" ; pszDir : LPCSTR optional -> "str" ; pszFile : LPCSTR optional -> "str" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathCombineA = shlwapi.NewProc("PathCombineA")
)
// pszDest (LPSTR out), pszDir (LPCSTR optional), pszFile (LPCSTR optional)
r1, _, err := procPathCombineA.Call(
uintptr(pszDest),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszDir))),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszFile))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPSTRfunction PathCombineA(
pszDest: PAnsiChar; // LPSTR out
pszDir: PAnsiChar; // LPCSTR optional
pszFile: PAnsiChar // LPCSTR optional
): PAnsiChar; stdcall;
external 'SHLWAPI.dll' name 'PathCombineA';result := DllCall("SHLWAPI\PathCombineA"
, "Ptr", pszDest ; LPSTR out
, "AStr", pszDir ; LPCSTR optional
, "AStr", pszFile ; LPCSTR optional
, "Ptr") ; return: LPSTR●PathCombineA(pszDest, pszDir, pszFile) = DLL("SHLWAPI.dll", "char* PathCombineA(char*, char*, char*)")
# 呼び出し: PathCombineA(pszDest, pszDir, pszFile)
# pszDest : LPSTR out -> "char*"
# pszDir : LPCSTR optional -> "char*"
# pszFile : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn PathCombineA(
pszDest: [*c]u8, // LPSTR out
pszDir: [*c]const u8, // LPCSTR optional
pszFile: [*c]const u8 // LPCSTR optional
) callconv(std.os.windows.WINAPI) [*c]const u8;proc PathCombineA(
pszDest: ptr char, # LPSTR out
pszDir: cstring, # LPCSTR optional
pszFile: cstring # LPCSTR optional
): cstring {.importc: "PathCombineA", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
const(char)* PathCombineA(
char* pszDest, // LPSTR out
const(char)* pszDir, // LPCSTR optional
const(char)* pszFile // LPCSTR optional
);ccall((:PathCombineA, "SHLWAPI.dll"), stdcall, Cstring,
(Ptr{UInt8}, Cstring, Cstring),
pszDest, pszDir, pszFile)
# pszDest : LPSTR out -> Ptr{UInt8}
# pszDir : LPCSTR optional -> Cstring
# pszFile : LPCSTR optional -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
const char* PathCombineA(
char* pszDest,
const char* pszDir,
const char* pszFile);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathCombineA(pszDest, pszDir, pszFile)
-- pszDest : LPSTR out
-- pszDir : LPCSTR optional
-- pszFile : LPCSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathCombineA = lib.func('__stdcall', 'PathCombineA', 'void *', ['char *', 'str', 'str']);
// PathCombineA(pszDest, pszDir, pszFile)
// pszDest : LPSTR out -> 'char *'
// pszDir : LPCSTR optional -> 'str'
// pszFile : LPCSTR optional -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathCombineA: { parameters: ["buffer", "buffer", "buffer"], result: "pointer" },
});
// lib.symbols.PathCombineA(pszDest, pszDir, pszFile)
// pszDest : LPSTR out -> "buffer"
// pszDir : LPCSTR optional -> "buffer"
// pszFile : LPCSTR optional -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const char* PathCombineA(
char* pszDest,
const char* pszDir,
const char* pszFile);
C, "SHLWAPI.dll");
// $ffi->PathCombineA(pszDest, pszDir, pszFile);
// pszDest : LPSTR out
// pszDir : LPCSTR optional
// pszFile : 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);
Pointer PathCombineA(
byte[] pszDest, // LPSTR out
String pszDir, // LPCSTR optional
String pszFile // LPCSTR optional
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun PathCombineA = PathCombineA(
pszDest : UInt8*, # LPSTR out
pszDir : UInt8*, # LPCSTR optional
pszFile : UInt8* # LPCSTR optional
) : UInt8*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCombineANative = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
typedef PathCombineADart = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
final PathCombineA = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathCombineANative, PathCombineADart>('PathCombineA');
// pszDest : LPSTR out -> Pointer<Utf8>
// pszDir : LPCSTR optional -> Pointer<Utf8>
// pszFile : LPCSTR optional -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCombineA(
pszDest: PAnsiChar; // LPSTR out
pszDir: PAnsiChar; // LPCSTR optional
pszFile: PAnsiChar // LPCSTR optional
): PAnsiChar; stdcall;
external 'SHLWAPI.dll' name 'PathCombineA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCombineA"
c_PathCombineA :: CString -> CString -> CString -> IO CString
-- pszDest : LPSTR out -> CString
-- pszDir : LPCSTR optional -> CString
-- pszFile : LPCSTR optional -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcombinea =
foreign "PathCombineA"
(string @-> string @-> string @-> returning string)
(* pszDest : LPSTR out -> string *)
(* pszDir : LPCSTR optional -> string *)
(* pszFile : 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 ("PathCombineA" path-combine-a :convention :stdcall) :string
(psz-dest :pointer) ; LPSTR out
(psz-dir :string) ; LPCSTR optional
(psz-file :string)) ; LPCSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCombineA = Win32::API::More->new('SHLWAPI',
'LPSTR PathCombineA(LPSTR pszDest, LPCSTR pszDir, LPCSTR pszFile)');
# my $ret = $PathCombineA->Call($pszDest, $pszDir, $pszFile);
# pszDest : LPSTR out -> LPSTR
# pszDir : LPCSTR optional -> LPCSTR
# pszFile : LPCSTR optional -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f PathCombineW (Unicode版) — ディレクトリパスとファイル名を連結し、正規化された1つのパスを生成する。