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

PathCleanupSpec

関数
ファイル名から無効な文字を除去し有効な名前に整える。
DLLSHELL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHELL32.dll
#include <windows.h>

INT PathCleanupSpec(
    LPCWSTR pszDir,   // optional
    LPWSTR pszSpec
);

パラメーター

名前方向説明
pszDirLPCWSTRinoptional

pszSpec で指定されたファイルまたはディレクトリを格納するディレクトリの完全修飾パスを含む、null 終端バッファーへのポインターです。パスの長さは、終端の null 文字を含めて MAX_PATH 文字を超えてはなりません。このパスは変更されません。

この値は NULL にすることができます。

pszSpecLPWSTRinout

クリーンアップ対象のファイル名またはディレクトリ名を含む、null 終端バッファーへのポインターです。ファイルの場合は、その拡張子も含めてください。'' は無効な文字とみなされて削除されるため、このバッファーには 1 階層を超える深さのパスを含めることができない点に注意してください。

戻り時には、クリーンアップ済みの名前を含む null 終端文字列がこのバッファーに格納されます。

バッファーオーバーランの可能性を回避するため、このバッファーは少なくとも MAX_PATH 文字の長さを確保してください。

戻り値の型: INT

公式ドキュメント

PathCleanupSpec は変更されるか、利用できなくなる可能性があります。

戻り値

型: int

次の値のうち 1 つ以上を返します。

戻り値 説明
PCS_REPLACEDCHAR
1 つ以上の無効な文字を置換しました。
PCS_REMOVEDCHAR
1 つ以上の無効な文字を削除しました。
PCS_TRUNCATED
返されるパスが切り詰められました。
PCS_PATHTOOLONG
pszDir で指定された入力パスが長すぎ、pszSpec から有効なファイル名を生成できなかったため、関数が失敗しました。このフラグが返される場合は、常に PCS_FATAL フラグも伴います。
PCS_FATAL
クリーンアップ後のパスが有効なファイル名ではありません。このフラグは常に PCS_PATHTOOLONG と組み合わせて返されます。

解説(Remarks)

次の文字は、すべての名前において無効な文字とみなされます。

\ / : * ? " < > |

制御文字も無効とみなされます。長いファイル名がサポートされていない場合は、セミコロン (;) およびコンマ (,) の文字も無効です。

pszDir で指定されたドライブのファイルシステムが長いファイル名をサポートしているかどうかが確認されます。サポートしていない場合、pszSpec の名前は 8.3 形式に切り詰められ、PCS_TRUNCATED 値が返されます。pszDirNULL の場合は、Windows がインストールされているドライブを使用して長いファイル名のサポートが判定されます。

完全なパス、すなわち pszDir のパスの文字数と pszSpec のクリーンアップ済みの名前の文字数の合計が、MAX_PATH – 1 (終端の null 文字を考慮) を超える場合、関数は PCS_PATHTOOLONG を返します。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// SHELL32.dll
#include <windows.h>

INT PathCleanupSpec(
    LPCWSTR pszDir,   // optional
    LPWSTR pszSpec
);
[DllImport("SHELL32.dll", ExactSpelling = true)]
static extern int PathCleanupSpec(
    [MarshalAs(UnmanagedType.LPWStr)] string pszDir,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszSpec   // LPWSTR in/out
);
<DllImport("SHELL32.dll", ExactSpelling:=True)>
Public Shared Function PathCleanupSpec(
    <MarshalAs(UnmanagedType.LPWStr)> pszDir As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> pszSpec As System.Text.StringBuilder   ' LPWSTR in/out
) As Integer
End Function
' pszDir : LPCWSTR optional
' pszSpec : LPWSTR in/out
Declare PtrSafe Function PathCleanupSpec Lib "shell32" ( _
    ByVal pszDir As LongPtr, _
    ByVal pszSpec As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PathCleanupSpec = ctypes.windll.shell32.PathCleanupSpec
PathCleanupSpec.restype = ctypes.c_int
PathCleanupSpec.argtypes = [
    wintypes.LPCWSTR,  # pszDir : LPCWSTR optional
    wintypes.LPWSTR,  # pszSpec : LPWSTR in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procPathCleanupSpec = shell32.NewProc("PathCleanupSpec")
)

// pszDir (LPCWSTR optional), pszSpec (LPWSTR in/out)
r1, _, err := procPathCleanupSpec.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszDir))),
	uintptr(pszSpec),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function PathCleanupSpec(
  pszDir: PWideChar;   // LPCWSTR optional
  pszSpec: PWideChar   // LPWSTR in/out
): Integer; stdcall;
  external 'SHELL32.dll' name 'PathCleanupSpec';
