Jump to content

SetDefaultDllDirectories and brethren


AdmiralAlkex
 Share

Recommended Posts

To use this you need KB2533623 installed (Win7/WinVista)

Links to the functions:

SetDefaultDllDirectories

AddDllDirectory

RemoveDllDirectory

UDF:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1 (stable)
 Author:         Alexander Samuelsson AKA AdmiralAlkex

 Script Function:
    See http://support.microsoft.com/kb/2533623 for details

#ce ----------------------------------------------------------------------------

Global Const $LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200
Global Const $LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000
Global Const $LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
Global Const $LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400

#cs
Specifies a default set of directories to search when the calling process loads a DLL. This search path is used when LoadLibraryEx is called with no LOAD_LIBRARY_SEARCH flags.

http://msdn.microsoft.com/en-us/library/hh310515(v=VS.85).aspx

Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
#ce
Func _SetDefaultDllDirectories($iDirectoryFlags)
    $aRet = DllCall("Kernel32.dll", "int", "SetDefaultDllDirectories", "DWORD", $iDirectoryFlags)
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc

#cs
Adds a directory to the process DLL search path.

http://msdn.microsoft.com/en-us/library/hh310513(v=VS.85).aspx

Return Value
If the function succeeds, the return value is an opaque pointer that can be passed to RemoveDllDirectory to remove the DLL from the process DLL search path.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
#ce

Func _AddDllDirectory($sNewDirectory)
    $aRet = DllCall("Kernel32.dll", "ptr", "AddDllDirectory", "wstr", $sNewDirectory)
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc

#cs
Removes a directory that was added to the process DLL search path by using AddDllDirectory.

http://msdn.microsoft.com/en-us/library/hh310514(VS.85).aspx

Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
#ce

Func _RemoveDllDirectory($pCookie)
    $aRet = DllCall("Kernel32.dll", "int", "RemoveDllDirectory", "ptr", $pCookie)
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc

Example:

#include "SetDefaultDllDirectories UDF.au3"

;Some random dll we can test DllOpen on
$sDll = "advpack.dll"

_DLL()

_SetDefaultDllDirectories($LOAD_LIBRARY_SEARCH_USER_DIRS)

_DLL()

$pCookie = _AddDllDirectory(@SystemDir)

_DLL()

_RemoveDllDirectory($pCookie)

_DLL()

_SetDefaultDllDirectories($LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)

_DLL()

Func _DLL($Whatever2 = @ScriptLineNumber)
    $hDLL = DllOpen($sDll)
    _CW($hDLL, $Whatever2)
    DllClose($hDLL)
EndFunc

Func _CW($Whatever1, $Whatever2 = @ScriptLineNumber, $Whatever3 = @error, $Whatever4 = @extended)
    ConsoleWrite("(" & $Whatever2 & ") :" & " /Current@Error:" & $Whatever3 & " /Current@Extended:" & $Whatever4 & " /VarGetType:" & VarGetType($Whatever1) & " /Value:" & $Whatever1 & @CRLF)
EndFunc

Windows XP (SP1) has the similar (but much simpler!!) SetDllDirectory.

UDF/Example:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1 (stable)
 Author:         Alexander Samuelsson AKA AdmiralAlkex

 Script Function:
    See http://msdn.microsoft.com/en-us/library/ms686203(v=VS.85).aspx for details

#ce ----------------------------------------------------------------------------

#cs   ;Example
;Some random dll we can test DllOpen on
$sDll = "bnts.dll"
$sDllPath = @WindowsDir & "\Help"

_DLL()

_SetDllDirectory($sDllPath)

_DLL()

_SetDllDirectory("")

_DLL()

Func _DLL($Whatever2 = @ScriptLineNumber)
    $hDLL = DllOpen($sDll)
    _CW($hDLL, $Whatever2)
    DllClose($hDLL)
EndFunc

Func _CW($Whatever1, $Whatever2 = @ScriptLineNumber, $Whatever3 = @error, $Whatever4 = @extended)
    ConsoleWrite("(" & $Whatever2 & ") :" & " /Current@Error:" & $Whatever3 & " /Current@Extended:" & $Whatever4 & " /VarGetType:" & VarGetType($Whatever1) & " /Value:" & $Whatever1 & @CRLF)
EndFunc
#ce

#cs
Adds a directory to the search path used to locate DLLs for the application.

http://msdn.microsoft.com/en-us/library/ms686203(v=VS.85).aspx

Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
#ce

Func _SetDllDirectory($sPathName)
    $aRet = DllCall("Kernel32.dll", "int", "SetDllDirectoryW", "wstr", $sPathName)
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...