# Load DLL to memory and persist

## Recommended Posts

This is relevant

Quote

Yes. Call LoadLibrary() on that DLL. That will increase the internal reference count. FreeLibrary() only unloads a DLL when its internal reference count drops to zero. If you LoadLibrary and never FreeLibrary, the DLL will be stuck in memory for the lifetime of your process.

If you're running into a situation where somebody is calling FreeLibrary() on your DLL and causing it to be removed from memory while you're still using it, you probably have a bug - a disagreement or misunderstanding about who owns the DLL and is responsible for releasing it. A bug that should be fixed rather than worked around by a LoadLibrary hack.

I use several UDFs on the Forum to do various things.  Those UDFs work very well.

Effectively the UDFs are DLL wrappers, that make it possible to access DLL functions easily without the long hard slog of DLLCall() every time.

However, I have now run into the issue that multiple UDF DLLCalls are slow. Not mind numbingly slow, but slow enough to become noticeable with a large of repeated function calls.

So I was wondering, is it possible to "load a DLL into memory" and leave it there for the duration of my script's lifetime, avoid repeated DLL on-disk reads with a persistent in memory DLL?

From Microsoft

So next question, (a) how do I do this with AutoIt (b) How would this impact on standard AutoIt type DLL calls?

The point is speed.  Is there a different approach?

Or am I barking up the wrong tree?

Skysnake

Edited by Skysnake

Skysnake

Why is the snake in the sky?

You could try to use the Func _WinAPI_GetModuleHandleEx with the flag $GET_MODULE_HANDLE_EX_FLAG_PIN. I would be very interested to know the results ... Not much of a signature but working on it... Spoiler ##### Link to post ##### Share on other sites 1 hour ago, Nine said: _WinAPI_GetModuleHandleEx﻿ very interesting. I will need to mod the UDF to see what happens. Will try ... this is very new to me Skysnake Why is the snake in the sky? ##### Link to post ##### Share on other sites err.number is: 0x80040154 err.windescription: Class not registered err.description is: err.source is: err.helpfile is: err.helpcontext is: err.lastdllerror is: 0 err.scriptline is: 797 err.retcode is: 0x00000000 I simply replaced the DllOpen with the _WinAPI_GetModuleHandleEx ;$__hDll_QPDF = DllOpen($sDLLFile) ; handle to DLL 2019.03.28$__hDll_QPDF =  _WinAPI_GetModuleHandleEx($sDLLFile,$GET_MODULE_HANDLE_EX_FLAG_PIN)

But this is obviously not the way to do it.

Edited by Skysnake

Skysnake

Why is the snake in the sky?

##### Share on other sites

Your DLLs will remain stored in memory as long as your script is running. The problems you experience when a large number of repetitive function calls become slow have nothing to do with the DLLs not being constantly stored in memory.

It's simply because AutoIt is an interpreted language that's not fast for a large number of repetitive tasks.

The only solution to this kind of bottleneck problems is to replace the critical parts of the code with true compiled code.

There are two ways you can do this in AutoIt. The absolute easiest way is to use C# or VB.NET code through the .NET framework.

The other way is to create your own DLL with a language capable of creating DLLs.

## Create an account

Register a new account

• ### Similar Content

• By DevMode
welcome
I need help dealing with a C++ DLL
Specify that I need help with how to use this function
struct libusb_device; typedef struct libusb_context libusb_context; ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,libusb_device ***list); My problem is how to deal with "libusb_device ***list"
my code
$libusb_device = DllStructCreate("PTR") Func libusb_init()$Var = DllCall($libusb0, "ptr", "libusb_init",'ptr',$_libusb_device_handle_Ptr) If @error Then Return False Return $Var[1] EndFunc Func libusb_get_device_list() MsgBox(0,VarGetType($_libusb_device_handle_Ptr),$_libusb_device_handle_Ptr)$Var = DllCall($libusb0, "int", "libusb_get_device_list","ptr",$_libusb_device_handle_Ptr,"ptr*",POINTER(POINTER($libusb_device)))$Error = @error If $Error Then Return False MsgBox(0,VarGetType($Var),$Error&@CRLF&$Var) _ArrayDisplay($Var) Return$Var[1] EndFunc
Thank you
iLibUSB.dll iLibUSB_32.dll libusb.h

