PathCchRemoveFileSpec
関数パスから最後のファイル名要素を除去する。
シグネチャ
// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>
HRESULT PathCchRemoveFileSpec(
LPWSTR pszPath,
UINT_PTR cchPath
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszPath | LPWSTR | inout | 完全修飾パス文字列へのポインターです。この関数が正常に終了すると、文字列から最後の要素とその先頭のバックスラッシュが削除されています。この関数は "C:" のようなルートパスには影響を与えません。ルートパスの場合、パス文字列は変更されずに返されます。パス文字列が末尾のバックスラッシュで終わっている場合は、そのバックスラッシュのみが削除されます。 |
| cchPath | UINT_PTR | in | pszPath が指すバッファーのサイズ(文字数)です。 |
戻り値の型: HRESULT
公式ドキュメント
パス文字列の最後の要素を削除します。その要素がファイル名であってもディレクトリ名であっても削除されます。
戻り値
解説(Remarks)
次の表は、いくつかのパス文字列に対するこの関数の効果を示しています。
| 元の文字列 | 返される文字列 |
|---|---|
| "C:\path1" | "C:\" |
| "C:\path1\path2" | "C:\path1" |
| "C:\path1\" | "C:\path1" |
| "\\path1\path2\path3" | "\\path1\path2" |
| "\path1" | "\" |
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// api-ms-win-core-path-l1-1-0.dll
#include <windows.h>
HRESULT PathCchRemoveFileSpec(
LPWSTR pszPath,
UINT_PTR cchPath
);[DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling = true)]
static extern int PathCchRemoveFileSpec(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, // LPWSTR in/out
UIntPtr cchPath // UINT_PTR
);<DllImport("api-ms-win-core-path-l1-1-0.dll", ExactSpelling:=True)>
Public Shared Function PathCchRemoveFileSpec(
<MarshalAs(UnmanagedType.LPWStr)> pszPath As System.Text.StringBuilder, ' LPWSTR in/out
cchPath As UIntPtr ' UINT_PTR
) As Integer
End Function' pszPath : LPWSTR in/out
' cchPath : UINT_PTR
Declare PtrSafe Function PathCchRemoveFileSpec Lib "api-ms-win-core-path-l1-1-0" ( _
ByVal pszPath As LongPtr, _
ByVal cchPath As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PathCchRemoveFileSpec = ctypes.windll.LoadLibrary("api-ms-win-core-path-l1-1-0.dll").PathCchRemoveFileSpec
PathCchRemoveFileSpec.restype = ctypes.c_int
PathCchRemoveFileSpec.argtypes = [
wintypes.LPWSTR, # pszPath : LPWSTR in/out
ctypes.c_size_t, # cchPath : UINT_PTR
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('api-ms-win-core-path-l1-1-0.dll')
PathCchRemoveFileSpec = Fiddle::Function.new(
lib['PathCchRemoveFileSpec'],
[
Fiddle::TYPE_VOIDP, # pszPath : LPWSTR in/out
Fiddle::TYPE_UINTPTR_T, # cchPath : UINT_PTR
],
Fiddle::TYPE_INT)#[link(name = "api-ms-win-core-path-l1-1-0")]
extern "system" {
fn PathCchRemoveFileSpec(
pszPath: *mut u16, // LPWSTR in/out
cchPath: usize // UINT_PTR
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("api-ms-win-core-path-l1-1-0.dll")]
public static extern int PathCchRemoveFileSpec([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, UIntPtr cchPath);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-path-l1-1-0_PathCchRemoveFileSpec' -Namespace Win32 -PassThru
# $api::PathCchRemoveFileSpec(pszPath, cchPath)#uselib "api-ms-win-core-path-l1-1-0.dll"
#func global PathCchRemoveFileSpec "PathCchRemoveFileSpec" sptr, sptr
; PathCchRemoveFileSpec varptr(pszPath), cchPath ; 戻り値は stat
; pszPath : LPWSTR in/out -> "sptr"
; cchPath : UINT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchRemoveFileSpec "PathCchRemoveFileSpec" var, sptr ; res = PathCchRemoveFileSpec(pszPath, cchPath) ; pszPath : LPWSTR in/out -> "var" ; cchPath : UINT_PTR -> "sptr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchRemoveFileSpec "PathCchRemoveFileSpec" sptr, sptr ; res = PathCchRemoveFileSpec(varptr(pszPath), cchPath) ; pszPath : LPWSTR in/out -> "sptr" ; cchPath : UINT_PTR -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT PathCchRemoveFileSpec(LPWSTR pszPath, UINT_PTR cchPath) #uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchRemoveFileSpec "PathCchRemoveFileSpec" var, intptr ; res = PathCchRemoveFileSpec(pszPath, cchPath) ; pszPath : LPWSTR in/out -> "var" ; cchPath : UINT_PTR -> "intptr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT PathCchRemoveFileSpec(LPWSTR pszPath, UINT_PTR cchPath) #uselib "api-ms-win-core-path-l1-1-0.dll" #cfunc global PathCchRemoveFileSpec "PathCchRemoveFileSpec" intptr, intptr ; res = PathCchRemoveFileSpec(varptr(pszPath), cchPath) ; pszPath : LPWSTR in/out -> "intptr" ; cchPath : UINT_PTR -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
api_ms_win_core_path_l1_1_0 = windows.NewLazySystemDLL("api-ms-win-core-path-l1-1-0.dll")
procPathCchRemoveFileSpec = api_ms_win_core_path_l1_1_0.NewProc("PathCchRemoveFileSpec")
)
// pszPath (LPWSTR in/out), cchPath (UINT_PTR)
r1, _, err := procPathCchRemoveFileSpec.Call(
uintptr(pszPath),
uintptr(cchPath),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction PathCchRemoveFileSpec(
pszPath: PWideChar; // LPWSTR in/out
cchPath: NativeUInt // UINT_PTR
): Integer; stdcall;
external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchRemoveFileSpec';result := DllCall("api-ms-win-core-path-l1-1-0\PathCchRemoveFileSpec"
, "Ptr", pszPath ; LPWSTR in/out
, "UPtr", cchPath ; UINT_PTR
, "Int") ; return: HRESULT●PathCchRemoveFileSpec(pszPath, cchPath) = DLL("api-ms-win-core-path-l1-1-0.dll", "int PathCchRemoveFileSpec(char*, int)")
# 呼び出し: PathCchRemoveFileSpec(pszPath, cchPath)
# pszPath : LPWSTR in/out -> "char*"
# cchPath : UINT_PTR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "api-ms-win-core-path-l1-1-0" fn PathCchRemoveFileSpec(
pszPath: [*c]u16, // LPWSTR in/out
cchPath: usize // UINT_PTR
) callconv(std.os.windows.WINAPI) i32;proc PathCchRemoveFileSpec(
pszPath: ptr uint16, # LPWSTR in/out
cchPath: uint # UINT_PTR
): int32 {.importc: "PathCchRemoveFileSpec", stdcall, dynlib: "api-ms-win-core-path-l1-1-0.dll".}pragma(lib, "api-ms-win-core-path-l1-1-0");
extern(Windows)
int PathCchRemoveFileSpec(
wchar* pszPath, // LPWSTR in/out
size_t cchPath // UINT_PTR
);ccall((:PathCchRemoveFileSpec, "api-ms-win-core-path-l1-1-0.dll"), stdcall, Int32,
(Ptr{UInt16}, Csize_t),
pszPath, cchPath)
# pszPath : LPWSTR in/out -> Ptr{UInt16}
# cchPath : UINT_PTR -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PathCchRemoveFileSpec(
uint16_t* pszPath,
uintptr_t cchPath);
]]
local api-ms-win-core-path-l1-1-0 = ffi.load("api-ms-win-core-path-l1-1-0")
-- api-ms-win-core-path-l1-1-0.PathCchRemoveFileSpec(pszPath, cchPath)
-- pszPath : LPWSTR in/out
-- cchPath : UINT_PTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-path-l1-1-0.dll');
const PathCchRemoveFileSpec = lib.func('__stdcall', 'PathCchRemoveFileSpec', 'int32_t', ['uint16_t *', 'uintptr_t']);
// PathCchRemoveFileSpec(pszPath, cchPath)
// pszPath : LPWSTR in/out -> 'uint16_t *'
// cchPath : UINT_PTR -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("api-ms-win-core-path-l1-1-0.dll", {
PathCchRemoveFileSpec: { parameters: ["buffer", "usize"], result: "i32" },
});
// lib.symbols.PathCchRemoveFileSpec(pszPath, cchPath)
// pszPath : LPWSTR in/out -> "buffer"
// cchPath : UINT_PTR -> "usize"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PathCchRemoveFileSpec(
uint16_t* pszPath,
size_t cchPath);
C, "api-ms-win-core-path-l1-1-0.dll");
// $ffi->PathCchRemoveFileSpec(pszPath, cchPath);
// pszPath : LPWSTR in/out
// cchPath : UINT_PTR
// 構造体/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 Api-ms-win-core-path-l1-1-0 extends StdCallLibrary {
Api-ms-win-core-path-l1-1-0 INSTANCE = Native.load("api-ms-win-core-path-l1-1-0", Api-ms-win-core-path-l1-1-0.class);
int PathCchRemoveFileSpec(
char[] pszPath, // LPWSTR in/out
long cchPath // UINT_PTR
);
}@[Link("api-ms-win-core-path-l1-1-0")]
lib Libapi-ms-win-core-path-l1-1-0
fun PathCchRemoveFileSpec = PathCchRemoveFileSpec(
pszPath : UInt16*, # LPWSTR in/out
cchPath : LibC::SizeT # UINT_PTR
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathCchRemoveFileSpecNative = Int32 Function(Pointer<Utf16>, UintPtr);
typedef PathCchRemoveFileSpecDart = int Function(Pointer<Utf16>, int);
final PathCchRemoveFileSpec = DynamicLibrary.open('api-ms-win-core-path-l1-1-0.dll')
.lookupFunction<PathCchRemoveFileSpecNative, PathCchRemoveFileSpecDart>('PathCchRemoveFileSpec');
// pszPath : LPWSTR in/out -> Pointer<Utf16>
// cchPath : UINT_PTR -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathCchRemoveFileSpec(
pszPath: PWideChar; // LPWSTR in/out
cchPath: NativeUInt // UINT_PTR
): Integer; stdcall;
external 'api-ms-win-core-path-l1-1-0.dll' name 'PathCchRemoveFileSpec';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathCchRemoveFileSpec"
c_PathCchRemoveFileSpec :: CWString -> CUIntPtr -> IO Int32
-- pszPath : LPWSTR in/out -> CWString
-- cchPath : UINT_PTR -> CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathcchremovefilespec =
foreign "PathCchRemoveFileSpec"
((ptr uint16_t) @-> size_t @-> returning int32_t)
(* pszPath : LPWSTR in/out -> (ptr uint16_t) *)
(* cchPath : UINT_PTR -> size_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library api-ms-win-core-path-l1-1-0 (t "api-ms-win-core-path-l1-1-0.dll"))
(cffi:use-foreign-library api-ms-win-core-path-l1-1-0)
(cffi:defcfun ("PathCchRemoveFileSpec" path-cch-remove-file-spec :convention :stdcall) :int32
(psz-path :pointer) ; LPWSTR in/out
(cch-path :uint64)) ; UINT_PTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathCchRemoveFileSpec = Win32::API::More->new('api-ms-win-core-path-l1-1-0',
'int PathCchRemoveFileSpec(LPWSTR pszPath, WPARAM cchPath)');
# my $ret = $PathCchRemoveFileSpec->Call($pszPath, $cchPath);
# pszPath : LPWSTR in/out -> LPWSTR
# cchPath : UINT_PTR -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。