Win32 API 日本語リファレンス
ホームSystem.Diagnostics.Debug › MakeSureDirectoryPathExists

MakeSureDirectoryPathExists

関数
指定したディレクトリパスの全階層を作成する。
DLLdbghelp.dll呼出規約winapiSetLastErrorあり

シグネチャ

// dbghelp.dll
#include <windows.h>

BOOL MakeSureDirectoryPathExists(
    LPCSTR DirPath
);

パラメーター

名前方向説明
DirPathLPCSTRin有効なパス名。パスの最後の要素がファイル名ではなくディレクトリの場合、文字列はバックスラッシュ (\) 文字で終わる必要があります。

戻り値の型: BOOL

公式ドキュメント

指定したパスに含まれるすべてのディレクトリを、ルートから順に作成します。

戻り値

関数が成功した場合、戻り値は TRUE です。

関数が失敗した場合、戻り値は FALSE です。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

指定された各ディレクトリは、まだ存在しない場合に作成されます。一部のディレクトリのみが作成された場合、この関数は FALSE を返します。

この関数は Unicode 文字列をサポートしません。Unicode パスを指定するには、SHCreateDirectoryEx 関数を使用してください。

この関数を含むすべての DbgHelp 関数はシングルスレッドです。そのため、複数のスレッドからこの関数を呼び出すと、予期しない動作やメモリ破損が発生する可能性があります。これを回避するには、複数のスレッドからこの関数への同時呼び出しをすべて同期する必要があります。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// dbghelp.dll
#include <windows.h>

BOOL MakeSureDirectoryPathExists(
    LPCSTR DirPath
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("dbghelp.dll", SetLastError = true, ExactSpelling = true)]
static extern bool MakeSureDirectoryPathExists(
    [MarshalAs(UnmanagedType.LPStr)] string DirPath   // LPCSTR
);
<DllImport("dbghelp.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function MakeSureDirectoryPathExists(
    <MarshalAs(UnmanagedType.LPStr)> DirPath As String   ' LPCSTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' DirPath : LPCSTR
Declare PtrSafe Function MakeSureDirectoryPathExists Lib "dbghelp" ( _
    ByVal DirPath As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

MakeSureDirectoryPathExists = ctypes.windll.dbghelp.MakeSureDirectoryPathExists
MakeSureDirectoryPathExists.restype = wintypes.BOOL
MakeSureDirectoryPathExists.argtypes = [
    wintypes.LPCSTR,  # DirPath : LPCSTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('dbghelp.dll')
MakeSureDirectoryPathExists = Fiddle::Function.new(
  lib['MakeSureDirectoryPathExists'],
  [
    Fiddle::TYPE_VOIDP,  # DirPath : LPCSTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "dbghelp")]
extern "system" {
    fn MakeSureDirectoryPathExists(
        DirPath: *const u8  // LPCSTR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("dbghelp.dll", SetLastError = true)]
public static extern bool MakeSureDirectoryPathExists([MarshalAs(UnmanagedType.LPStr)] string DirPath);
"@
$api = Add-Type -MemberDefinition $sig -Name 'dbghelp_MakeSureDirectoryPathExists' -Namespace Win32 -PassThru
# $api::MakeSureDirectoryPathExists(DirPath)
#uselib "dbghelp.dll"
#func global MakeSureDirectoryPathExists "MakeSureDirectoryPathExists" sptr
; MakeSureDirectoryPathExists DirPath   ; 戻り値は stat
; DirPath : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "dbghelp.dll"
#cfunc global MakeSureDirectoryPathExists "MakeSureDirectoryPathExists" str
; res = MakeSureDirectoryPathExists(DirPath)
; DirPath : LPCSTR -> "str"
; BOOL MakeSureDirectoryPathExists(LPCSTR DirPath)
#uselib "dbghelp.dll"
#cfunc global MakeSureDirectoryPathExists "MakeSureDirectoryPathExists" str
; res = MakeSureDirectoryPathExists(DirPath)
; DirPath : LPCSTR -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	dbghelp = windows.NewLazySystemDLL("dbghelp.dll")
	procMakeSureDirectoryPathExists = dbghelp.NewProc("MakeSureDirectoryPathExists")
)

// DirPath (LPCSTR)
r1, _, err := procMakeSureDirectoryPathExists.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(DirPath))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function MakeSureDirectoryPathExists(
  DirPath: PAnsiChar   // LPCSTR
): BOOL; stdcall;
  external 'dbghelp.dll' name 'MakeSureDirectoryPathExists';
result := DllCall("dbghelp\MakeSureDirectoryPathExists"
    , "AStr", DirPath   ; LPCSTR
    , "Int")   ; return: BOOL
●MakeSureDirectoryPathExists(DirPath) = DLL("dbghelp.dll", "bool MakeSureDirectoryPathExists(char*)")
# 呼び出し: MakeSureDirectoryPathExists(DirPath)
# DirPath : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "dbghelp" fn MakeSureDirectoryPathExists(
    DirPath: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) i32;
proc MakeSureDirectoryPathExists(
    DirPath: cstring  # LPCSTR
): int32 {.importc: "MakeSureDirectoryPathExists", stdcall, dynlib: "dbghelp.dll".}
pragma(lib, "dbghelp");
extern(Windows)
int MakeSureDirectoryPathExists(
    const(char)* DirPath   // LPCSTR
);
ccall((:MakeSureDirectoryPathExists, "dbghelp.dll"), stdcall, Int32,
      (Cstring,),
      DirPath)
# DirPath : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t MakeSureDirectoryPathExists(
    const char* DirPath);
]]
local dbghelp = ffi.load("dbghelp")
-- dbghelp.MakeSureDirectoryPathExists(DirPath)
-- DirPath : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('dbghelp.dll');
const MakeSureDirectoryPathExists = lib.func('__stdcall', 'MakeSureDirectoryPathExists', 'int32_t', ['str']);
// MakeSureDirectoryPathExists(DirPath)
// DirPath : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("dbghelp.dll", {
  MakeSureDirectoryPathExists: { parameters: ["buffer"], result: "i32" },
});
// lib.symbols.MakeSureDirectoryPathExists(DirPath)
// DirPath : LPCSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t MakeSureDirectoryPathExists(
    const char* DirPath);
