PathCommonPrefixA
関数2つのパスに共通する先頭部分を求めて取得する。
シグネチャ
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
INT PathCommonPrefixA(
LPCSTR pszFile1,
LPCSTR pszFile2,
LPSTR achPath // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszFile1 | LPCSTR | in | 1 つ目のパス名を格納する、長さ MAX_PATH の null 終端文字列へのポインターです。 |
| pszFile2 | LPCSTR | in | 2 つ目のパス名を格納する、長さ MAX_PATH の null 終端文字列へのポインターです。 |
| achPath | LPSTR | outoptional | 共通のプレフィックスを受け取るバッファーへのポインターです。このバッファーのサイズは最低でも MAX_PATH 文字でなければなりません。共通のプレフィックスがない場合は NULL に設定されます。 |
戻り値の型: INT
公式ドキュメント
2 つのパスを比較して、共通のプレフィックスを持つかどうかを判定します。プレフィックスは次のいずれかの型です: "C:\"、"."、".."、"..\"。(ANSI)
戻り値
型: int
パス内の共通プレフィックス文字数を返します。出力バッファーのポインターが NULL でない場合、これらの文字が出力バッファーにコピーされます。
解説(Remarks)
メモ
shlwapi.h ヘッダーは PathCommonPrefix をエイリアスとして定義しており、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 (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 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathCommonPrefixA "PathCommonPrefixA" str, str, sptr ; res = PathCommonPrefixA(pszFile1, pszFile2, varptr(achPath)) ; pszFile1 : LPCSTR -> "str" ; pszFile2 : LPCSTR -> "str" ; achPath : LPSTR optional, out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; INT PathCommonPrefixA(LPCSTR pszFile1, LPCSTR pszFile2, LPSTR achPath) #uselib "SHLWAPI.dll" #cfunc global PathCommonPrefixA "PathCommonPrefixA" str, str, intptr ; res = PathCommonPrefixA(pszFile1, pszFile2, varptr(achPath)) ; pszFile1 : LPCSTR -> "str" ; pszFile2 : LPCSTR -> "str" ; achPath : LPSTR optional, out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // INTfunction 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)。const std = @import("std");
extern "shlwapi" fn PathCommonPrefixA(
pszFile1: [*c]const u8, // LPCSTR
pszFile2: [*c]const u8, // LPCSTR
achPath: [*c]u8 // LPSTR optional, out
) callconv(std.os.windows.WINAPI) i32;proc PathCommonPrefixA(
pszFile1: cstring, # LPCSTR
pszFile2: cstring, # LPCSTR
achPath: ptr char # LPSTR optional, out
): int32 {.importc: "PathCommonPrefixA", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
int PathCommonPrefixA(
const(char)* pszFile1, // LPCSTR
const(char)* pszFile2, // LPCSTR
char* achPath // LPSTR optional, out
);ccall((:PathCommonPrefixA, "SHLWAPI.dll"), stdcall, Int32,
(Cstring, Cstring, Ptr{UInt8}),
pszFile1, pszFile2, achPath)
# pszFile1 : LPCSTR -> Cstring
# pszFile2 : LPCSTR -> Cstring
# achPath : LPSTR optional, out -> Ptr{UInt8}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathCommonPrefixA(
const char* pszFile1,
const char* pszFile2,
char* achPath);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathCommonPrefixA(pszFile1, pszFile2, achPath)
-- pszFile1 : LPCSTR
-- pszFile2 : LPCSTR
-- achPath : LPSTR optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathCommonPrefixA = lib.func('__stdcall', 'PathCommonPrefixA', 'int32_t', ['str', 'str', 'char *']);
// PathCommonPrefixA(pszFile1, pszFile2, achPath)
// pszFile1 : LPCSTR -> 'str'
// pszFile2 : LPCSTR -> 'str'
// achPath : LPSTR optional, out -> 'char *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathCommonPrefixA: { parameters: ["buffer", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.PathCommonPrefixA(pszFile1, pszFile2, achPath)
// pszFile1 : LPCSTR -> "buffer"
// pszFile2 : LPCSTR -> "buffer"
// achPath : LPSTR optional, out -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathCommonPrefixA(
const char* pszFile1,
const char* pszFile2,
char* achPath);
C, "SHLWAPI.dll");
// $ffi->PathCommonPrefixA(pszFile1, pszFile2, achPath);
// pszFile1 : LPCSTR
// pszFile2 : LPCSTR
// achPath : LPSTR 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.ASCII_OPTIONS);
int PathCommonPrefixA(
String pszFile1, // LPCSTR
String pszFile2, // LPCSTR
byte[] achPath // LPSTR optional, out
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun PathCommonPrefixA = PathCommonPrefixA(
pszFile1 : UInt8*, # LPCSTR
pszFile2 : UInt8*, # LPCSTR
achPath : UInt8* # LPSTR 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 PathCommonPrefixANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
typedef PathCommonPrefixADart = int Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
final PathCommonPrefixA = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathCommonPrefixANative, PathCommonPrefixADart>('PathCommonPrefixA');
// pszFile1 : LPCSTR -> Pointer<Utf8>
// pszFile2 : LPCSTR -> Pointer<Utf8>
// achPath : LPSTR optional, out -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCommonPrefixA(
pszFile1: PAnsiChar; // LPCSTR
pszFile2: PAnsiChar; // LPCSTR
achPath: PAnsiChar // LPSTR optional, out
): Integer; stdcall;
external 'SHLWAPI.dll' name 'PathCommonPrefixA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCommonPrefixA"
c_PathCommonPrefixA :: CString -> CString -> CString -> IO Int32
-- pszFile1 : LPCSTR -> CString
-- pszFile2 : LPCSTR -> CString
-- achPath : LPSTR optional, out -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcommonprefixa =
foreign "PathCommonPrefixA"
(string @-> string @-> string @-> returning int32_t)
(* pszFile1 : LPCSTR -> string *)
(* pszFile2 : LPCSTR -> string *)
(* achPath : LPSTR optional, out -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("PathCommonPrefixA" path-common-prefix-a :convention :stdcall) :int32
(psz-file1 :string) ; LPCSTR
(psz-file2 :string) ; LPCSTR
(ach-path :pointer)) ; LPSTR optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCommonPrefixA = Win32::API::More->new('SHLWAPI',
'int PathCommonPrefixA(LPCSTR pszFile1, LPCSTR pszFile2, LPSTR achPath)');
# my $ret = $PathCommonPrefixA->Call($pszFile1, $pszFile2, $achPath);
# pszFile1 : LPCSTR -> LPCSTR
# pszFile2 : LPCSTR -> LPCSTR
# achPath : LPSTR optional, out -> LPSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f PathCommonPrefixW (Unicode版) — 2つのパスに共通する先頭部分を求めて取得する。