Forum > General
[SOLVED] Help debugging why LoadLibrary keeps returning zero with sample project
Gizmo:
I've recently compiled a DLL using the 64-bit MSYS2 MinGW and it compiled successfully. I can browse it using DLL Explorer.
But, I can't load it with LoadLibrary using Lazarus 2.0.12 and FPC 3.2.0 on 64-bit Windows 10.
I have created a small demo project and supplied the DLL in the hope someone might look at it and see if the problem is my code, or the DLL itself.
I have Googled the various reasons that LoadLibrary fails, and all the answers seem to relate to things like permissions or filenames. It keeps returning 0 with me, but I can't see WHY its returning zero, because the name string of the filename shows correctly in the debugger and the permissions are allowed to execute it and the path is valid. I've tried using Unicode string, rawbytestring, and normal string. I've tried using THandle and TLibHandle. Just can't get anything other than 0.
It would be a big help if someone could try it for me and see if they can get a non-zero value? Or has the DLL being made incorrectly? DLL Explorer shows all the valid functions in it.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- procedure TForm1.Button1Click(Sender: TObject);var //libFileName : Unicodestring; // libFileName : RawByteString; libFileName : string; fLibHandle : TLibHandle; // fLibHandle : THandle;begin fLibHandle := nilhandle; //libFileName:=ExtractFilePath(Application.ExeName)+'libewf-v3.dll'; /// gets full path to DLL OK libFileName:='libewf-v3.dll'; // if DLL in same folder as exe if FileExists(libFileName) then begin fLibHandle := LoadLibrary(PChar(libFileName)); // Keeps returning zero //fLibHandle := dynlibs.LoadLibrary(libFileName); //fLibHandle := LoadLibraryA(PAnsiChar(libFileName)); // Windows API method if fLibHandle<>nilhandle then begin Label1.Caption := 'Success'; end else Label1.Caption := 'Failed'; end;end;
Sample project with the DLL in question here : https://we.tl/t-FeC0iquYgX
Imants:
Have you tried to call GetLoadErrorStr if result is 0 or give full path to dll file?
Gizmo:
I've just used Depenancy Walker (old tool but still valid) and it has thrown some errors with the DLL. I think I may have compiled from wrong release link. Re-trying. Will report back. Meanwhile, if anyone can see a problem with the code, recommendations welcome.
trev:
As Imants mentioned, you have no error checking!
For example (from a macOS example I wrote):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- // load and get the dynamic library handle LibHandle := LoadLibrary(PChar('libtest.dylib')); // check whether loading was successful if LibHandle <> 0 then begin // assign address of the subroutine call to the variable cvtString Pointer(cvtString) := GetProcAddress(LibHandle, 'cvtString'); // check whether a valid address has been returned if @cvtString <> nil then WriteLn(cvtString('hello world')) // error message on no valid address else WriteLn('GetLastOSError1 = ', SysErrorMessage(GetLastOSError)); end else // error message on load failure WriteLn('GetLastOSError2 = ', SysErrorMessage(GetLastOSError));
Mr.Madguy:
Two possible reasons:
1) DllMain doesn't return true for DLL_PROCESS_ATTACH.
2) This DLL has some dependencies, that aren't satisfied.
Navigation
[0] Message Index
[#] Next page