PathCommonPrefixW
関数2つのパスに共通する先頭部分を求めて取得する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
INT PathCommonPrefixW(
LPCWSTR pszFile1,
LPCWSTR pszFile2,
LPWSTR achPath // optional
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| pszFile1 | LPCWSTR | in |
| pszFile2 | LPCWSTR | in |
| achPath | LPWSTR | outoptional |
戻り値の型: INT
各言語での呼び出し定義
// 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 版の利用を推奨。