PathCompactPathExA
関数指定の文字数に収まるよう、パスを省略記号付きで短縮する。
シグネチャ
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
BOOL PathCompactPathExA(
LPSTR pszOut,
LPCSTR pszSrc,
DWORD cchMax,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszOut | LPSTR | out | 変更後の文字列のアドレス。 |
| pszSrc | LPCSTR | in | 変更対象のパスを格納した、長さ MAX_PATH の null 終端文字列へのポインター。 |
| cchMax | DWORD | in | 終端の null 文字を含めて、新しい文字列に格納できる最大文字数。たとえば cchMax = 8 の場合、結果の文字列には最大 7 文字と終端の null 文字を格納できます。 |
| dwFlags | DWORD | in |
戻り値の型: BOOL
公式ドキュメント
パスコンポーネントを省略記号(...)に置き換えることで、パスを指定した文字数に収まるように切り詰めます。(ANSI)
戻り値
型: BOOL
成功した場合は TRUE を、それ以外の場合は FALSE を返します。
解説(Remarks)
元の文字列が '/' 区切り文字を使用していた場合は、'' の代わりに '/' が使用されます。pszSrc がパスではなく長すぎるファイル名を指している場合、そのファイル名は省略記号と終端の NULL 文字を含めて cchMax 文字に切り詰められます。たとえば、入力ファイル名が "My Filename" で cchMax が 10 の場合、PathCompactPathEx は "My Fil..." を返します。
メモ
shlwapi.h ヘッダーは PathCompactPathEx を、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 (ANSI / -A)
#include <windows.h>
BOOL PathCompactPathExA(
LPSTR pszOut,
LPCSTR pszSrc,
DWORD cchMax,
DWORD dwFlags
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool PathCompactPathExA(
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszOut, // LPSTR out
[MarshalAs(UnmanagedType.LPStr)] string pszSrc, // LPCSTR
uint cchMax, // DWORD
uint dwFlags // DWORD
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function PathCompactPathExA(
<MarshalAs(UnmanagedType.LPStr)> pszOut As System.Text.StringBuilder, ' LPSTR out
<MarshalAs(UnmanagedType.LPStr)> pszSrc As String, ' LPCSTR
cchMax As UInteger, ' DWORD
dwFlags As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pszOut : LPSTR out
' pszSrc : LPCSTR
' cchMax : DWORD
' dwFlags : DWORD
Declare PtrSafe Function PathCompactPathExA Lib "shlwapi" ( _
ByVal pszOut As String, _
ByVal pszSrc As String, _
ByVal cchMax As Long, _
ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathCompactPathExA = ctypes.windll.shlwapi.PathCompactPathExA
PathCompactPathExA.restype = wintypes.BOOL
PathCompactPathExA.argtypes = [
wintypes.LPSTR, # pszOut : LPSTR out
wintypes.LPCSTR, # pszSrc : LPCSTR
wintypes.DWORD, # cchMax : DWORD
wintypes.DWORD, # dwFlags : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathCompactPathExA = Fiddle::Function.new(
lib['PathCompactPathExA'],
[
Fiddle::TYPE_VOIDP, # pszOut : LPSTR out
Fiddle::TYPE_VOIDP, # pszSrc : LPCSTR
-Fiddle::TYPE_INT, # cchMax : DWORD
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_INT)#[link(name = "shlwapi")]
extern "system" {
fn PathCompactPathExA(
pszOut: *mut u8, // LPSTR out
pszSrc: *const u8, // LPCSTR
cchMax: u32, // DWORD
dwFlags: u32 // DWORD
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern bool PathCompactPathExA([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszOut, [MarshalAs(UnmanagedType.LPStr)] string pszSrc, uint cchMax, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathCompactPathExA' -Namespace Win32 -PassThru
# $api::PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags)#uselib "SHLWAPI.dll"
#func global PathCompactPathExA "PathCompactPathExA" sptr, sptr, sptr, sptr
; PathCompactPathExA varptr(pszOut), pszSrc, cchMax, dwFlags ; 戻り値は stat
; pszOut : LPSTR out -> "sptr"
; pszSrc : LPCSTR -> "sptr"
; cchMax : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathCompactPathExA "PathCompactPathExA" var, str, int, int ; res = PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags) ; pszOut : LPSTR out -> "var" ; pszSrc : LPCSTR -> "str" ; cchMax : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathCompactPathExA "PathCompactPathExA" sptr, str, int, int ; res = PathCompactPathExA(varptr(pszOut), pszSrc, cchMax, dwFlags) ; pszOut : LPSTR out -> "sptr" ; pszSrc : LPCSTR -> "str" ; cchMax : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL PathCompactPathExA(LPSTR pszOut, LPCSTR pszSrc, DWORD cchMax, DWORD dwFlags) #uselib "SHLWAPI.dll" #cfunc global PathCompactPathExA "PathCompactPathExA" var, str, int, int ; res = PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags) ; pszOut : LPSTR out -> "var" ; pszSrc : LPCSTR -> "str" ; cchMax : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL PathCompactPathExA(LPSTR pszOut, LPCSTR pszSrc, DWORD cchMax, DWORD dwFlags) #uselib "SHLWAPI.dll" #cfunc global PathCompactPathExA "PathCompactPathExA" intptr, str, int, int ; res = PathCompactPathExA(varptr(pszOut), pszSrc, cchMax, dwFlags) ; pszOut : LPSTR out -> "intptr" ; pszSrc : LPCSTR -> "str" ; cchMax : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathCompactPathExA = shlwapi.NewProc("PathCompactPathExA")
)
// pszOut (LPSTR out), pszSrc (LPCSTR), cchMax (DWORD), dwFlags (DWORD)
r1, _, err := procPathCompactPathExA.Call(
uintptr(pszOut),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszSrc))),
uintptr(cchMax),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction PathCompactPathExA(
pszOut: PAnsiChar; // LPSTR out
pszSrc: PAnsiChar; // LPCSTR
cchMax: DWORD; // DWORD
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'SHLWAPI.dll' name 'PathCompactPathExA';result := DllCall("SHLWAPI\PathCompactPathExA"
, "Ptr", pszOut ; LPSTR out
, "AStr", pszSrc ; LPCSTR
, "UInt", cchMax ; DWORD
, "UInt", dwFlags ; DWORD
, "Int") ; return: BOOL●PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags) = DLL("SHLWAPI.dll", "bool PathCompactPathExA(char*, char*, dword, dword)")
# 呼び出し: PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags)
# pszOut : LPSTR out -> "char*"
# pszSrc : LPCSTR -> "char*"
# cchMax : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn PathCompactPathExA(
pszOut: [*c]u8, // LPSTR out
pszSrc: [*c]const u8, // LPCSTR
cchMax: u32, // DWORD
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc PathCompactPathExA(
pszOut: ptr char, # LPSTR out
pszSrc: cstring, # LPCSTR
cchMax: uint32, # DWORD
dwFlags: uint32 # DWORD
): int32 {.importc: "PathCompactPathExA", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
int PathCompactPathExA(
char* pszOut, // LPSTR out
const(char)* pszSrc, // LPCSTR
uint cchMax, // DWORD
uint dwFlags // DWORD
);ccall((:PathCompactPathExA, "SHLWAPI.dll"), stdcall, Int32,
(Ptr{UInt8}, Cstring, UInt32, UInt32),
pszOut, pszSrc, cchMax, dwFlags)
# pszOut : LPSTR out -> Ptr{UInt8}
# pszSrc : LPCSTR -> Cstring
# cchMax : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathCompactPathExA(
char* pszOut,
const char* pszSrc,
uint32_t cchMax,
uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags)
-- pszOut : LPSTR out
-- pszSrc : LPCSTR
-- cchMax : DWORD
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathCompactPathExA = lib.func('__stdcall', 'PathCompactPathExA', 'int32_t', ['char *', 'str', 'uint32_t', 'uint32_t']);
// PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags)
// pszOut : LPSTR out -> 'char *'
// pszSrc : LPCSTR -> 'str'
// cchMax : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathCompactPathExA: { parameters: ["buffer", "buffer", "u32", "u32"], result: "i32" },
});
// lib.symbols.PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags)
// pszOut : LPSTR out -> "buffer"
// pszSrc : LPCSTR -> "buffer"
// cchMax : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathCompactPathExA(
char* pszOut,
const char* pszSrc,
uint32_t cchMax,
uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags);
// pszOut : LPSTR out
// pszSrc : LPCSTR
// cchMax : DWORD
// dwFlags : DWORD
// 構造体/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);
boolean PathCompactPathExA(
byte[] pszOut, // LPSTR out
String pszSrc, // LPCSTR
int cchMax, // DWORD
int dwFlags // DWORD
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun PathCompactPathExA = PathCompactPathExA(
pszOut : UInt8*, # LPSTR out
pszSrc : UInt8*, # LPCSTR
cchMax : UInt32, # DWORD
dwFlags : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCompactPathExANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>, Uint32, Uint32);
typedef PathCompactPathExADart = int Function(Pointer<Utf8>, Pointer<Utf8>, int, int);
final PathCompactPathExA = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathCompactPathExANative, PathCompactPathExADart>('PathCompactPathExA');
// pszOut : LPSTR out -> Pointer<Utf8>
// pszSrc : LPCSTR -> Pointer<Utf8>
// cchMax : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCompactPathExA(
pszOut: PAnsiChar; // LPSTR out
pszSrc: PAnsiChar; // LPCSTR
cchMax: DWORD; // DWORD
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'SHLWAPI.dll' name 'PathCompactPathExA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCompactPathExA"
c_PathCompactPathExA :: CString -> CString -> Word32 -> Word32 -> IO CInt
-- pszOut : LPSTR out -> CString
-- pszSrc : LPCSTR -> CString
-- cchMax : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcompactpathexa =
foreign "PathCompactPathExA"
(string @-> string @-> uint32_t @-> uint32_t @-> returning int32_t)
(* pszOut : LPSTR out -> string *)
(* pszSrc : LPCSTR -> string *)
(* cchMax : DWORD -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("PathCompactPathExA" path-compact-path-ex-a :convention :stdcall) :int32
(psz-out :pointer) ; LPSTR out
(psz-src :string) ; LPCSTR
(cch-max :uint32) ; DWORD
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCompactPathExA = Win32::API::More->new('SHLWAPI',
'BOOL PathCompactPathExA(LPSTR pszOut, LPCSTR pszSrc, DWORD cchMax, DWORD dwFlags)');
# my $ret = $PathCompactPathExA->Call($pszOut, $pszSrc, $cchMax, $dwFlags);
# pszOut : LPSTR out -> LPSTR
# pszSrc : LPCSTR -> LPCSTR
# cchMax : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f PathCompactPathExW (Unicode版) — 指定の文字数に収まるよう、パスを省略記号付きで短縮する。
類似 API
- f PathCompactPathA — 指定のピクセル幅に収まるよう、パス文字列を省略記号で短縮する。