Win32 API 日本語リファレンス
ホーム › System.Environment › GetCurrentDirectoryA

GetCurrentDirectoryA

関数
現在のプロセスのカレントディレクトリを取得する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapi

シグネチャ

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

DWORD GetCurrentDirectoryA(
    DWORD nBufferLength,
    LPSTR lpBuffer   // optional
);

パラメーター

名前型方向説明
nBufferLengthDWORDin現在のディレクトリ文字列を格納するバッファーの長さ (TCHARs 単位)。 バッファーの長さには、終端の null 文字を格納する領域を含める必要があります。
lpBufferLPSTRoutoptional

現在のディレクトリ文字列を受け取るバッファーへのポインター。この null 終端文字列は、 現在のディレクトリへの絶対パスを表します。

必要なバッファーサイズを調べるには、このパラメーターに NULL を指定し、 nBufferLength パラメーターに 0 を指定します。

戻り値の型: DWORD

公式ドキュメント

現在のプロセスの現在のディレクトリを取得します。

戻り値

関数が成功した場合、戻り値はバッファーに書き込まれた文字数であり、 終端の null 文字は含みません。

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

lpBuffer が指すバッファーの大きさが十分でない場合、戻り値には必要なバッファーのサイズが 文字数単位で格納されます。この値には終端の null 文字が含まれます。

解説(Remarks)

各プロセスは、次の 2 つの部分から成る 1 つの現在のディレクトリを持ちます。

現在のディレクトリを設定するには、 SetCurrentDirectory 関数を使用します。

マルチスレッドアプリケーションおよび共有ライブラリのコードでは、 GetCurrentDirectory 関数を使用せず、相対パス名の使用も避けてください。SetCurrentDirectory 関数によって書き込まれる現在のディレクトリの状態は、プロセスごとにグローバル変数として格納されます。そのため、マルチスレッドアプリケーションでは、この値の読み取りや設定を行う他のスレッドによってデータが破損するおそれがあり、この値を確実に使用することはできません。この制限は SetCurrentDirectory 関数および GetFullPathName 関数にも当てはまります。例外は、アプリケーションが単一スレッドで実行されることが保証されている場合です。たとえば、追加のスレッドを作成する前に、メインスレッドでコマンドライン引数の文字列からファイル名を解析する場合などです。マルチスレッドアプリケーションや共有ライブラリのコードで相対パス名を使用すると、予測できない結果になる可能性があり、サポートされていません。

Windows 8 および Windows Server 2012 では、この関数は次のテクノロジでサポートされます。

テクノロジ サポート
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 透過的フェールオーバー (TFO) はい
スケールアウト ファイル共有 (SO) を使用する SMB 3.0 はい
クラスター共有ボリューム ファイル システム (CsvFS) はい
回復性のあるファイル システム (ReFS) はい

例

例については、 現在のディレクトリの変更を参照してください。

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

各言語での呼び出し定義

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

