Page 1 of 1

Wrapper function for LoadLibrary and FreeLibrary

Posted: 24 Sep 2019, 07:35
by jNizM
How about a wrapper function for LoadLibrary and FreeLibrary?
It would be easier to understand for general use with AutoHotkey and especially for beginners.


DllOpen(LibFileName) (calls LoadLibrary function)

Code: Select all

/*
	Function .......:	DllOpen(LibFileName)
	Parameters .....:   LibFileName -> The name of the module. This can be either a library module (a .dll file) or an executable module (an .exe file).
	Return Value ...:	- If the function succeeds, the return value is a handle to the module.
						- If the function fails, the return value is NULL.
	AHK Source .....:	Calls internally LoadLibrary function
*/
hModule := DllOpen("DllFile.dll")

DllClose(hLibModule) (calls FreeLibrary function)

Code: Select all

/*
	Function .......:	DllClose(hLibModule)
	Parameters .....:	hLibModule -> A handle to the loaded library module.
	Return Value ...:	- If the function succeeds, the return value is nonzero.
						- If the function fails, the return value is zero.
	AHK Source .....:	Calls internally FreeLibrary function
*/
DllClose(hModule)

Example Codes:

Code: Select all

; loads the specified module (bcrypt) into the address space of the calling process
hBCRYPT := DllOpen("bcrypt.dll")


; some code with the bcrypt library
DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA512_ALGORITHM, "ptr", 0, "uint", 0)


; frees the loaded module (bcrypt) and, if necessary, decrements its reference count (cleaning up resources)
DllClose(hBCRYPT)

Code: Select all

; loads the specified module (sqlite) into the address space of the calling process
hSQLITE := DllOpen(A_ScriptDir "\" (A_PtrSize = 8 ? "x64" : "x86") "\sqlite3.dll")


; some code with sqlite
Version := StrGet(DllCall("sqlite3.dll\sqlite3_libversion", "Cdecl UPtr"), "UTF-8")


; frees the loaded module (sqlite) and, if necessary, decrements its reference count (cleaning up resources)
DllClose(hSQLITE)

Re: Wrapper function for LoadLibrary and FreeLibrary

Posted: 24 Sep 2019, 09:18
by Helgef
Hi jNizM. An even simpler alternative was already added to AHK v2, see :arrow: #DllLoad. Unlke any dll loaded at run time, using #DllLoad enables the built-in optimisation for dllcalls which first parameter is a quoted string.
; frees the loaded module (bcrypt) and, if necessary, decrements its reference count (cleaning up resources)
FreeLibrary decrements the reference count, and if it reaches 0 it will free the module.

Cheers.

Re: Wrapper function for LoadLibrary and FreeLibrary

Posted: 24 Sep 2019, 09:49
by swagfag
the only thing that these functions abstract away are the types and return types. not much of a win
id rather have a raii wrapper instead, so as to avoid having to call FreeLibrary myself

the directive for compile time loading as Helgef says

Re: Wrapper function for LoadLibrary and FreeLibrary

Posted: 25 Sep 2019, 02:08
by lexikos
This makes LoadLibrary and FreeLibrary "easier to understand", but to actually make use of them, you still need to learn how to use DllCall (to call any functions).

These functions are not for "general use". They have a very specific purpose.

Re: Wrapper function for LoadLibrary and FreeLibrary

Posted: 13 Feb 2020, 21:37
by guest3456
Helgef wrote:
24 Sep 2019, 09:18
An even simpler alternative was already added to AHK v2, see :arrow: #DllLoad. Unlke any dll loaded at run time, using #DllLoad enables the built-in optimisation for dllcalls which first parameter is a quoted string.
any chance #DllLoad gets backported to v1?