C, "dbghelp.dll");
// $ffi->MakeSureDirectoryPathExists(DirPath);
// DirPath : 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 Dbghelp extends StdCallLibrary {
    Dbghelp INSTANCE = Native.load("dbghelp", Dbghelp.class);
    boolean MakeSureDirectoryPathExists(
        String DirPath   // LPCSTR
    );
}
@[Link("dbghelp")]
lib Libdbghelp
  fun MakeSureDirectoryPathExists = MakeSureDirectoryPathExists(
    DirPath : 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 MakeSureDirectoryPathExistsNative = Int32 Function(Pointer<Utf8>);
typedef MakeSureDirectoryPathExistsDart = int Function(Pointer<Utf8>);
final MakeSureDirectoryPathExists = DynamicLibrary.open('dbghelp.dll')
    .lookupFunction<MakeSureDirectoryPathExistsNative, MakeSureDirectoryPathExistsDart>('MakeSureDirectoryPathExists');
// DirPath : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MakeSureDirectoryPathExists(
  DirPath: PAnsiChar   // LPCSTR
): BOOL; stdcall;
  external 'dbghelp.dll' name 'MakeSureDirectoryPathExists';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MakeSureDirectoryPathExists"
  c_MakeSureDirectoryPathExists :: CString -> IO CInt
-- DirPath : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let makesuredirectorypathexists =
  foreign "MakeSureDirectoryPathExists"
    (string @-> returning int32_t)
(* DirPath : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dbghelp (t "dbghelp.dll"))
(cffi:use-foreign-library dbghelp)

(cffi:defcfun ("MakeSureDirectoryPathExists" make-sure-directory-path-exists :convention :stdcall) :int32
  (dir-path :string))   ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MakeSureDirectoryPathExists = Win32::API::More->new('dbghelp',
    'BOOL MakeSureDirectoryPathExists(LPCSTR DirPath)');
# my $ret = $MakeSureDirectoryPathExists->Call($DirPath);
# DirPath : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。