DWORD GetCurrentDirectoryA(
    DWORD nBufferLength,
    LPSTR lpBuffer   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint GetCurrentDirectoryA(
    uint nBufferLength,   // DWORD
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer   // LPSTR optional, out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function GetCurrentDirectoryA(
    nBufferLength As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPStr)> lpBuffer As System.Text.StringBuilder   ' LPSTR optional, out
) As UInteger
End Function
' nBufferLength : DWORD
' lpBuffer : LPSTR optional, out
Declare PtrSafe Function GetCurrentDirectoryA Lib "kernel32" ( _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetCurrentDirectoryA = ctypes.windll.kernel32.GetCurrentDirectoryA
GetCurrentDirectoryA.restype = wintypes.DWORD
GetCurrentDirectoryA.argtypes = [
    wintypes.DWORD,  # nBufferLength : DWORD
    wintypes.LPSTR,  # lpBuffer : LPSTR optional, out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetCurrentDirectoryA = Fiddle::Function.new(
  lib['GetCurrentDirectoryA'],
  [
    -Fiddle::TYPE_INT,  # nBufferLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPSTR optional, out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn GetCurrentDirectoryA(
        nBufferLength: u32,  // DWORD
        lpBuffer: *mut u8  // LPSTR optional, out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi)]
public static extern uint GetCurrentDirectoryA(uint nBufferLength, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetCurrentDirectoryA' -Namespace Win32 -PassThru
# $api::GetCurrentDirectoryA(nBufferLength, lpBuffer)
#uselib "KERNEL32.dll"
#func global GetCurrentDirectoryA "GetCurrentDirectoryA" sptr, sptr
; GetCurrentDirectoryA nBufferLength, varptr(lpBuffer)   ; 戻り値は stat
; nBufferLength : DWORD -> "sptr"
; lpBuffer : LPSTR optional, out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetCurrentDirectoryA "GetCurrentDirectoryA" int, var
; res = GetCurrentDirectoryA(nBufferLength, lpBuffer)
; nBufferLength : DWORD -> "int"
; lpBuffer : LPSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)
#uselib "KERNEL32.dll"
#cfunc global GetCurrentDirectoryA "GetCurrentDirectoryA" int, var
; res = GetCurrentDirectoryA(nBufferLength, lpBuffer)
; nBufferLength : DWORD -> "int"
; lpBuffer : LPSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetCurrentDirectoryA = kernel32.NewProc("GetCurrentDirectoryA")
)

// nBufferLength (DWORD), lpBuffer (LPSTR optional, out)
r1, _, err := procGetCurrentDirectoryA.Call(
	uintptr(nBufferLength),
	uintptr(lpBuffer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetCurrentDirectoryA(
  nBufferLength: DWORD;   // DWORD
  lpBuffer: PAnsiChar   // LPSTR optional, out
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetCurrentDirectoryA';
result := DllCall("KERNEL32\GetCurrentDirectoryA"
    , "UInt", nBufferLength   ; DWORD
    , "Ptr", lpBuffer   ; LPSTR optional, out
    , "UInt")   ; return: DWORD
●GetCurrentDirectoryA(nBufferLength, lpBuffer) = DLL("KERNEL32.dll", "dword GetCurrentDirectoryA(dword, char*)")
# 呼び出し: GetCurrentDirectoryA(nBufferLength, lpBuffer)
# nBufferLength : DWORD -> "dword"
# lpBuffer : LPSTR optional, out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn GetCurrentDirectoryA(
    nBufferLength: u32, // DWORD
    lpBuffer: [*c]u8 // LPSTR optional, out
) callconv(std.os.windows.WINAPI) u32;
proc GetCurrentDirectoryA(
    nBufferLength: uint32,  # DWORD
    lpBuffer: ptr char  # LPSTR optional, out
): uint32 {.importc: "GetCurrentDirectoryA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
uint GetCurrentDirectoryA(
    uint nBufferLength,   // DWORD
    char* lpBuffer   // LPSTR optional, out
);
ccall((:GetCurrentDirectoryA, "KERNEL32.dll"), stdcall, UInt32,
      (UInt32, Ptr{UInt8}),
      nBufferLength, lpBuffer)
# nBufferLength : DWORD -> UInt32
# lpBuffer : LPSTR optional, out -> Ptr{UInt8}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetCurrentDirectoryA(
    uint32_t nBufferLength,
    char* lpBuffer);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetCurrentDirectoryA(nBufferLength, lpBuffer)
-- nBufferLength : DWORD
-- lpBuffer : LPSTR optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetCurrentDirectoryA = lib.func('__stdcall', 'GetCurrentDirectoryA', 'uint32_t', ['uint32_t', 'char *']);
// GetCurrentDirectoryA(nBufferLength, lpBuffer)
// nBufferLength : DWORD -> 'uint32_t'
// lpBuffer : LPSTR optional, out -> 'char *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetCurrentDirectoryA: { parameters: ["u32", "buffer"], result: "u32" },
});
// lib.symbols.GetCurrentDirectoryA(nBufferLength, lpBuffer)
// nBufferLength : DWORD -> "u32"
// lpBuffer : LPSTR optional, out -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetCurrentDirectoryA(
    uint32_t nBufferLength,
    char* lpBuffer);
C, "KERNEL32.dll");
// $ffi->GetCurrentDirectoryA(nBufferLength, lpBuffer);
// nBufferLength : DWORD
// lpBuffer : LPSTR optional, out
// 構造体/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);
    int GetCurrentDirectoryA(
        int nBufferLength,   // DWORD
        byte[] lpBuffer   // LPSTR optional, out
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun GetCurrentDirectoryA = GetCurrentDirectoryA(
    nBufferLength : UInt32,   # DWORD
    lpBuffer : UInt8*   # LPSTR optional, out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetCurrentDirectoryANative = Uint32 Function(Uint32, Pointer<Utf8>);
typedef GetCurrentDirectoryADart = int Function(int, Pointer<Utf8>);
final GetCurrentDirectoryA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetCurrentDirectoryANative, GetCurrentDirectoryADart>('GetCurrentDirectoryA');
// nBufferLength : DWORD -> Uint32
// lpBuffer : LPSTR optional, out -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetCurrentDirectoryA(
  nBufferLength: DWORD;   // DWORD
  lpBuffer: PAnsiChar   // LPSTR optional, out
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetCurrentDirectoryA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetCurrentDirectoryA"
  c_GetCurrentDirectoryA :: Word32 -> CString -> IO Word32
-- nBufferLength : DWORD -> Word32
-- lpBuffer : LPSTR optional, out -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getcurrentdirectorya =
  foreign "GetCurrentDirectoryA"
    (uint32_t @-> string @-> returning uint32_t)
(* nBufferLength : DWORD -> uint32_t *)
(* lpBuffer : LPSTR optional, out -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetCurrentDirectoryA" get-current-directory-a :convention :stdcall) :uint32
  (n-buffer-length :uint32)   ; DWORD
  (lp-buffer :pointer))   ; LPSTR optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetCurrentDirectoryA = Win32::API::More->new('KERNEL32',
    'DWORD GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)');
# my $ret = $GetCurrentDirectoryA->Call($nBufferLength, $lpBuffer);
# nBufferLength : DWORD -> DWORD
# lpBuffer : LPSTR optional, out -> LPSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目