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
);

パラメーター

名前方向説明
nBufferLengthDWORDinlpBufferの文字数(終端含む)。
lpBufferLPSTRoutoptionalカレントディレクトリのパスを受け取るANSI出力バッファ。

戻り値の型: DWORD

各言語での呼び出し定義

// 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 を使用。

関連項目

文字セット違い