How to load mingw version DLL?

Ask for help, how to use AHK_H, etc.
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

How to load mingw version DLL?

Post by arcticir » 20 Apr 2019, 07:56

I found a libbrotlidec.dll to decode the br data, can view it normally by "DLL Export Viewer".

But it can't be used normally, "LoadLibrary" returns 0.

How can I load it normally?


brotli-1.0.7_1-win32-mingw:
https://curl.haxx.se/windows/dl-7.64.1_1/brotli-1.0.7_1-win32-mingw.zip

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to load mingw version DLL?

Post by swagfag » 20 Apr 2019, 08:52

what problems do u have loading it? a_lasterror??
libbrotlidec.dll is a 32 bit dll that depends on libbrotlicommon.dll. did u loadlib libbrotlicommon.dll beforehand, or do u have it in ur script dir?

arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: How to load mingw version DLL?

Post by arcticir » 20 Apr 2019, 09:57

thank you very much. :shock:

MrHue
Posts: 17
Joined: 01 Oct 2015, 02:51

Re: How to load mingw version DLL?

Post by MrHue » 13 Jan 2022, 05:07

For x64 https://packages.msys2.org/package/mingw-w64-x86_64-brotli

Code: Select all

br_decode(byref pComprData, comprLen, ByRef uncompr) {
	static brotli := A_ScriptDir "\libbrotlidec.dll"	
	; specify exact path because LoadLibary may otherwise return module not found
	if !hlib1 := DllCall("LoadLibrary", Str, A_ScriptDir "\libbrotlicommon.dll", Ptr)
		throw Exception("Can't load libbrotlicommon.dll. A_LastError #" A_LastError)
	if !hlib2 := DllCall("LoadLibrary", Str, brotli, Ptr)
		throw Exception("Can't load " brotli ". A_LastError #" A_LastError)
	Loop
	{
		chunk := VarSetCapacity(uncompr, comprLen*A_Index*20)		; create large output buffer
	      	res := DllCall(brotli "\BrotliDecoderDecompress", "UPtr", comprLen, "Ptr", &pComprData, "UPtrP", chunk, "Ptr", &uncompr)
		switch res
		{
			case 0: if (A_Index>5)
					throw Exception(A_Index ": BROTLI_DECODER_RESULT_ERROR " comprLen " " chunk)
			case 1: break	; SUCCESS
			case 2: throw Exception(A_Index ": BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT " comprLen " " chunk)
		}
	}	; retry if 0 (BROTLI_DECODER_RESULT_ERROR) or 3 (BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT)
	DllCall("FreeLibrary", Ptr, hLib2)
	DllCall("FreeLibrary", Ptr, hLib1)
	Return chunk
}
For LoadLibrary #126 error try this

Code: Select all

LoadLibrary(dll)
{
	static IMAGE_DOS_SIGNATURE := 0x5A4D
		, IMAGE_NT_SIGNATURE := 0x4550
		, Machines := {0x014c: "I386", 0x0200: "IA64", 0x8664: "AMD64"}

	; Try add fullpath to prevent module not found error because LoadLibrary does not always search in current / relative dir
	; On some PCs LoadLibrary will search in the current path, but not in others... Not sure why
	if !InStr(dll, "\") 
	{	
		if FileExist(A_WorkingDir "\" dll)
			dll := A_WorkingDir "\" dll	
		else if FileExist(A_ScriptDir "\" dll)
			dll := A_ScriptDir "\" dll	
	} else if InStr(dll, ".\") && FileExist(dll) {
		cc := DllCall("GetFullPathName", "str", dll, "uint", 0, "ptr", 0, "ptr", 0, "uint")
		VarSetCapacity(buf, cc<<!!A_IsUnicode)
		DllCall("GetFullPathName", "str", dll, "uint", cc, "str", buf, "ptr", 0, "uint")
		dll := buf
	}

	; if hModule := DllCall("LoadLibrary", "Str", dll)
	; Use LoadLibraryEx to load dependencies as well
	if hModule := DllCall("LoadLibraryEx", "Str", dll, "Ptr", 0, "UInt", 0x1100, "UPtr")
		return hModule

	if (A_LastError=126)	; module not found
	{
		; Check if dll file actually exists
		if !FileExist(dll)
			throw Exception("#126: " GetLastError(126) ". " dll " does not exist (check spelling/path).")

		; Check if 32 vs 64 bit Ahk & DLL
		; https://www.autohotkey.com/boards/viewtopic.php?t=26713
		if ((file := FileOpen(dll, "r"))) {
			if (file.ReadUShort() == IMAGE_DOS_SIGNATURE) {		; DOS header
				file.Seek(60)
				e_lfanew := file.ReadInt()
				file.Seek(e_lfanew)
				if (file.ReadUInt() == IMAGE_NT_SIGNATURE) {	; PE header
					file.Seek(e_lfanew + 4)
					type := Machines[file.ReadUShort()]
				}
			}
		}
		file.Close()
		AHK := A_IsUnicode ? "Unicode" : "Ansi"
		AHK .= (A_PtrSize = 8) ? " 64" : " 32"
		if !Instr(type, (A_PtrSize = 8) ? "64" : "86")
			throw Exception("#126: " GetLastError(126) ". " AHK "bit Autohotkey can't load " type " " dll " (wrong architecture).")

		; Don't know, give message to use Process Monitor
		; https://stackoverflow.com/questions/14361992/dll-load-library-error-code-126
		throw Exception("#126: " GetLastError(126) ". Use Process Monitor to see what file Autohotkey.exe failed to load.")
	}
	throw Exception("#" A_LastError ": " GetLastError())
}

GetLastError(Error=0)
{
	VarSetCapacity(ErrorString, 1024)
	IfEqual, Error, 0, SetEnv, Error, %A_LastError%
	if DllCall("FormatMessage" 
		, "UINT", 0x1000	; FORMAT_MESSAGE_FROM_SYSTEM: The function should search the system message-table resource(s) for the requested message. 
		, "PTR", 0		; A handle to the module that contains the message table to search. 
		, "UINT", Error
		, "UINT", 0             ; Language-ID is automatically retreived 
		, "Str",  ErrorString 
		, "UINT", 1024          ; Buffer-Length 
		, "STR", "")		; "str",  "")            ;An array of values that are used as insert values in the formatted message. (not used) 
		return ErrorString
}

Post Reply

Return to “Ask for Help”