PathCombineW
関数ディレクトリパスとファイル名を連結し、正規化された1つのパスを生成する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
LPWSTR PathCombineW(
LPWSTR pszDest,
LPCWSTR pszDir, // optional
LPCWSTR pszFile // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszDest | LPWSTR | out | この関数が正常に終了したときに、連結後のパス文字列を受け取るバッファーへのポインターです。返される文字列を確実に格納できるよう、このバッファーのサイズは MAX_PATH に設定する必要があります。 |
| pszDir | LPCWSTR | inoptional | 最初のパスを格納する、最大長 MAX_PATH の null 終端文字列へのポインターです。この値は NULL でもかまいません。 |
| pszFile | LPCWSTR | inoptional | 2 番目のパスを格納する、最大長 MAX_PATH の null 終端文字列へのポインターです。この値は NULL でもかまいません。 |
戻り値の型: LPWSTR
公式ドキュメント
正しく形成された 2 つのパス文字列を 1 つのパスに連結します。相対パス要素も連結します。(Unicode)
戻り値
型: LPTSTR
この関数が正常に終了したときに、連結後のパス文字列を受け取るバッファーへのポインターです。これは pszPathOut が指す文字列と同じものです。この関数が正常に終了しなかった場合、この値は NULL になります。
解説(Remarks)
ディレクトリパスは A:、B:、...、Z: の形式で指定してください。ファイルパスは、パスのファイル名部分を表す正しい形式である必要があります。ディレクトリパスがバックスラッシュで終わっている場合、そのバックスラッシュは保持されます。lpszDir と lpszFile はどちらも省略可能なパラメーターですが、両方を同時に NULL にすることはできない点に注意してください。
例
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
int 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 ヘッダーは PathCombine を、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 (Unicode / -W)
#include <windows.h>
LPWSTR PathCombineW(
LPWSTR pszDest,
LPCWSTR pszDir, // optional
LPCWSTR pszFile // optional
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr PathCombineW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDest, // LPWSTR out
[MarshalAs(UnmanagedType.LPWStr)] string pszDir, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string pszFile // LPCWSTR optional
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PathCombineW(
<MarshalAs(UnmanagedType.LPWStr)> pszDest As System.Text.StringBuilder, ' LPWSTR out
<MarshalAs(UnmanagedType.LPWStr)> pszDir As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> pszFile As String ' LPCWSTR optional
) As IntPtr
End Function' pszDest : LPWSTR out
' pszDir : LPCWSTR optional
' pszFile : LPCWSTR optional
Declare PtrSafe Function PathCombineW Lib "shlwapi" ( _
ByVal pszDest As LongPtr, _
ByVal pszDir As LongPtr, _
ByVal pszFile As LongPtr) As LongPtr
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathCombineW = ctypes.windll.shlwapi.PathCombineW
PathCombineW.restype = wintypes.LPWSTR
PathCombineW.argtypes = [
wintypes.LPWSTR, # pszDest : LPWSTR out
wintypes.LPCWSTR, # pszDir : LPCWSTR optional
wintypes.LPCWSTR, # pszFile : LPCWSTR optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathCombineW = Fiddle::Function.new(
lib['PathCombineW'],
[
Fiddle::TYPE_VOIDP, # pszDest : LPWSTR out
Fiddle::TYPE_VOIDP, # pszDir : LPCWSTR optional
Fiddle::TYPE_VOIDP, # pszFile : LPCWSTR optional
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn PathCombineW(
pszDest: *mut u16, // LPWSTR out
pszDir: *const u16, // LPCWSTR optional
pszFile: *const u16 // LPCWSTR optional
) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr PathCombineW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDest, [MarshalAs(UnmanagedType.LPWStr)] string pszDir, [MarshalAs(UnmanagedType.LPWStr)] string pszFile);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathCombineW' -Namespace Win32 -PassThru
# $api::PathCombineW(pszDest, pszDir, pszFile)#uselib "SHLWAPI.dll"
#func global PathCombineW "PathCombineW" wptr, wptr, wptr
; PathCombineW varptr(pszDest), pszDir, pszFile ; 戻り値は stat
; pszDest : LPWSTR out -> "wptr"
; pszDir : LPCWSTR optional -> "wptr"
; pszFile : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathCombineW "PathCombineW" var, wstr, wstr ; res = PathCombineW(pszDest, pszDir, pszFile) ; pszDest : LPWSTR out -> "var" ; pszDir : LPCWSTR optional -> "wstr" ; pszFile : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathCombineW "PathCombineW" sptr, wstr, wstr ; res = PathCombineW(varptr(pszDest), pszDir, pszFile) ; pszDest : LPWSTR out -> "sptr" ; pszDir : LPCWSTR optional -> "wstr" ; pszFile : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; LPWSTR PathCombineW(LPWSTR pszDest, LPCWSTR pszDir, LPCWSTR pszFile) #uselib "SHLWAPI.dll" #cfunc global PathCombineW "PathCombineW" var, wstr, wstr ; res = PathCombineW(pszDest, pszDir, pszFile) ; pszDest : LPWSTR out -> "var" ; pszDir : LPCWSTR optional -> "wstr" ; pszFile : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; LPWSTR PathCombineW(LPWSTR pszDest, LPCWSTR pszDir, LPCWSTR pszFile) #uselib "SHLWAPI.dll" #cfunc global PathCombineW "PathCombineW" intptr, wstr, wstr ; res = PathCombineW(varptr(pszDest), pszDir, pszFile) ; pszDest : LPWSTR out -> "intptr" ; pszDir : LPCWSTR optional -> "wstr" ; pszFile : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathCombineW = shlwapi.NewProc("PathCombineW")
)
// pszDest (LPWSTR out), pszDir (LPCWSTR optional), pszFile (LPCWSTR optional)
r1, _, err := procPathCombineW.Call(
uintptr(pszDest),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszDir))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFile))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPWSTRfunction PathCombineW(
pszDest: PWideChar; // LPWSTR out
pszDir: PWideChar; // LPCWSTR optional
pszFile: PWideChar // LPCWSTR optional
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'PathCombineW';result := DllCall("SHLWAPI\PathCombineW"
, "Ptr", pszDest ; LPWSTR out
, "WStr", pszDir ; LPCWSTR optional
, "WStr", pszFile ; LPCWSTR optional
, "Ptr") ; return: LPWSTR●PathCombineW(pszDest, pszDir, pszFile) = DLL("SHLWAPI.dll", "char* PathCombineW(char*, char*, char*)")
# 呼び出し: PathCombineW(pszDest, pszDir, pszFile)
# pszDest : LPWSTR out -> "char*"
# pszDir : LPCWSTR optional -> "char*"
# pszFile : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shlwapi" fn PathCombineW(
pszDest: [*c]u16, // LPWSTR out
pszDir: [*c]const u16, // LPCWSTR optional
pszFile: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) [*c]const u16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc PathCombineW(
pszDest: ptr uint16, # LPWSTR out
pszDir: WideCString, # LPCWSTR optional
pszFile: WideCString # LPCWSTR optional
): WideCString {.importc: "PathCombineW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* PathCombineW(
wchar* pszDest, // LPWSTR out
const(wchar)* pszDir, // LPCWSTR optional
const(wchar)* pszFile // LPCWSTR optional
);ccall((:PathCombineW, "SHLWAPI.dll"), stdcall, Cwstring,
(Ptr{UInt16}, Cwstring, Cwstring),
pszDest, pszDir, pszFile)
# pszDest : LPWSTR out -> Ptr{UInt16}
# pszDir : LPCWSTR optional -> Cwstring
# pszFile : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
const uint16_t* PathCombineW(
uint16_t* pszDest,
const uint16_t* pszDir,
const uint16_t* pszFile);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathCombineW(pszDest, pszDir, pszFile)
-- pszDest : LPWSTR out
-- pszDir : LPCWSTR optional
-- pszFile : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathCombineW = lib.func('__stdcall', 'PathCombineW', 'void *', ['uint16_t *', 'str16', 'str16']);
// PathCombineW(pszDest, pszDir, pszFile)
// pszDest : LPWSTR out -> 'uint16_t *'
// pszDir : LPCWSTR optional -> 'str16'
// pszFile : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathCombineW: { parameters: ["buffer", "buffer", "buffer"], result: "pointer" },
});
// lib.symbols.PathCombineW(pszDest, pszDir, pszFile)
// pszDest : LPWSTR out -> "buffer"
// pszDir : LPCWSTR optional -> "buffer"
// pszFile : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const uint16_t* PathCombineW(
uint16_t* pszDest,
const uint16_t* pszDir,
const uint16_t* pszFile);
C, "SHLWAPI.dll");
// $ffi->PathCombineW(pszDest, pszDir, pszFile);
// pszDest : LPWSTR out
// pszDir : LPCWSTR optional
// pszFile : 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 Shlwapi extends StdCallLibrary {
Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.UNICODE_OPTIONS);
Pointer PathCombineW(
char[] pszDest, // LPWSTR out
WString pszDir, // LPCWSTR optional
WString pszFile // LPCWSTR optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shlwapi")]
lib LibSHLWAPI
fun PathCombineW = PathCombineW(
pszDest : UInt16*, # LPWSTR out
pszDir : UInt16*, # LPCWSTR optional
pszFile : UInt16* # LPCWSTR optional
) : UInt16*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCombineWNative = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef PathCombineWDart = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final PathCombineW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathCombineWNative, PathCombineWDart>('PathCombineW');
// pszDest : LPWSTR out -> Pointer<Utf16>
// pszDir : LPCWSTR optional -> Pointer<Utf16>
// pszFile : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCombineW(
pszDest: PWideChar; // LPWSTR out
pszDir: PWideChar; // LPCWSTR optional
pszFile: PWideChar // LPCWSTR optional
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'PathCombineW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCombineW"
c_PathCombineW :: CWString -> CWString -> CWString -> IO CWString
-- pszDest : LPWSTR out -> CWString
-- pszDir : LPCWSTR optional -> CWString
-- pszFile : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcombinew =
foreign "PathCombineW"
((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning (ptr uint16_t))
(* pszDest : LPWSTR out -> (ptr uint16_t) *)
(* pszDir : LPCWSTR optional -> (ptr uint16_t) *)
(* pszFile : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("PathCombineW" path-combine-w :convention :stdcall) (:string :encoding :utf-16le)
(psz-dest :pointer) ; LPWSTR out
(psz-dir (:string :encoding :utf-16le)) ; LPCWSTR optional
(psz-file (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCombineW = Win32::API::More->new('SHLWAPI',
'LPWSTR PathCombineW(LPWSTR pszDest, LPCWSTR pszDir, LPCWSTR pszFile)');
# my $ret = $PathCombineW->Call($pszDest, $pszDir, $pszFile);
# pszDest : LPWSTR out -> LPWSTR
# pszDir : LPCWSTR optional -> LPCWSTR
# pszFile : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f PathCombineA (ANSI版) — ディレクトリとファイル名を結合して完全なパスを作る。