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
cchMaxDWORDin
pszTemplateLPCWSTRin
pszLongPlateLPCWSTRinoptional
pszDirLPCWSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

// 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 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)。