PathYetAnotherMakeUniqueName
関数指定ディレクトリ内で重複しない一意のファイル名を生成する。
シグネチャ
// SHELL32.dll
#include <windows.h>
BOOL PathYetAnotherMakeUniqueName(
LPWSTR pszUniqueName,
LPCWSTR pszPath,
LPCWSTR pszShort, // optional
LPCWSTR pszFileSpec // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszUniqueName | LPWSTR | out | 一意のファイル名の完全修飾パスを含む、null 終端された Unicode 文字列を受け取る文字列バッファーです。バッファー オーバーランを防ぐため、このバッファーは少なくとも MAX_PATH 文字の長さが必要です。 |
| pszPath | LPCWSTR | in | 新しいファイルを格納するフォルダーの完全修飾パスを含む、null 終端された Unicode 文字列です。pszShort に NULL を指定した場合、この文字列には、新しいファイル名の基となる長いファイル名で終わる完全な宛先パスを含める必要があります。 |
| pszShort | LPCWSTR | inoptional | 一意の名前の基となる短いファイル名を含む、null 終端された Unicode 文字列です。長いファイル名を基に名前を作成するには、この値を NULL に設定します。 |
| pszFileSpec | LPCWSTR | inoptional | 一意の名前の基となる長いファイル名を含む、null 終端された Unicode 文字列です。 |
戻り値の型: BOOL
公式ドキュメント
既存のファイル名を基に、一意のファイル名を作成します。
戻り値
型: BOOL
一意の名前が正常に作成された場合は TRUE を、それ以外の場合は FALSE を返します。
解説(Remarks)
生成されたパスが MAX_PATH 文字を超える場合、PathYetAnotherMakeUniqueName は切り詰められた文字列を返すことがあります。その場合、この関数は FALSE を返します。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHELL32.dll
#include <windows.h>
BOOL PathYetAnotherMakeUniqueName(
LPWSTR pszUniqueName,
LPCWSTR pszPath,
LPCWSTR pszShort, // optional
LPCWSTR pszFileSpec // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHELL32.dll", ExactSpelling = true)]
static extern bool PathYetAnotherMakeUniqueName(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszUniqueName, // LPWSTR out
[MarshalAs(UnmanagedType.LPWStr)] string pszPath, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string pszShort, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string pszFileSpec // LPCWSTR optional
);<DllImport("SHELL32.dll", ExactSpelling:=True)>
Public Shared Function PathYetAnotherMakeUniqueName(
<MarshalAs(UnmanagedType.LPWStr)> pszUniqueName As System.Text.StringBuilder, ' LPWSTR out
<MarshalAs(UnmanagedType.LPWStr)> pszPath As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> pszShort As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> pszFileSpec As String ' LPCWSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pszUniqueName : LPWSTR out
' pszPath : LPCWSTR
' pszShort : LPCWSTR optional
' pszFileSpec : LPCWSTR optional
Declare PtrSafe Function PathYetAnotherMakeUniqueName Lib "shell32" ( _
ByVal pszUniqueName As LongPtr, _
ByVal pszPath As LongPtr, _
ByVal pszShort As LongPtr, _
ByVal pszFileSpec As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathYetAnotherMakeUniqueName = ctypes.windll.shell32.PathYetAnotherMakeUniqueName
PathYetAnotherMakeUniqueName.restype = wintypes.BOOL
PathYetAnotherMakeUniqueName.argtypes = [
wintypes.LPWSTR, # pszUniqueName : LPWSTR out
wintypes.LPCWSTR, # pszPath : LPCWSTR
wintypes.LPCWSTR, # pszShort : LPCWSTR optional
wintypes.LPCWSTR, # pszFileSpec : LPCWSTR optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHELL32.dll')
PathYetAnotherMakeUniqueName = Fiddle::Function.new(
lib['PathYetAnotherMakeUniqueName'],
[
Fiddle::TYPE_VOIDP, # pszUniqueName : LPWSTR out
Fiddle::TYPE_VOIDP, # pszPath : LPCWSTR
Fiddle::TYPE_VOIDP, # pszShort : LPCWSTR optional
Fiddle::TYPE_VOIDP, # pszFileSpec : LPCWSTR optional
],
Fiddle::TYPE_INT)#[link(name = "shell32")]
extern "system" {
fn PathYetAnotherMakeUniqueName(
pszUniqueName: *mut u16, // LPWSTR out
pszPath: *const u16, // LPCWSTR
pszShort: *const u16, // LPCWSTR optional
pszFileSpec: *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 PathYetAnotherMakeUniqueName([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszUniqueName, [MarshalAs(UnmanagedType.LPWStr)] string pszPath, [MarshalAs(UnmanagedType.LPWStr)] string pszShort, [MarshalAs(UnmanagedType.LPWStr)] string pszFileSpec);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_PathYetAnotherMakeUniqueName' -Namespace Win32 -PassThru
# $api::PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec)#uselib "SHELL32.dll"
#func global PathYetAnotherMakeUniqueName "PathYetAnotherMakeUniqueName" sptr, sptr, sptr, sptr
; PathYetAnotherMakeUniqueName varptr(pszUniqueName), pszPath, pszShort, pszFileSpec ; 戻り値は stat
; pszUniqueName : LPWSTR out -> "sptr"
; pszPath : LPCWSTR -> "sptr"
; pszShort : LPCWSTR optional -> "sptr"
; pszFileSpec : LPCWSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHELL32.dll" #cfunc global PathYetAnotherMakeUniqueName "PathYetAnotherMakeUniqueName" var, wstr, wstr, wstr ; res = PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec) ; pszUniqueName : LPWSTR out -> "var" ; pszPath : LPCWSTR -> "wstr" ; pszShort : LPCWSTR optional -> "wstr" ; pszFileSpec : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHELL32.dll" #cfunc global PathYetAnotherMakeUniqueName "PathYetAnotherMakeUniqueName" sptr, wstr, wstr, wstr ; res = PathYetAnotherMakeUniqueName(varptr(pszUniqueName), pszPath, pszShort, pszFileSpec) ; pszUniqueName : LPWSTR out -> "sptr" ; pszPath : LPCWSTR -> "wstr" ; pszShort : LPCWSTR optional -> "wstr" ; pszFileSpec : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL PathYetAnotherMakeUniqueName(LPWSTR pszUniqueName, LPCWSTR pszPath, LPCWSTR pszShort, LPCWSTR pszFileSpec) #uselib "SHELL32.dll" #cfunc global PathYetAnotherMakeUniqueName "PathYetAnotherMakeUniqueName" var, wstr, wstr, wstr ; res = PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec) ; pszUniqueName : LPWSTR out -> "var" ; pszPath : LPCWSTR -> "wstr" ; pszShort : LPCWSTR optional -> "wstr" ; pszFileSpec : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL PathYetAnotherMakeUniqueName(LPWSTR pszUniqueName, LPCWSTR pszPath, LPCWSTR pszShort, LPCWSTR pszFileSpec) #uselib "SHELL32.dll" #cfunc global PathYetAnotherMakeUniqueName "PathYetAnotherMakeUniqueName" intptr, wstr, wstr, wstr ; res = PathYetAnotherMakeUniqueName(varptr(pszUniqueName), pszPath, pszShort, pszFileSpec) ; pszUniqueName : LPWSTR out -> "intptr" ; pszPath : LPCWSTR -> "wstr" ; pszShort : LPCWSTR optional -> "wstr" ; pszFileSpec : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shell32 = windows.NewLazySystemDLL("SHELL32.dll")
procPathYetAnotherMakeUniqueName = shell32.NewProc("PathYetAnotherMakeUniqueName")
)
// pszUniqueName (LPWSTR out), pszPath (LPCWSTR), pszShort (LPCWSTR optional), pszFileSpec (LPCWSTR optional)
r1, _, err := procPathYetAnotherMakeUniqueName.Call(
uintptr(pszUniqueName),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszPath))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszShort))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFileSpec))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction PathYetAnotherMakeUniqueName(
pszUniqueName: PWideChar; // LPWSTR out
pszPath: PWideChar; // LPCWSTR
pszShort: PWideChar; // LPCWSTR optional
pszFileSpec: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'SHELL32.dll' name 'PathYetAnotherMakeUniqueName';result := DllCall("SHELL32\PathYetAnotherMakeUniqueName"
, "Ptr", pszUniqueName ; LPWSTR out
, "WStr", pszPath ; LPCWSTR
, "WStr", pszShort ; LPCWSTR optional
, "WStr", pszFileSpec ; LPCWSTR optional
, "Int") ; return: BOOL●PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec) = DLL("SHELL32.dll", "bool PathYetAnotherMakeUniqueName(char*, char*, char*, char*)")
# 呼び出し: PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec)
# pszUniqueName : LPWSTR out -> "char*"
# pszPath : LPCWSTR -> "char*"
# pszShort : LPCWSTR optional -> "char*"
# pszFileSpec : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shell32" fn PathYetAnotherMakeUniqueName(
pszUniqueName: [*c]u16, // LPWSTR out
pszPath: [*c]const u16, // LPCWSTR
pszShort: [*c]const u16, // LPCWSTR optional
pszFileSpec: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;proc PathYetAnotherMakeUniqueName(
pszUniqueName: ptr uint16, # LPWSTR out
pszPath: WideCString, # LPCWSTR
pszShort: WideCString, # LPCWSTR optional
pszFileSpec: WideCString # LPCWSTR optional
): int32 {.importc: "PathYetAnotherMakeUniqueName", stdcall, dynlib: "SHELL32.dll".}pragma(lib, "shell32");
extern(Windows)
int PathYetAnotherMakeUniqueName(
wchar* pszUniqueName, // LPWSTR out
const(wchar)* pszPath, // LPCWSTR
const(wchar)* pszShort, // LPCWSTR optional
const(wchar)* pszFileSpec // LPCWSTR optional
);ccall((:PathYetAnotherMakeUniqueName, "SHELL32.dll"), stdcall, Int32,
(Ptr{UInt16}, Cwstring, Cwstring, Cwstring),
pszUniqueName, pszPath, pszShort, pszFileSpec)
# pszUniqueName : LPWSTR out -> Ptr{UInt16}
# pszPath : LPCWSTR -> Cwstring
# pszShort : LPCWSTR optional -> Cwstring
# pszFileSpec : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathYetAnotherMakeUniqueName(
uint16_t* pszUniqueName,
const uint16_t* pszPath,
const uint16_t* pszShort,
const uint16_t* pszFileSpec);
]]
local shell32 = ffi.load("shell32")
-- shell32.PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec)
-- pszUniqueName : LPWSTR out
-- pszPath : LPCWSTR
-- pszShort : LPCWSTR optional
-- pszFileSpec : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const PathYetAnotherMakeUniqueName = lib.func('__stdcall', 'PathYetAnotherMakeUniqueName', 'int32_t', ['uint16_t *', 'str16', 'str16', 'str16']);
// PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec)
// pszUniqueName : LPWSTR out -> 'uint16_t *'
// pszPath : LPCWSTR -> 'str16'
// pszShort : LPCWSTR optional -> 'str16'
// pszFileSpec : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHELL32.dll", {
PathYetAnotherMakeUniqueName: { parameters: ["buffer", "buffer", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec)
// pszUniqueName : LPWSTR out -> "buffer"
// pszPath : LPCWSTR -> "buffer"
// pszShort : LPCWSTR optional -> "buffer"
// pszFileSpec : LPCWSTR optional -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathYetAnotherMakeUniqueName(
uint16_t* pszUniqueName,
const uint16_t* pszPath,
const uint16_t* pszShort,
const uint16_t* pszFileSpec);
C, "SHELL32.dll");
// $ffi->PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec);
// pszUniqueName : LPWSTR out
// pszPath : LPCWSTR
// pszShort : LPCWSTR optional
// pszFileSpec : 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 PathYetAnotherMakeUniqueName(
char[] pszUniqueName, // LPWSTR out
WString pszPath, // LPCWSTR
WString pszShort, // LPCWSTR optional
WString pszFileSpec // LPCWSTR optional
);
}@[Link("shell32")]
lib LibSHELL32
fun PathYetAnotherMakeUniqueName = PathYetAnotherMakeUniqueName(
pszUniqueName : UInt16*, # LPWSTR out
pszPath : UInt16*, # LPCWSTR
pszShort : UInt16*, # LPCWSTR optional
pszFileSpec : 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 PathYetAnotherMakeUniqueNameNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef PathYetAnotherMakeUniqueNameDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final PathYetAnotherMakeUniqueName = DynamicLibrary.open('SHELL32.dll')
.lookupFunction<PathYetAnotherMakeUniqueNameNative, PathYetAnotherMakeUniqueNameDart>('PathYetAnotherMakeUniqueName');
// pszUniqueName : LPWSTR out -> Pointer<Utf16>
// pszPath : LPCWSTR -> Pointer<Utf16>
// pszShort : LPCWSTR optional -> Pointer<Utf16>
// pszFileSpec : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathYetAnotherMakeUniqueName(
pszUniqueName: PWideChar; // LPWSTR out
pszPath: PWideChar; // LPCWSTR
pszShort: PWideChar; // LPCWSTR optional
pszFileSpec: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'SHELL32.dll' name 'PathYetAnotherMakeUniqueName';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathYetAnotherMakeUniqueName"
c_PathYetAnotherMakeUniqueName :: CWString -> CWString -> CWString -> CWString -> IO CInt
-- pszUniqueName : LPWSTR out -> CWString
-- pszPath : LPCWSTR -> CWString
-- pszShort : LPCWSTR optional -> CWString
-- pszFileSpec : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathyetanothermakeuniquename =
foreign "PathYetAnotherMakeUniqueName"
((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* pszUniqueName : LPWSTR out -> (ptr uint16_t) *)
(* pszPath : LPCWSTR -> (ptr uint16_t) *)
(* pszShort : LPCWSTR optional -> (ptr uint16_t) *)
(* pszFileSpec : 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 ("PathYetAnotherMakeUniqueName" path-yet-another-make-unique-name :convention :stdcall) :int32
(psz-unique-name :pointer) ; LPWSTR out
(psz-path (:string :encoding :utf-16le)) ; LPCWSTR
(psz-short (:string :encoding :utf-16le)) ; LPCWSTR optional
(psz-file-spec (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathYetAnotherMakeUniqueName = Win32::API::More->new('SHELL32',
'BOOL PathYetAnotherMakeUniqueName(LPWSTR pszUniqueName, LPCWSTR pszPath, LPCWSTR pszShort, LPCWSTR pszFileSpec)');
# my $ret = $PathYetAnotherMakeUniqueName->Call($pszUniqueName, $pszPath, $pszShort, $pszFileSpec);
# pszUniqueName : LPWSTR out -> LPWSTR
# pszPath : LPCWSTR -> LPCWSTR
# pszShort : LPCWSTR optional -> LPCWSTR
# pszFileSpec : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f PathMakeUniqueName — テンプレートを基に重複しない一意のファイル名を生成する。