result := DllCall("SHELL32\PathCleanupSpec"
    , "WStr", pszDir   ; LPCWSTR optional
    , "Ptr", pszSpec   ; LPWSTR in/out
    , "Int")   ; return: INT
●PathCleanupSpec(pszDir, pszSpec) = DLL("SHELL32.dll", "int PathCleanupSpec(char*, char*)")
# 呼び出し: PathCleanupSpec(pszDir, pszSpec)
# pszDir : LPCWSTR optional -> "char*"
# pszSpec : LPWSTR in/out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shell32" fn PathCleanupSpec(
    pszDir: [*c]const u16, // LPCWSTR optional
    pszSpec: [*c]u16 // LPWSTR in/out
) callconv(std.os.windows.WINAPI) i32;
proc PathCleanupSpec(
    pszDir: WideCString,  # LPCWSTR optional
    pszSpec: ptr uint16  # LPWSTR in/out
): int32 {.importc: "PathCleanupSpec", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
int PathCleanupSpec(
    const(wchar)* pszDir,   // LPCWSTR optional
    wchar* pszSpec   // LPWSTR in/out
);
ccall((:PathCleanupSpec, "SHELL32.dll"), stdcall, Int32,
      (Cwstring, Ptr{UInt16}),
      pszDir, pszSpec)
# pszDir : LPCWSTR optional -> Cwstring
# pszSpec : LPWSTR in/out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t PathCleanupSpec(
    const uint16_t* pszDir,
    uint16_t* pszSpec);
]]
local shell32 = ffi.load("shell32")
-- shell32.PathCleanupSpec(pszDir, pszSpec)
-- pszDir : LPCWSTR optional
-- pszSpec : LPWSTR in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const PathCleanupSpec = lib.func('__stdcall', 'PathCleanupSpec', 'int32_t', ['str16', 'uint16_t *']);
// PathCleanupSpec(pszDir, pszSpec)
// pszDir : LPCWSTR optional -> 'str16'
// pszSpec : LPWSTR in/out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  PathCleanupSpec: { parameters: ["buffer", "buffer"], result: "i32" },
});
// lib.symbols.PathCleanupSpec(pszDir, pszSpec)
// pszDir : LPCWSTR optional -> "buffer"
// pszSpec : LPWSTR in/out -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t PathCleanupSpec(
    const uint16_t* pszDir,
    uint16_t* pszSpec);
C, "SHELL32.dll");
// $ffi->PathCleanupSpec(pszDir, pszSpec);
// pszDir : LPCWSTR optional
// pszSpec : LPWSTR in/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 Shell32 extends StdCallLibrary {
    Shell32 INSTANCE = Native.load("shell32", Shell32.class);
    int PathCleanupSpec(
        WString pszDir,   // LPCWSTR optional
        char[] pszSpec   // LPWSTR in/out
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun PathCleanupSpec = PathCleanupSpec(
    pszDir : UInt16*,   # LPCWSTR optional
    pszSpec : UInt16*   # LPWSTR in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef PathCleanupSpecNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>);
typedef PathCleanupSpecDart = int Function(Pointer<Utf16>, Pointer<Utf16>);
final PathCleanupSpec = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<PathCleanupSpecNative, PathCleanupSpecDart>('PathCleanupSpec');
// pszDir : LPCWSTR optional -> Pointer<Utf16>
// pszSpec : LPWSTR in/out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PathCleanupSpec(
  pszDir: PWideChar;   // LPCWSTR optional
  pszSpec: PWideChar   // LPWSTR in/out
): Integer; stdcall;
  external 'SHELL32.dll' name 'PathCleanupSpec';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PathCleanupSpec"
  c_PathCleanupSpec :: CWString -> CWString -> IO Int32
-- pszDir : LPCWSTR optional -> CWString
-- pszSpec : LPWSTR in/out -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pathcleanupspec =
  foreign "PathCleanupSpec"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* pszDir : LPCWSTR optional -> (ptr uint16_t) *)
(* pszSpec : LPWSTR in/out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("PathCleanupSpec" path-cleanup-spec :convention :stdcall) :int32
  (psz-dir (:string :encoding :utf-16le))   ; LPCWSTR optional
  (psz-spec :pointer))   ; LPWSTR in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PathCleanupSpec = Win32::API::More->new('SHELL32',
    'int PathCleanupSpec(LPCWSTR pszDir, LPWSTR pszSpec)');
# my $ret = $PathCleanupSpec->Call($pszDir, $pszSpec);
# pszDir : LPCWSTR optional -> LPCWSTR
# pszSpec : LPWSTR in/out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。