SetDllDirectoryW
関数シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL SetDllDirectoryW(
LPCWSTR lpPathName // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpPathName | LPCWSTR | inoptional | 検索パスに追加するディレクトリ。このパラメーターが空文字列("")の場合、呼び出しによってデフォルトの DLL 検索順序からカレントディレクトリが削除されます。このパラメーターが NULL の場合、関数はデフォルトの検索順序を復元します。 |
戻り値の型: BOOL
公式ドキュメント
アプリケーションの DLL を検索するために使用される検索パスにディレクトリを追加します。(Unicode)
戻り値
関数が成功すると、戻り値は 0 以外になります。
関数が失敗すると、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出してください。
解説(Remarks)
SetDllDirectory 関数は、以降のすべての LoadLibrary および LoadLibraryEx 関数の呼び出しに影響します。また、指定したディレクトリが検索パスに含まれている間、安全な DLL 検索モードを実質的に無効化します。
パッケージ化されたプロセスでも保護されたプロセスでもない Win32 プロセスの場合、この関数を呼び出すと、その関数を呼び出したプロセスから起動された子プロセスの DLL 検索順序にも影響します。
SetDllDirectory を呼び出した後の標準的な DLL 検索パスは次のとおりです。
- アプリケーションが読み込まれたディレクトリ。
- lpPathName パラメーターで指定されたディレクトリ。
- システムディレクトリ。このディレクトリのパスを取得するには、 GetSystemDirectory 関数を使用してください。このディレクトリ名は System32 です。
- 16 ビットのシステムディレクトリ。このディレクトリのパスを取得する関数はありませんが、検索の対象になります。このディレクトリ名は System です。
- Windows ディレクトリ。このディレクトリのパスを取得するには、 GetWindowsDirectory 関数を使用してください。
- PATH 環境変数に列挙されているディレクトリ。
LoadLibrary および LoadLibraryEx が使用する標準の検索パスに戻すには、 SetDllDirectory を NULL を指定して呼び出してください。これにより、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 (Unicode / -W)
#include <windows.h>
BOOL SetDllDirectoryW(
LPCWSTR lpPathName // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetDllDirectoryW(
[MarshalAs(UnmanagedType.LPWStr)] string lpPathName // LPCWSTR optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetDllDirectoryW(
<MarshalAs(UnmanagedType.LPWStr)> lpPathName As String ' LPCWSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpPathName : LPCWSTR optional
Declare PtrSafe Function SetDllDirectoryW Lib "kernel32" ( _
ByVal lpPathName As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetDllDirectoryW = ctypes.windll.kernel32.SetDllDirectoryW
SetDllDirectoryW.restype = wintypes.BOOL
SetDllDirectoryW.argtypes = [
wintypes.LPCWSTR, # lpPathName : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
SetDllDirectoryW = Fiddle::Function.new(
lib['SetDllDirectoryW'],
[
Fiddle::TYPE_VOIDP, # lpPathName : LPCWSTR optional
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn SetDllDirectoryW(
lpPathName: *const u16 // LPCWSTR optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetDllDirectoryW([MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetDllDirectoryW' -Namespace Win32 -PassThru
# $api::SetDllDirectoryW(lpPathName)#uselib "KERNEL32.dll"
#func global SetDllDirectoryW "SetDllDirectoryW" wptr
; SetDllDirectoryW lpPathName ; 戻り値は stat
; lpPathName : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global SetDllDirectoryW "SetDllDirectoryW" wstr
; res = SetDllDirectoryW(lpPathName)
; lpPathName : LPCWSTR optional -> "wstr"; BOOL SetDllDirectoryW(LPCWSTR lpPathName)
#uselib "KERNEL32.dll"
#cfunc global SetDllDirectoryW "SetDllDirectoryW" wstr
; res = SetDllDirectoryW(lpPathName)
; lpPathName : LPCWSTR optional -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procSetDllDirectoryW = kernel32.NewProc("SetDllDirectoryW")
)
// lpPathName (LPCWSTR optional)
r1, _, err := procSetDllDirectoryW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPathName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetDllDirectoryW(
lpPathName: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetDllDirectoryW';result := DllCall("KERNEL32\SetDllDirectoryW"
, "WStr", lpPathName ; LPCWSTR optional
, "Int") ; return: BOOL●SetDllDirectoryW(lpPathName) = DLL("KERNEL32.dll", "bool SetDllDirectoryW(char*)")
# 呼び出し: SetDllDirectoryW(lpPathName)
# lpPathName : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn SetDllDirectoryW(
lpPathName: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc SetDllDirectoryW(
lpPathName: WideCString # LPCWSTR optional
): int32 {.importc: "SetDllDirectoryW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
int SetDllDirectoryW(
const(wchar)* lpPathName // LPCWSTR optional
);ccall((:SetDllDirectoryW, "KERNEL32.dll"), stdcall, Int32,
(Cwstring,),
lpPathName)
# lpPathName : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t SetDllDirectoryW(
const uint16_t* lpPathName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetDllDirectoryW(lpPathName)
-- lpPathName : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetDllDirectoryW = lib.func('__stdcall', 'SetDllDirectoryW', 'int32_t', ['str16']);
// SetDllDirectoryW(lpPathName)
// lpPathName : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
SetDllDirectoryW: { parameters: ["buffer"], result: "i32" },
});
// lib.symbols.SetDllDirectoryW(lpPathName)
// lpPathName : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetDllDirectoryW(
const uint16_t* lpPathName);
C, "KERNEL32.dll");
// $ffi->SetDllDirectoryW(lpPathName);
// lpPathName : LPCWSTR 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.UNICODE_OPTIONS);
boolean SetDllDirectoryW(
WString lpPathName // LPCWSTR optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun SetDllDirectoryW = SetDllDirectoryW(
lpPathName : UInt16* # LPCWSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetDllDirectoryWNative = Int32 Function(Pointer<Utf16>);
typedef SetDllDirectoryWDart = int Function(Pointer<Utf16>);
final SetDllDirectoryW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<SetDllDirectoryWNative, SetDllDirectoryWDart>('SetDllDirectoryW');
// lpPathName : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetDllDirectoryW(
lpPathName: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetDllDirectoryW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetDllDirectoryW"
c_SetDllDirectoryW :: CWString -> IO CInt
-- lpPathName : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setdlldirectoryw =
foreign "SetDllDirectoryW"
((ptr uint16_t) @-> returning int32_t)
(* lpPathName : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("SetDllDirectoryW" set-dll-directory-w :convention :stdcall) :int32
(lp-path-name (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetDllDirectoryW = Win32::API::More->new('KERNEL32',
'BOOL SetDllDirectoryW(LPCWSTR lpPathName)');
# my $ret = $SetDllDirectoryW->Call($lpPathName);
# lpPathName : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f SetDllDirectoryA (ANSI版) — DLL検索ディレクトリをANSIパスで設定する。
- f AddDllDirectory — DLL検索パスにディレクトリを追加する。
- f GetDllDirectoryW — 現在のDLL検索ディレクトリをUnicode文字列で取得する。
- f GetSystemDirectoryW — Windowsシステムディレクトリのパスを取得する(Unicode版)。
- f GetWindowsDirectoryW — Windowsディレクトリのパスを取得する(Unicode版)。
- f LoadLibraryW — 指定DLLをUnicodeパスで読み込みハンドルを返す。
- f LoadLibraryExW — フラグ指定でDLLをUnicodeパスから読み込む。