• Hi all,
My programming knowledge is very basic.
I have an old script that creates shares and assign permissions. It normally registers SetAcl.ocx if necessary and creates an object to assign permissions.
The command that registers SetAcl was apparently working fine under Windows 7 but is not working under Windows 10.
RunWait("regsvr32.exe path\to\setacl.ocx /s", "", @SW_HIDE) As I'm logged in as admin, I changed this command to :
RunAsWait(@UserName, "", "", 0, "regsvr32.exe path\to\setacl.ocx /s", "", @SW_HIDE) It seems to terminate correctly but the script still doesn't work as expected.
To check that, I've created that small script :
Local $objSetAcl = ObjCreate("SETACL.SetACLCtrl.1") If IsObj($objSetAcl) Then ConsoleWrite("Object successfully created." & @CRLF) Else ConsoleWrite("Object not created. Registering SetAcl.ocx" & @CRLF) Local $result = RunAsWait(@UserName, "", "", 0, "regsvr32.exe path\to\setacl.ocx /s", "", @SW_HIDE); Use of my admin username to elevate CMD ConsoleWrite("Return code : " &$result & @CRLF) ConsoleWrite("Creating object" & @CRLF) $objSetAcl = ObjCreate("SETACL.SetACLCtrl.1") If IsObj($objSetAcl) Then ConsoleWrite("Object successfully created." & @CRLF) Else ConsoleWrite("Object creation failed." & @CRLF) EndIf EndIf It tries to register SetAcl.ocx, return code 0 seems to be fine but still can't use SetAcl.
But if I go to CMD as admin, run the regsvr32 command and restart my script, it can create the object without issue.
I know my poor knowledge makes me miss something. Anyone can help me figure this out ?

• Dear members of the forum,
I'm working on a project in which I have to use Image recognition technique.
Due to client restrictions, I couldn't use AutoIt for this project.
Is there a way to use this DLL "ImageSearchDLL.dll" (which is used to do image recognition steps in AutoIt) in VB.Net to achieve the same result?
I have used this DLL few years before and got good results. If there is a latest version of this DLL and if you can share it, that will be helpful too.
Any guidance is deeply appreciated.

• Hi AutoIt Scripters/Programmers. I have a question about MIME Tools for Notepad++:
I've recently found a UDF about Base64 in forum, but they can't decode\encode correctly some emojis , other UTFs and etc. so i decided to use mimeTools.dll of Notepad++ or main site
My problem is how to use this dll in AutoIt Language?

• Good morning Forums
In these days, I am working on a project that involved me to use some Windows APIs to obtain some information about Terminal Servers.
I'm doing this using wtsapi32.dll in a VBA Project, but, the lack of knowledge about few things threated in the articles make this quite difficult to
implement and understand at the same time.
The most difficult thing I'm facing is "translating" C/C++ functions or struct in VBA when pointers are used, or pointers of pointers, and so on.
Since VBA seems to not have a pointer type, to make those functions work I need to implement other functions taken from other DLLs, and this confuses me a lot.
For example, starting from this code, I splitted all the functions and all the definitions to understand why they are there, and why I need to use them.
At the end, I've found out that the code I was going to implement starting from the functions provided in the Microsoft Docs won't be ever be able to work without some supplementary functions which are not mentioned anywhere.
So, I was wondering if someone would please point me out to a good and practical exaplanation about pointers (in general) or specifically for VBA, because I need to use them quite often in these days, and I'd like to understand what I am doing.

Best Regards and Stay at home 🏡

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...