RemoveDirectoryA
関数シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL RemoveDirectoryA(
LPCSTR lpPathName
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpPathName | LPCSTR | in | 削除するディレクトリのパス。このパスは空のディレクトリを指定する必要があり、呼び出し元プロセスはそのディレクトリに対する削除アクセス権を持っている必要があります。 既定では、名前は MAX_PATH 文字に制限されています。この制限を 32,767 ワイド文字に拡張するには、パスの先頭に "\\?\" を付加します。詳細については、ファイル、パス、および名前空間の名前付けを参照してください。 ヒント
Windows 10 バージョン 1607 以降では、"\\?\" を付加せずに MAX_PATH の制限を解除するようオプトインできます。詳細については、ファイル、パス、および名前空間の名前付けの「Maximum Path Length Limitation」セクションを参照してください。 |
戻り値の型: BOOL
公式ドキュメント
既存の空のディレクトリを削除します。(ANSI)
戻り値
関数が成功した場合、戻り値は 0 以外の値になります。
関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、GetLastError を呼び出します。
解説(Remarks)
RemoveDirectory 関数は、クローズ時に削除するようディレクトリにマークを付けます。そのため、ディレクトリに対する最後のハンドルが閉じられるまで、ディレクトリは削除されません。
ディレクトリ内のファイルを再帰的に削除するには、SHFileOperation 関数を使用します。
RemoveDirectory はディレクトリ ジャンクションの削除に使用できます。ターゲット ディレクトリとその内容は正規パスを通じて引き続きアクセス可能であるため、ジャンクションを削除しても、そのターゲットとなるディレクトリ自体は影響を受けません。このため、lpPathName がディレクトリ ジャンクションを参照している場合、RemoveDirectory はターゲット ディレクトリが空かどうかにかかわらず、指定されたリンクを削除します。ジャンクションの詳細については、ハード リンクとジャンクションを参照してください。
POSIX 削除を使用すると、ハンドルが開いたままの状態でディレクトリが削除されます。その後、CreateDirectory を呼び出してそのディレクトリを開こうとすると、ERROR_FILE_NOT_FOUND で失敗します。
Windows 8 および Windows Server 2012 では、この関数は次のテクノロジでサポートされています。
| テクノロジ | サポート |
|---|---|
| Server Message Block (SMB) 3.0 プロトコル | はい |
| SMB 3.0 トランスペアレント フェールオーバー (TFO) | はい |
| スケールアウト ファイル共有 (SO) を使用した SMB 3.0 | はい |
| クラスター共有ボリューム ファイル システム (CsvFS) | はい |
| 回復性のあるファイル システム (ReFS) | はい |
fileapi.h ヘッダーは RemoveDirectory をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコード ニュートラルなエイリアスの使用を、エンコード ニュートラルではないコードと混在させると、コンパイル エラーまたは実行時エラーを引き起こす不整合が生じる可能性があります。詳細については、関数プロトタイプの規則を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL RemoveDirectoryA(
LPCSTR lpPathName
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool RemoveDirectoryA(
[MarshalAs(UnmanagedType.LPStr)] string lpPathName // LPCSTR
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function RemoveDirectoryA(
<MarshalAs(UnmanagedType.LPStr)> lpPathName As String ' LPCSTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpPathName : LPCSTR
Declare PtrSafe Function RemoveDirectoryA Lib "kernel32" ( _
ByVal lpPathName As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RemoveDirectoryA = ctypes.windll.kernel32.RemoveDirectoryA
RemoveDirectoryA.restype = wintypes.BOOL
RemoveDirectoryA.argtypes = [
wintypes.LPCSTR, # lpPathName : LPCSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
RemoveDirectoryA = Fiddle::Function.new(
lib['RemoveDirectoryA'],
[
Fiddle::TYPE_VOIDP, # lpPathName : LPCSTR
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn RemoveDirectoryA(
lpPathName: *const u8 // LPCSTR
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool RemoveDirectoryA([MarshalAs(UnmanagedType.LPStr)] string lpPathName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_RemoveDirectoryA' -Namespace Win32 -PassThru
# $api::RemoveDirectoryA(lpPathName)#uselib "KERNEL32.dll"
#func global RemoveDirectoryA "RemoveDirectoryA" sptr
; RemoveDirectoryA lpPathName ; 戻り値は stat
; lpPathName : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global RemoveDirectoryA "RemoveDirectoryA" str
; res = RemoveDirectoryA(lpPathName)
; lpPathName : LPCSTR -> "str"; BOOL RemoveDirectoryA(LPCSTR lpPathName)
#uselib "KERNEL32.dll"
#cfunc global RemoveDirectoryA "RemoveDirectoryA" str
; res = RemoveDirectoryA(lpPathName)
; lpPathName : LPCSTR -> "str"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procRemoveDirectoryA = kernel32.NewProc("RemoveDirectoryA")
)
// lpPathName (LPCSTR)
r1, _, err := procRemoveDirectoryA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpPathName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction RemoveDirectoryA(
lpPathName: PAnsiChar // LPCSTR
): BOOL; stdcall;
external 'KERNEL32.dll' name 'RemoveDirectoryA';result := DllCall("KERNEL32\RemoveDirectoryA"
, "AStr", lpPathName ; LPCSTR
, "Int") ; return: BOOL●RemoveDirectoryA(lpPathName) = DLL("KERNEL32.dll", "bool RemoveDirectoryA(char*)")
# 呼び出し: RemoveDirectoryA(lpPathName)
# lpPathName : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn RemoveDirectoryA(
lpPathName: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) i32;proc RemoveDirectoryA(
lpPathName: cstring # LPCSTR
): int32 {.importc: "RemoveDirectoryA", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int RemoveDirectoryA(
const(char)* lpPathName // LPCSTR
);ccall((:RemoveDirectoryA, "KERNEL32.dll"), stdcall, Int32,
(Cstring,),
lpPathName)
# lpPathName : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t RemoveDirectoryA(
const char* lpPathName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.RemoveDirectoryA(lpPathName)
-- lpPathName : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const RemoveDirectoryA = lib.func('__stdcall', 'RemoveDirectoryA', 'int32_t', ['str']);
// RemoveDirectoryA(lpPathName)
// lpPathName : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
RemoveDirectoryA: { parameters: ["buffer"], result: "i32" },
});
// lib.symbols.RemoveDirectoryA(lpPathName)
// lpPathName : LPCSTR -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t RemoveDirectoryA(
const char* lpPathName);
C, "KERNEL32.dll");
// $ffi->RemoveDirectoryA(lpPathName);
// lpPathName : LPCSTR
// 構造体/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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.ASCII_OPTIONS);
boolean RemoveDirectoryA(
String lpPathName // LPCSTR
);
}@[Link("kernel32")]
lib LibKERNEL32
fun RemoveDirectoryA = RemoveDirectoryA(
lpPathName : UInt8* # LPCSTR
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef RemoveDirectoryANative = Int32 Function(Pointer<Utf8>);
typedef RemoveDirectoryADart = int Function(Pointer<Utf8>);
final RemoveDirectoryA = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<RemoveDirectoryANative, RemoveDirectoryADart>('RemoveDirectoryA');
// lpPathName : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RemoveDirectoryA(
lpPathName: PAnsiChar // LPCSTR
): BOOL; stdcall;
external 'KERNEL32.dll' name 'RemoveDirectoryA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RemoveDirectoryA"
c_RemoveDirectoryA :: CString -> IO CInt
-- lpPathName : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let removedirectorya =
foreign "RemoveDirectoryA"
(string @-> returning int32_t)
(* lpPathName : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("RemoveDirectoryA" remove-directory-a :convention :stdcall) :int32
(lp-path-name :string)) ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RemoveDirectoryA = Win32::API::More->new('KERNEL32',
'BOOL RemoveDirectoryA(LPCSTR lpPathName)');
# my $ret = $RemoveDirectoryA->Call($lpPathName);
# lpPathName : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f RemoveDirectoryW (Unicode版) — 指定した空のディレクトリを削除する。
- f RemoveDirectory2A — フラグを指定してディレクトリを削除する(ANSI版)。