Wrapper function for LoadLibrary and FreeLibrary

Propose new features and changes
User avatar
jNizM
Posts: 2508
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Wrapper function for LoadLibrary and FreeLibrary

24 Sep 2019, 07:35

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)
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Helgef
Posts: 3977
Joined: 17 Jul 2016, 01:02
Contact:

Re: Wrapper function for LoadLibrary and FreeLibrary

24 Sep 2019, 09:18

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.
swagfag
Posts: 2981
Joined: 11 Jan 2017, 17:59

Re: Wrapper function for LoadLibrary and FreeLibrary

24 Sep 2019, 09:49

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
lexikos
Posts: 6652
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Wrapper function for LoadLibrary and FreeLibrary

25 Sep 2019, 02:08

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.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 11 guests