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

PathMakeUniqueName

関数
テンプレートを基に重複しない一意のファイル名を生成する。
DLLSHELL32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

BOOL PathMakeUniqueName(
    LPWSTR pszUniqueName,
    DWORD cchMax,
    LPCWSTR pszTemplate,
    LPCWSTR pszLongPlate,   // optional
    LPCWSTR pszDir   // optional
);

パラメーター

名前方向説明
pszUniqueNameLPWSTRout一意のパス名を含む、null で終わる Unicode 文字列を受け取るバッファー。長さは少なくとも MAX_PATH 文字必要です。
cchMaxDWORDinpszUniqueName が指すバッファー内の文字数。
pszTemplateLPCWSTRin一意の名前を構築するために使用されるテンプレートを含む、null で終わる Unicode 文字列。このテンプレートは、8.3 形式のファイル名を必要とするドライブで使用されます。この文字列は、終端の null 文字を含めて MAX_PATH 文字以下である必要があります。
pszLongPlateLPCWSTRinoptional一意の名前を構築するために使用されるテンプレートを含む、null で終わる Unicode 文字列。このテンプレートは、長いファイル名をサポートするドライブで使用されます。この文字列は、終端の null 文字を含めて MAX_PATH 文字以下である必要があります。
pszDirLPCWSTRinoptional新しいファイルが置かれるディレクトリを含む、null で終わる文字列。この文字列は、終端の null 文字を含めて MAX_PATH 文字以下である必要があります。

戻り値の型: BOOL

公式ドキュメント

テンプレートから一意のパス名を作成します。

戻り値

型: BOOL

成功した場合は TRUE を返します。それ以外の場合は FALSE を返します。

解説(Remarks)

この関数は、8.3 形式を必要とするドライブ向けには pszTemplate で指定されたテンプレートに基づいて、長いファイル名をサポートするドライブ向けには pszLongPlate に基づいて、新しい一意のファイル名を生成します。たとえば、pszLongPlate に "My New Filename" を指定すると、PathMakeUniqueName は "My New Filename (1)"、"My New Filename (2)" などの名前を返します。

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

各言語での呼び出し定義

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

BOOL PathMakeUniqueName(
    LPWSTR pszUniqueName,
    DWORD cchMax,
    LPCWSTR pszTemplate,
    LPCWSTR pszLongPlate,   // optional
    LPCWSTR pszDir   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHELL32.dll", ExactSpelling = true)]
static extern bool PathMakeUniqueName(
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszUniqueName,   // LPWSTR out
    uint cchMax,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string pszTemplate,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszLongPlate,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string pszDir   // LPCWSTR optional
);
<DllImport("SHELL32.dll", ExactSpelling:=True)>
Public Shared Function PathMakeUniqueName(
    <MarshalAs(UnmanagedType.LPWStr)> pszUniqueName As System.Text.StringBuilder,   ' LPWSTR out
    cchMax As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pszTemplate As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszLongPlate As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> pszDir As String   ' LPCWSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' pszUniqueName : LPWSTR out
' cchMax : DWORD
' pszTemplate : LPCWSTR
' pszLongPlate : LPCWSTR optional
' pszDir : LPCWSTR optional
Declare PtrSafe Function PathMakeUniqueName Lib "shell32" ( _
    ByVal pszUniqueName As LongPtr, _
    ByVal cchMax As Long, _
    ByVal pszTemplate As LongPtr, _
    ByVal pszLongPlate As LongPtr, _
    ByVal pszDir As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PathMakeUniqueName = ctypes.windll.shell32.PathMakeUniqueName
PathMakeUniqueName.restype = wintypes.BOOL
PathMakeUniqueName.argtypes = [
    wintypes.LPWSTR,  # pszUniqueName : LPWSTR out
    wintypes.DWORD,  # cchMax : DWORD
    wintypes.LPCWSTR,  # pszTemplate : LPCWSTR
    wintypes.LPCWSTR,  # pszLongPlate : LPCWSTR optional
    wintypes.LPCWSTR,  # pszDir : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procPathMakeUniqueName = shell32.NewProc("PathMakeUniqueName")
)

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

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

typedef PathMakeUniqueNameNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef PathMakeUniqueNameDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final PathMakeUniqueName = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<PathMakeUniqueNameNative, PathMakeUniqueNameDart>('PathMakeUniqueName');
// pszUniqueName : LPWSTR out -> Pointer<Utf16>
// cchMax : DWORD -> Uint32
// pszTemplate : LPCWSTR -> Pointer<Utf16>
// pszLongPlate : LPCWSTR optional -> Pointer<Utf16>
// pszDir : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PathMakeUniqueName(
  pszUniqueName: PWideChar;   // LPWSTR out
  cchMax: DWORD;   // DWORD
  pszTemplate: PWideChar;   // LPCWSTR
  pszLongPlate: PWideChar;   // LPCWSTR optional
  pszDir: PWideChar   // LPCWSTR optional
): BOOL; stdcall;
  external 'SHELL32.dll' name 'PathMakeUniqueName';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PathMakeUniqueName"
  c_PathMakeUniqueName :: CWString -> Word32 -> CWString -> CWString -> CWString -> IO CInt
-- pszUniqueName : LPWSTR out -> CWString
-- cchMax : DWORD -> Word32
-- pszTemplate : LPCWSTR -> CWString
-- pszLongPlate : LPCWSTR optional -> CWString
-- pszDir : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pathmakeuniquename =
  foreign "PathMakeUniqueName"
    ((ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* pszUniqueName : LPWSTR out -> (ptr uint16_t) *)
(* cchMax : DWORD -> uint32_t *)
(* pszTemplate : LPCWSTR -> (ptr uint16_t) *)
(* pszLongPlate : LPCWSTR optional -> (ptr uint16_t) *)
(* pszDir : LPCWSTR optional -> (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 ("PathMakeUniqueName" path-make-unique-name :convention :stdcall) :int32
  (psz-unique-name :pointer)   ; LPWSTR out
  (cch-max :uint32)   ; DWORD
  (psz-template (:string :encoding :utf-16le))   ; LPCWSTR
  (psz-long-plate (:string :encoding :utf-16le))   ; LPCWSTR optional
  (psz-dir (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PathMakeUniqueName = Win32::API::More->new('SHELL32',
    'BOOL PathMakeUniqueName(LPWSTR pszUniqueName, DWORD cchMax, LPCWSTR pszTemplate, LPCWSTR pszLongPlate, LPCWSTR pszDir)');
# my $ret = $PathMakeUniqueName->Call($pszUniqueName, $cchMax, $pszTemplate, $pszLongPlate, $pszDir);
# pszUniqueName : LPWSTR out -> LPWSTR
# cchMax : DWORD -> DWORD
# pszTemplate : LPCWSTR -> LPCWSTR
# pszLongPlate : LPCWSTR optional -> LPCWSTR
# pszDir : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。