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