ホーム › System.LibraryLoader › GetDllDirectoryA
GetDllDirectoryA
関数現在のDLL検索ディレクトリをANSI文字列で取得する。
シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
DWORD GetDllDirectoryA(
DWORD nBufferLength,
LPSTR lpBuffer // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| nBufferLength | DWORD | in | 出力バッファのサイズ(文字数単位)。 |
| lpBuffer | LPSTR | outoptional | 検索パスのアプリケーション固有の部分を受け取るバッファへのポインター。 |
戻り値の型: DWORD
公式ドキュメント
アプリケーションの DLL を検索するために使用される検索パスのうち、アプリケーション固有の部分を取得します。(ANSI)
戻り値
関数が成功した場合、戻り値は lpBuffer にコピーされた文字列の長さ(文字数単位、終端の null 文字を含まない)です。戻り値が nBufferLength より大きい場合、その値はパスに必要なバッファのサイズを示します。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、GetLastError を呼び出します。
解説(Remarks)
この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0502 以降として定義します。詳細については、Using the Windows Headers を参照してください。
メモ
winbase.h ヘッダーは GetDllDirectory をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディングニュートラルなエイリアスの使用と、エンコーディングニュートラルでないコードを混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因となる場合があります。詳細については、Conventions for Function Prototypes を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
DWORD GetDllDirectoryA(
DWORD nBufferLength,
LPSTR lpBuffer // optional
);[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern uint GetDllDirectoryA(
uint nBufferLength, // DWORD
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer // LPSTR optional, out
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetDllDirectoryA(
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 GetDllDirectoryA 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
GetDllDirectoryA = ctypes.windll.kernel32.GetDllDirectoryA
GetDllDirectoryA.restype = wintypes.DWORD
GetDllDirectoryA.argtypes = [
wintypes.DWORD, # nBufferLength : DWORD
wintypes.LPSTR, # lpBuffer : LPSTR optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetDllDirectoryA = Fiddle::Function.new(
lib['GetDllDirectoryA'],
[
-Fiddle::TYPE_INT, # nBufferLength : DWORD
Fiddle::TYPE_VOIDP, # lpBuffer : LPSTR optional, out
],
-Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn GetDllDirectoryA(
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, SetLastError = true)]
public static extern uint GetDllDirectoryA(uint nBufferLength, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetDllDirectoryA' -Namespace Win32 -PassThru
# $api::GetDllDirectoryA(nBufferLength, lpBuffer)#uselib "KERNEL32.dll"
#func global GetDllDirectoryA "GetDllDirectoryA" sptr, sptr
; GetDllDirectoryA nBufferLength, varptr(lpBuffer) ; 戻り値は stat
; nBufferLength : DWORD -> "sptr"
; lpBuffer : LPSTR optional, out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global GetDllDirectoryA "GetDllDirectoryA" int, var ; res = GetDllDirectoryA(nBufferLength, lpBuffer) ; nBufferLength : DWORD -> "int" ; lpBuffer : LPSTR optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global GetDllDirectoryA "GetDllDirectoryA" int, sptr ; res = GetDllDirectoryA(nBufferLength, varptr(lpBuffer)) ; nBufferLength : DWORD -> "int" ; lpBuffer : LPSTR optional, out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD GetDllDirectoryA(DWORD nBufferLength, LPSTR lpBuffer) #uselib "KERNEL32.dll" #cfunc global GetDllDirectoryA "GetDllDirectoryA" int, var ; res = GetDllDirectoryA(nBufferLength, lpBuffer) ; nBufferLength : DWORD -> "int" ; lpBuffer : LPSTR optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD GetDllDirectoryA(DWORD nBufferLength, LPSTR lpBuffer) #uselib "KERNEL32.dll" #cfunc global GetDllDirectoryA "GetDllDirectoryA" int, intptr ; res = GetDllDirectoryA(nBufferLength, varptr(lpBuffer)) ; nBufferLength : DWORD -> "int" ; lpBuffer : LPSTR optional, out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetDllDirectoryA = kernel32.NewProc("GetDllDirectoryA")
)
// nBufferLength (DWORD), lpBuffer (LPSTR optional, out)
r1, _, err := procGetDllDirectoryA.Call(
uintptr(nBufferLength),
uintptr(lpBuffer),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction GetDllDirectoryA(
nBufferLength: DWORD; // DWORD
lpBuffer: PAnsiChar // LPSTR optional, out
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetDllDirectoryA';result := DllCall("KERNEL32\GetDllDirectoryA"
, "UInt", nBufferLength ; DWORD
, "Ptr", lpBuffer ; LPSTR optional, out
, "UInt") ; return: DWORD●GetDllDirectoryA(nBufferLength, lpBuffer) = DLL("KERNEL32.dll", "dword GetDllDirectoryA(dword, char*)")
# 呼び出し: GetDllDirectoryA(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 GetDllDirectoryA(
nBufferLength: u32, // DWORD
lpBuffer: [*c]u8 // LPSTR optional, out
) callconv(std.os.windows.WINAPI) u32;proc GetDllDirectoryA(
nBufferLength: uint32, # DWORD
lpBuffer: ptr char # LPSTR optional, out
): uint32 {.importc: "GetDllDirectoryA", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
uint GetDllDirectoryA(
uint nBufferLength, // DWORD
char* lpBuffer // LPSTR optional, out
);ccall((:GetDllDirectoryA, "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 GetDllDirectoryA(
uint32_t nBufferLength,
char* lpBuffer);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetDllDirectoryA(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 GetDllDirectoryA = lib.func('__stdcall', 'GetDllDirectoryA', 'uint32_t', ['uint32_t', 'char *']);
// GetDllDirectoryA(nBufferLength, lpBuffer)
// nBufferLength : DWORD -> 'uint32_t'
// lpBuffer : LPSTR optional, out -> 'char *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
GetDllDirectoryA: { parameters: ["u32", "buffer"], result: "u32" },
});
// lib.symbols.GetDllDirectoryA(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 GetDllDirectoryA(
uint32_t nBufferLength,
char* lpBuffer);
C, "KERNEL32.dll");
// $ffi->GetDllDirectoryA(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 GetDllDirectoryA(
int nBufferLength, // DWORD
byte[] lpBuffer // LPSTR optional, out
);
}@[Link("kernel32")]
lib LibKERNEL32
fun GetDllDirectoryA = GetDllDirectoryA(
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 GetDllDirectoryANative = Uint32 Function(Uint32, Pointer<Utf8>);
typedef GetDllDirectoryADart = int Function(int, Pointer<Utf8>);
final GetDllDirectoryA = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<GetDllDirectoryANative, GetDllDirectoryADart>('GetDllDirectoryA');
// nBufferLength : DWORD -> Uint32
// lpBuffer : LPSTR optional, out -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetDllDirectoryA(
nBufferLength: DWORD; // DWORD
lpBuffer: PAnsiChar // LPSTR optional, out
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetDllDirectoryA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetDllDirectoryA"
c_GetDllDirectoryA :: Word32 -> CString -> IO Word32
-- nBufferLength : DWORD -> Word32
-- lpBuffer : LPSTR optional, out -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getdlldirectorya =
foreign "GetDllDirectoryA"
(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 ("GetDllDirectoryA" get-dll-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 $GetDllDirectoryA = Win32::API::More->new('KERNEL32',
'DWORD GetDllDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)');
# my $ret = $GetDllDirectoryA->Call($nBufferLength, $lpBuffer);
# nBufferLength : DWORD -> DWORD
# lpBuffer : LPSTR optional, out -> LPSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f GetDllDirectoryW (Unicode版) — 現在のDLL検索ディレクトリをUnicode文字列で取得する。
公式の関連項目
- f SetDllDirectoryA — DLL検索ディレクトリをANSIパスで設定する。