Win32 API 日本語リファレンス
ホームStorage.FileSystem › CreateDirectoryW

CreateDirectoryW

関数
指定した名前のディレクトリを新規作成する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL CreateDirectoryW(
    LPCWSTR lpPathName,
    SECURITY_ATTRIBUTES* lpSecurityAttributes   // optional
);

パラメーター

名前方向
lpPathNameLPCWSTRin
lpSecurityAttributesSECURITY_ATTRIBUTES*inoptional

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL CreateDirectoryW(
    LPCWSTR lpPathName,
    SECURITY_ATTRIBUTES* lpSecurityAttributes   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CreateDirectoryW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpPathName,   // LPCWSTR
    IntPtr lpSecurityAttributes   // SECURITY_ATTRIBUTES* optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateDirectoryW(
    <MarshalAs(UnmanagedType.LPWStr)> lpPathName As String,   ' LPCWSTR
    lpSecurityAttributes As IntPtr   ' SECURITY_ATTRIBUTES* optional
) As Boolean
End Function
' lpPathName : LPCWSTR
' lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
Declare PtrSafe Function CreateDirectoryW Lib "kernel32" ( _
    ByVal lpPathName As LongPtr, _
    ByVal lpSecurityAttributes As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateDirectoryW = ctypes.windll.kernel32.CreateDirectoryW
CreateDirectoryW.restype = wintypes.BOOL
CreateDirectoryW.argtypes = [
    wintypes.LPCWSTR,  # lpPathName : LPCWSTR
    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')
CreateDirectoryW = Fiddle::Function.new(
  lib['CreateDirectoryW'],
  [
    Fiddle::TYPE_VOIDP,  # lpPathName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn CreateDirectoryW(
        lpPathName: *const u16,  // LPCWSTR
        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.Unicode, SetLastError = true)]
public static extern bool CreateDirectoryW([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, IntPtr lpSecurityAttributes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateDirectoryW' -Namespace Win32 -PassThru
# $api::CreateDirectoryW(lpPathName, lpSecurityAttributes)
#uselib "KERNEL32.dll"
#func global CreateDirectoryW "CreateDirectoryW" wptr, wptr
; CreateDirectoryW lpPathName, varptr(lpSecurityAttributes)   ; 戻り値は stat
; lpPathName : LPCWSTR -> "wptr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateDirectoryW "CreateDirectoryW" wstr, var
; res = CreateDirectoryW(lpPathName, lpSecurityAttributes)
; lpPathName : LPCWSTR -> "wstr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CreateDirectoryW(LPCWSTR lpPathName, SECURITY_ATTRIBUTES* lpSecurityAttributes)
#uselib "KERNEL32.dll"
#cfunc global CreateDirectoryW "CreateDirectoryW" wstr, var
; res = CreateDirectoryW(lpPathName, lpSecurityAttributes)
; lpPathName : LPCWSTR -> "wstr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateDirectoryW = kernel32.NewProc("CreateDirectoryW")
)

// lpPathName (LPCWSTR), lpSecurityAttributes (SECURITY_ATTRIBUTES* optional)
r1, _, err := procCreateDirectoryW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPathName))),
	uintptr(lpSecurityAttributes),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CreateDirectoryW(
  lpPathName: PWideChar;   // LPCWSTR
  lpSecurityAttributes: Pointer   // SECURITY_ATTRIBUTES* optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'CreateDirectoryW';
result := DllCall("KERNEL32\CreateDirectoryW"
    , "WStr", lpPathName   ; LPCWSTR
    , "Ptr", lpSecurityAttributes   ; SECURITY_ATTRIBUTES* optional
    , "Int")   ; return: BOOL
●CreateDirectoryW(lpPathName, lpSecurityAttributes) = DLL("KERNEL32.dll", "bool CreateDirectoryW(char*, void*)")
# 呼び出し: CreateDirectoryW(lpPathName, lpSecurityAttributes)
# lpPathName : LPCWSTR -> "char*"
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。