SetDllDirectoryA
関数シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL SetDllDirectoryA(
LPCSTR lpPathName // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpPathName | LPCSTR | inoptional | 検索パスに追加するディレクトリ。このパラメーターが空文字列("")の場合、既定の DLL 検索順序からカレントディレクトリが削除されます。このパラメーターが NULL の場合、関数は既定の検索順序を復元します。 |
戻り値の型: BOOL
公式ドキュメント
アプリケーションの DLL を検索するために使用される検索パスにディレクトリを追加します。(ANSI)
戻り値
関数が成功した場合、戻り値は 0 以外の値です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
SetDllDirectory 関数は、以降のすべての LoadLibrary および LoadLibraryEx 関数の呼び出しに影響します。また、指定したディレクトリが検索パスに含まれている間、安全な DLL 検索モードを実質的に無効にします。
パッケージ化されたプロセスや保護されたプロセスとして実行されていない Win32 プロセスでは、この関数を呼び出すと、その関数を呼び出したプロセスから起動された子プロセスの DLL 検索順序にも影響します。
SetDllDirectory を呼び出した後の標準的な DLL 検索パスは次のとおりです。
- アプリケーションが読み込まれたディレクトリ。
- lpPathName パラメーターで指定したディレクトリ。
- システムディレクトリ。このディレクトリのパスを取得するには、 GetSystemDirectory 関数を使用します。このディレクトリの名前は System32 です。
- 16 ビットシステムディレクトリ。このディレクトリのパスを取得する関数はありませんが、検索の対象になります。このディレクトリの名前は System です。
- Windows ディレクトリ。このディレクトリのパスを取得するには、 GetWindowsDirectory 関数を使用します。
- PATH 環境変数に列挙されているディレクトリ。
LoadLibrary および LoadLibraryEx が使用する標準の検索パスに戻すには、 NULL を指定して SetDllDirectory を呼び出します。これにより、SafeDllSearchMode レジストリ値に基づく安全な DLL 検索モードも復元されます。
この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0502 以降として定義します。詳細については、 Using the Windows Headers を参照してください。
winbase.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして SetDllDirectory を定義します。エンコーディングに依存しないエイリアスの使用を、エンコーディングに依存しないわけではないコードと混在させると、不整合が生じ、コンパイルエラーや実行時エラーが発生する可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL SetDllDirectoryA(
LPCSTR lpPathName // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool SetDllDirectoryA(
[MarshalAs(UnmanagedType.LPStr)] string lpPathName // LPCSTR optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetDllDirectoryA(
<MarshalAs(UnmanagedType.LPStr)> lpPathName As String ' LPCSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpPathName : LPCSTR optional
Declare PtrSafe Function SetDllDirectoryA Lib "kernel32" ( _
ByVal lpPathName As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetDllDirectoryA = ctypes.windll.kernel32.SetDllDirectoryA
SetDllDirectoryA.restype = wintypes.BOOL
SetDllDirectoryA.argtypes = [
wintypes.LPCSTR, # lpPathName : LPCSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
SetDllDirectoryA = Fiddle::Function.new(
lib['SetDllDirectoryA'],
[
Fiddle::TYPE_VOIDP, # lpPathName : LPCSTR optional
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn SetDllDirectoryA(
lpPathName: *const u8 // LPCSTR optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool SetDllDirectoryA([MarshalAs(UnmanagedType.LPStr)] string lpPathName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetDllDirectoryA' -Namespace Win32 -PassThru
# $api::SetDllDirectoryA(lpPathName)#uselib "KERNEL32.dll"
#func global SetDllDirectoryA "SetDllDirectoryA" sptr
; SetDllDirectoryA lpPathName ; 戻り値は stat
; lpPathName : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global SetDllDirectoryA "SetDllDirectoryA" str
; res = SetDllDirectoryA(lpPathName)
; lpPathName : LPCSTR optional -> "str"; BOOL SetDllDirectoryA(LPCSTR lpPathName)
#uselib "KERNEL32.dll"
#cfunc global SetDllDirectoryA "SetDllDirectoryA" str
; res = SetDllDirectoryA(lpPathName)
; lpPathName : LPCSTR optional -> "str"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procSetDllDirectoryA = kernel32.NewProc("SetDllDirectoryA")
)
// lpPathName (LPCSTR optional)
r1, _, err := procSetDllDirectoryA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpPathName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetDllDirectoryA(
lpPathName: PAnsiChar // LPCSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetDllDirectoryA';result := DllCall("KERNEL32\SetDllDirectoryA"
, "AStr", lpPathName ; LPCSTR optional
, "Int") ; return: BOOL●SetDllDirectoryA(lpPathName) = DLL("KERNEL32.dll", "bool SetDllDirectoryA(char*)")
# 呼び出し: SetDllDirectoryA(lpPathName)
# lpPathName : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn SetDllDirectoryA(
lpPathName: [*c]const u8 // LPCSTR optional
) callconv(std.os.windows.WINAPI) i32;proc SetDllDirectoryA(
lpPathName: cstring # LPCSTR optional
): int32 {.importc: "SetDllDirectoryA", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int SetDllDirectoryA(
const(char)* lpPathName // LPCSTR optional
);ccall((:SetDllDirectoryA, "KERNEL32.dll"), stdcall, Int32,
(Cstring,),
lpPathName)
# lpPathName : LPCSTR optional -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetDllDirectoryA(
const char* lpPathName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetDllDirectoryA(lpPathName)
-- lpPathName : LPCSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetDllDirectoryA = lib.func('__stdcall', 'SetDllDirectoryA', 'int32_t', ['str']);
// SetDllDirectoryA(lpPathName)
// lpPathName : LPCSTR optional -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
SetDllDirectoryA: { parameters: ["buffer"], result: "i32" },
});
// lib.symbols.SetDllDirectoryA(lpPathName)
// lpPathName : LPCSTR optional -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetDllDirectoryA(
const char* lpPathName);
C, "KERNEL32.dll");
// $ffi->SetDllDirectoryA(lpPathName);
// lpPathName : LPCSTR optional
// 構造体/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);
boolean SetDllDirectoryA(
String lpPathName // LPCSTR optional
);
}@[Link("kernel32")]
lib LibKERNEL32
fun SetDllDirectoryA = SetDllDirectoryA(
lpPathName : UInt8* # LPCSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetDllDirectoryANative = Int32 Function(Pointer<Utf8>);
typedef SetDllDirectoryADart = int Function(Pointer<Utf8>);
final SetDllDirectoryA = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<SetDllDirectoryANative, SetDllDirectoryADart>('SetDllDirectoryA');
// lpPathName : LPCSTR optional -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetDllDirectoryA(
lpPathName: PAnsiChar // LPCSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetDllDirectoryA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetDllDirectoryA"
c_SetDllDirectoryA :: CString -> IO CInt
-- lpPathName : LPCSTR optional -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setdlldirectorya =
foreign "SetDllDirectoryA"
(string @-> returning int32_t)
(* lpPathName : LPCSTR optional -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("SetDllDirectoryA" set-dll-directory-a :convention :stdcall) :int32
(lp-path-name :string)) ; LPCSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetDllDirectoryA = Win32::API::More->new('KERNEL32',
'BOOL SetDllDirectoryA(LPCSTR lpPathName)');
# my $ret = $SetDllDirectoryA->Call($lpPathName);
# lpPathName : LPCSTR optional -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f SetDllDirectoryW (Unicode版) — DLL検索ディレクトリをUnicodeパスで設定する。
- f AddDllDirectory — DLL検索パスにディレクトリを追加する。
- f GetDllDirectoryA — 現在のDLL検索ディレクトリをANSI文字列で取得する。
- f GetSystemDirectoryA — Windowsシステムディレクトリのパスを取得する(ANSI版)。
- f GetWindowsDirectoryA — Windowsディレクトリのパスを取得する(ANSI版)。
- f LoadLibraryA — 指定DLLをANSIパスで読み込みハンドルを返す。
- f LoadLibraryExA — フラグ指定でDLLをANSIパスから読み込む。