Search code examples
dllautoit

AutoIt DLL preloading possible?


Is there a better way than opening and calling a .dll file every time?

Func getLife()    
    Local $hDLL = DllOpen("MyFirstDll.dll")
    Local $result = DllCall($hDLL, "int:cdecl", "getLife")
    If @error > 0 Then
        MsgBox(0, "Error", "Oh ups.. dll loading fail")
        Else
        MsgBox(0, "Result", $result[0])
    EndIf
    DllClose($hDLL)
EndFunc

In AutoHotkey it is possible to preload a .dll file. Is this also possible in AutoIt (to save performance)?


Solution

  • Is there a better way than opening and calling a .dll file every time?

    Load and close the .dll file once (as opposed to every function call). Example (untested):

    Global Const $g_sFileDll      = 'MyFirstDll.dll'
    Global Const $g_sErrorDllLoad = 'Failed to load .dll file.' & @LF
    Global Const $g_sErrorDllCall = 'Failed to call .dll file.' & @LF
    Global Const $g_iCountDllCall = 10
    Global Const $g_iDelayDllCall = 1000
    
    Global       $g_hDLL          = DllOpen($g_sFileDll)
    
    If $g_hDLL = -1 Then    
        ConsoleWrite($g_sErrorDllLoad)
        Exit    
    EndIf
    
    For $i1 = 1 To $g_iCountDllCall
        getLife($g_hDLL)
        Sleep($g_iDelayDllCall)    
    Next
    
    DllClose($g_hDLL)
    Exit
    
    Func getLife(ByRef $hDLL)
        Local $aResult = DllCall($hDLL, "int:cdecl", "getLife")
    
        If @error Then    
            ConsoleWrite($g_sErrorDllCall)    
        Else    
            ConsoleWrite('Result: ' & $aResult[0] & @LF)    
        EndIf
    
    EndFunc