PathCommonPrefixW
関数2つのパスに共通する先頭部分を求めて取得する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
INT PathCommonPrefixW(
LPCWSTR pszFile1,
LPCWSTR pszFile2,
LPWSTR achPath // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszFile1 | LPCWSTR | in | 1 つ目のパス名を格納した、長さ MAX_PATH の null 終端文字列へのポインターです。 |
| pszFile2 | LPCWSTR | in | 2 つ目のパス名を格納した、長さ MAX_PATH の null 終端文字列へのポインターです。 |
| achPath | LPWSTR | outoptional | 共通のプレフィックスを受け取るバッファーへのポインターです。このバッファーのサイズは少なくとも MAX_PATH 文字分必要です。共通のプレフィックスが存在しない場合は NULL が設定されます。 |
戻り値の型: INT
公式ドキュメント
2 つのパスを比較し、共通のプレフィックスを持つかどうかを判定します。プレフィックスとは次のいずれかの種類です: "C:\"、"."、".."、"..\"。(Unicode)
戻り値
型: int
パス内の共通プレフィックス文字数を返します。出力バッファーのポインターが NULL でない場合、これらの文字は出力バッファーにコピーされます。
解説(Remarks)
メモ
shlwapi.h ヘッダーは PathCommonPrefix をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在させて使用すると、不一致が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規約 を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は 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>
INT PathCommonPrefixW(
LPCWSTR pszFile1,
LPCWSTR pszFile2,
LPWSTR achPath // optional
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int PathCommonPrefixW(
[MarshalAs(UnmanagedType.LPWStr)] string pszFile1, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string pszFile2, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder achPath // LPWSTR optional, out
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PathCommonPrefixW(
<MarshalAs(UnmanagedType.LPWStr)> pszFile1 As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> pszFile2 As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> achPath As System.Text.StringBuilder ' LPWSTR optional, out
) As Integer
End Function' pszFile1 : LPCWSTR
' pszFile2 : LPCWSTR
' achPath : LPWSTR optional, out
Declare PtrSafe Function PathCommonPrefixW Lib "shlwapi" ( _
ByVal pszFile1 As LongPtr, _
ByVal pszFile2 As LongPtr, _
ByVal achPath As LongPtr) As Long
' 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
PathCommonPrefixW = ctypes.windll.shlwapi.PathCommonPrefixW
PathCommonPrefixW.restype = ctypes.c_int
PathCommonPrefixW.argtypes = [
wintypes.LPCWSTR, # pszFile1 : LPCWSTR
wintypes.LPCWSTR, # pszFile2 : LPCWSTR
wintypes.LPWSTR, # achPath : LPWSTR optional, out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathCommonPrefixW = Fiddle::Function.new(
lib['PathCommonPrefixW'],
[
Fiddle::TYPE_VOIDP, # pszFile1 : LPCWSTR
Fiddle::TYPE_VOIDP, # pszFile2 : LPCWSTR
Fiddle::TYPE_VOIDP, # achPath : LPWSTR optional, out
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn PathCommonPrefixW(
pszFile1: *const u16, // LPCWSTR
pszFile2: *const u16, // LPCWSTR
achPath: *mut u16 // LPWSTR optional, out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern int PathCommonPrefixW([MarshalAs(UnmanagedType.LPWStr)] string pszFile1, [MarshalAs(UnmanagedType.LPWStr)] string pszFile2, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder achPath);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathCommonPrefixW' -Namespace Win32 -PassThru
# $api::PathCommonPrefixW(pszFile1, pszFile2, achPath)#uselib "SHLWAPI.dll"
#func global PathCommonPrefixW "PathCommonPrefixW" wptr, wptr, wptr
; PathCommonPrefixW pszFile1, pszFile2, varptr(achPath) ; 戻り値は stat
; pszFile1 : LPCWSTR -> "wptr"
; pszFile2 : LPCWSTR -> "wptr"
; achPath : LPWSTR optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathCommonPrefixW "PathCommonPrefixW" wstr, wstr, var ; res = PathCommonPrefixW(pszFile1, pszFile2, achPath) ; pszFile1 : LPCWSTR -> "wstr" ; pszFile2 : LPCWSTR -> "wstr" ; achPath : LPWSTR optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathCommonPrefixW "PathCommonPrefixW" wstr, wstr, sptr ; res = PathCommonPrefixW(pszFile1, pszFile2, varptr(achPath)) ; pszFile1 : LPCWSTR -> "wstr" ; pszFile2 : LPCWSTR -> "wstr" ; achPath : LPWSTR optional, out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; INT PathCommonPrefixW(LPCWSTR pszFile1, LPCWSTR pszFile2, LPWSTR achPath) #uselib "SHLWAPI.dll" #cfunc global PathCommonPrefixW "PathCommonPrefixW" wstr, wstr, var ; res = PathCommonPrefixW(pszFile1, pszFile2, achPath) ; pszFile1 : LPCWSTR -> "wstr" ; pszFile2 : LPCWSTR -> "wstr" ; achPath : LPWSTR optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT PathCommonPrefixW(LPCWSTR pszFile1, LPCWSTR pszFile2, LPWSTR achPath) #uselib "SHLWAPI.dll" #cfunc global PathCommonPrefixW "PathCommonPrefixW" wstr, wstr, intptr ; res = PathCommonPrefixW(pszFile1, pszFile2, varptr(achPath)) ; pszFile1 : LPCWSTR -> "wstr" ; pszFile2 : LPCWSTR -> "wstr" ; achPath : LPWSTR optional, out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathCommonPrefixW = shlwapi.NewProc("PathCommonPrefixW")
)
// pszFile1 (LPCWSTR), pszFile2 (LPCWSTR), achPath (LPWSTR optional, out)
r1, _, err := procPathCommonPrefixW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFile1))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFile2))),
uintptr(achPath),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction PathCommonPrefixW(
pszFile1: PWideChar; // LPCWSTR
pszFile2: PWideChar; // LPCWSTR
achPath: PWideChar // LPWSTR optional, out
): Integer; stdcall;
external 'SHLWAPI.dll' name 'PathCommonPrefixW';result := DllCall("SHLWAPI\PathCommonPrefixW"
, "WStr", pszFile1 ; LPCWSTR
, "WStr", pszFile2 ; LPCWSTR
, "Ptr", achPath ; LPWSTR optional, out
, "Int") ; return: INT●PathCommonPrefixW(pszFile1, pszFile2, achPath) = DLL("SHLWAPI.dll", "int PathCommonPrefixW(char*, char*, char*)")
# 呼び出し: PathCommonPrefixW(pszFile1, pszFile2, achPath)
# pszFile1 : LPCWSTR -> "char*"
# pszFile2 : LPCWSTR -> "char*"
# achPath : LPWSTR optional, out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shlwapi" fn PathCommonPrefixW(
pszFile1: [*c]const u16, // LPCWSTR
pszFile2: [*c]const u16, // LPCWSTR
achPath: [*c]u16 // LPWSTR optional, out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc PathCommonPrefixW(
pszFile1: WideCString, # LPCWSTR
pszFile2: WideCString, # LPCWSTR
achPath: ptr uint16 # LPWSTR optional, out
): int32 {.importc: "PathCommonPrefixW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shlwapi");
extern(Windows)
int PathCommonPrefixW(
const(wchar)* pszFile1, // LPCWSTR
const(wchar)* pszFile2, // LPCWSTR
wchar* achPath // LPWSTR optional, out
);ccall((:PathCommonPrefixW, "SHLWAPI.dll"), stdcall, Int32,
(Cwstring, Cwstring, Ptr{UInt16}),
pszFile1, pszFile2, achPath)
# pszFile1 : LPCWSTR -> Cwstring
# pszFile2 : LPCWSTR -> Cwstring
# achPath : LPWSTR optional, out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t PathCommonPrefixW(
const uint16_t* pszFile1,
const uint16_t* pszFile2,
uint16_t* achPath);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathCommonPrefixW(pszFile1, pszFile2, achPath)
-- pszFile1 : LPCWSTR
-- pszFile2 : LPCWSTR
-- achPath : LPWSTR optional, out
-- 構造体/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 PathCommonPrefixW = lib.func('__stdcall', 'PathCommonPrefixW', 'int32_t', ['str16', 'str16', 'uint16_t *']);
// PathCommonPrefixW(pszFile1, pszFile2, achPath)
// pszFile1 : LPCWSTR -> 'str16'
// pszFile2 : LPCWSTR -> 'str16'
// achPath : LPWSTR optional, out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathCommonPrefixW: { parameters: ["buffer", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.PathCommonPrefixW(pszFile1, pszFile2, achPath)
// pszFile1 : LPCWSTR -> "buffer"
// pszFile2 : LPCWSTR -> "buffer"
// achPath : LPWSTR optional, out -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathCommonPrefixW(
const uint16_t* pszFile1,
const uint16_t* pszFile2,
uint16_t* achPath);
C, "SHLWAPI.dll");
// $ffi->PathCommonPrefixW(pszFile1, pszFile2, achPath);
// pszFile1 : LPCWSTR
// pszFile2 : LPCWSTR
// achPath : LPWSTR optional, out
// 構造体/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);
int PathCommonPrefixW(
WString pszFile1, // LPCWSTR
WString pszFile2, // LPCWSTR
char[] achPath // LPWSTR optional, out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shlwapi")]
lib LibSHLWAPI
fun PathCommonPrefixW = PathCommonPrefixW(
pszFile1 : UInt16*, # LPCWSTR
pszFile2 : UInt16*, # LPCWSTR
achPath : UInt16* # LPWSTR optional, out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCommonPrefixWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef PathCommonPrefixWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final PathCommonPrefixW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathCommonPrefixWNative, PathCommonPrefixWDart>('PathCommonPrefixW');
// pszFile1 : LPCWSTR -> Pointer<Utf16>
// pszFile2 : LPCWSTR -> Pointer<Utf16>
// achPath : LPWSTR optional, out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCommonPrefixW(
pszFile1: PWideChar; // LPCWSTR
pszFile2: PWideChar; // LPCWSTR
achPath: PWideChar // LPWSTR optional, out
): Integer; stdcall;
external 'SHLWAPI.dll' name 'PathCommonPrefixW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCommonPrefixW"
c_PathCommonPrefixW :: CWString -> CWString -> CWString -> IO Int32
-- pszFile1 : LPCWSTR -> CWString
-- pszFile2 : LPCWSTR -> CWString
-- achPath : LPWSTR optional, out -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcommonprefixw =
foreign "PathCommonPrefixW"
((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* pszFile1 : LPCWSTR -> (ptr uint16_t) *)
(* pszFile2 : LPCWSTR -> (ptr uint16_t) *)
(* achPath : LPWSTR optional, out -> (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 ("PathCommonPrefixW" path-common-prefix-w :convention :stdcall) :int32
(psz-file1 (:string :encoding :utf-16le)) ; LPCWSTR
(psz-file2 (:string :encoding :utf-16le)) ; LPCWSTR
(ach-path :pointer)) ; LPWSTR optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCommonPrefixW = Win32::API::More->new('SHLWAPI',
'int PathCommonPrefixW(LPCWSTR pszFile1, LPCWSTR pszFile2, LPWSTR achPath)');
# my $ret = $PathCommonPrefixW->Call($pszFile1, $pszFile2, $achPath);
# pszFile1 : LPCWSTR -> LPCWSTR
# pszFile2 : LPCWSTR -> LPCWSTR
# achPath : LPWSTR optional, out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f PathCommonPrefixA (ANSI版) — 2つのパスに共通する先頭部分を求めて取得する。