Win32 API 日本語リファレンス
ホームUI.Shell › PathCommonPrefixA

PathCommonPrefixA

関数
2つのパスに共通する先頭部分を求めて取得する。
DLLSHLWAPI.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// SHLWAPI.dll  (ANSI / -A)
#include <windows.h>

INT PathCommonPrefixA(
    LPCSTR pszFile1,
    LPCSTR pszFile2,
    LPSTR achPath   // optional
);

パラメーター

名前方向
pszFile1LPCSTRin
pszFile2LPCSTRin
achPathLPSTRoutoptional

戻り値の型: INT

各言語での呼び出し定義

// SHLWAPI.dll  (ANSI / -A)
#include <windows.h>

INT PathCommonPrefixA(
    LPCSTR pszFile1,
    LPCSTR pszFile2,
    LPSTR achPath   // optional
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int PathCommonPrefixA(
    [MarshalAs(UnmanagedType.LPStr)] string pszFile1,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string pszFile2,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder achPath   // LPSTR optional, out
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function PathCommonPrefixA(
    <MarshalAs(UnmanagedType.LPStr)> pszFile1 As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> pszFile2 As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> achPath As System.Text.StringBuilder   ' LPSTR optional, out
) As Integer
End Function
' pszFile1 : LPCSTR
' pszFile2 : LPCSTR
' achPath : LPSTR optional, out
Declare PtrSafe Function PathCommonPrefixA Lib "shlwapi" ( _
    ByVal pszFile1 As String, _
    ByVal pszFile2 As String, _
    ByVal achPath As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PathCommonPrefixA = ctypes.windll.shlwapi.PathCommonPrefixA
PathCommonPrefixA.restype = ctypes.c_int
PathCommonPrefixA.argtypes = [
    wintypes.LPCSTR,  # pszFile1 : LPCSTR
    wintypes.LPCSTR,  # pszFile2 : LPCSTR
    wintypes.LPSTR,  # achPath : LPSTR optional, out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
PathCommonPrefixA = Fiddle::Function.new(
  lib['PathCommonPrefixA'],
  [
    Fiddle::TYPE_VOIDP,  # pszFile1 : LPCSTR
    Fiddle::TYPE_VOIDP,  # pszFile2 : LPCSTR
    Fiddle::TYPE_VOIDP,  # achPath : LPSTR optional, out
  ],
  Fiddle::TYPE_INT)
#[link(name = "shlwapi")]
extern "system" {
    fn PathCommonPrefixA(
        pszFile1: *const u8,  // LPCSTR
        pszFile2: *const u8,  // LPCSTR
        achPath: *mut u8  // LPSTR optional, out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern int PathCommonPrefixA([MarshalAs(UnmanagedType.LPStr)] string pszFile1, [MarshalAs(UnmanagedType.LPStr)] string pszFile2, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder achPath);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathCommonPrefixA' -Namespace Win32 -PassThru
# $api::PathCommonPrefixA(pszFile1, pszFile2, achPath)
#uselib "SHLWAPI.dll"
#func global PathCommonPrefixA "PathCommonPrefixA" sptr, sptr, sptr
; PathCommonPrefixA pszFile1, pszFile2, varptr(achPath)   ; 戻り値は stat
; pszFile1 : LPCSTR -> "sptr"
; pszFile2 : LPCSTR -> "sptr"
; achPath : LPSTR optional, out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHLWAPI.dll"
#cfunc global PathCommonPrefixA "PathCommonPrefixA" str, str, var
; res = PathCommonPrefixA(pszFile1, pszFile2, achPath)
; pszFile1 : LPCSTR -> "str"
; pszFile2 : LPCSTR -> "str"
; achPath : LPSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT PathCommonPrefixA(LPCSTR pszFile1, LPCSTR pszFile2, LPSTR achPath)
#uselib "SHLWAPI.dll"
#cfunc global PathCommonPrefixA "PathCommonPrefixA" str, str, var
; res = PathCommonPrefixA(pszFile1, pszFile2, achPath)
; pszFile1 : LPCSTR -> "str"
; pszFile2 : LPCSTR -> "str"
; achPath : LPSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procPathCommonPrefixA = shlwapi.NewProc("PathCommonPrefixA")
)

// pszFile1 (LPCSTR), pszFile2 (LPCSTR), achPath (LPSTR optional, out)
r1, _, err := procPathCommonPrefixA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pszFile1))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pszFile2))),
	uintptr(achPath),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function PathCommonPrefixA(
  pszFile1: PAnsiChar;   // LPCSTR
  pszFile2: PAnsiChar;   // LPCSTR
  achPath: PAnsiChar   // LPSTR optional, out
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'PathCommonPrefixA';
result := DllCall("SHLWAPI\PathCommonPrefixA"
    , "AStr", pszFile1   ; LPCSTR
    , "AStr", pszFile2   ; LPCSTR
    , "Ptr", achPath   ; LPSTR optional, out
    , "Int")   ; return: INT
●PathCommonPrefixA(pszFile1, pszFile2, achPath) = DLL("SHLWAPI.dll", "int PathCommonPrefixA(char*, char*, char*)")
# 呼び出し: PathCommonPrefixA(pszFile1, pszFile2, achPath)
# pszFile1 : LPCSTR -> "char*"
# pszFile2 : LPCSTR -> "char*"
# achPath : LPSTR optional, out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。