willichan

__DllFindOpen() -- A DllOpen wraparound function

1 post in this topic

Here is a small function that I use if I need to open a DLL, but it could be in any one of several locations on the target PC.  You can pass an explicit path as a string variable, or pass an array of strings with multiple paths to check.  It will scan through the array, until it is successfully able to open the DLL then returns the handle.

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __DllFindOpen
; Description ...:
; Syntax ........: __DllFindOpen($sDllName[, $vSearchPath = @scriptdir])
; Parameters ....: $sDllName            - dll file to open
;                  $vSearchPath         - [optional] A string with an explicit path to the DLL, or an 1 dimentional array of
;                                         paths to search
; Return values .: Success: Returns a dll "handle" to be used with subsequent Dll functions
;                  Failure: Returns "" and sets @error
;                           -1: dll not found or could not open
;                           -2: $vSearchPath has more than 1 dimension
;                           -3: $vSearchPath is an improper variable type
; Author ........: David Williams (willichan)
; Modified ......:
; Remarks .......:
; Related .......: DllOpen
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __DllFindOpen($sDllName, $vSearchPath = Default)
    Local $hResult, $i
    If $vSearchPath = Default Then $vSearchPath = @ScriptDir
    If IsString($vSearchPath) Then ;This is easy.  Just one path to look at
        If StringRight($vSearchPath, 1) <> "\" Then $vSearchPath &= "\"
        $hResult = DllOpen($vSearchPath & $sDllName)
        If $hResult = -1 Then
            SetError(-1, 0, "") ;dll not found or could not open
        Else
            Return $hResult
        EndIf
    ElseIf IsArray($vSearchPath) Then ;Search the array for the DLL
        If UBound($vSearchPath, 0) > 1 Then SetError(-2, 0, "") ;too many dimentions in array
        For $i = 0 To UBound($vSearchPath) - 1
            If ($vSearchPath[$i] = Default) Or IsString($vSearchPath[$i]) Then ;only try the strings or Default.  Don't want to get into deep recursion.
                $hResult = __DllFindOpen($sDllName, $vSearchPath[$i])
                If @error = 0 Then Return $hResult
            EndIf
        Next
        SetError(-1, 0, "") ;didn't find it
    Else ;Some dummy wants to see how dummy-proof this is
        SetError(-3, 0, "") ;not a string or array
    EndIf
EndFunc   ;==>__DllFindOpen

Share this post


Link to post
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