1

I have a problem with dynamically loading my library when both the application and the library are compiled for 64-bit.

The following statement:

intHandle: = LoadPackage (PWideChar (strFileName));

causes the following error:

Project Project1.exe raised exception class EPackageError with message 'Can't load package F:\New Projects\bpls\exe\Win64\Debug\libs\MinhaLib.bpl.
%1 is not a valid Win32 application'.

If I recompile the application and the library for 32-bit, everything works perfectly.

I changed my library to be a DLL and use the LoadLibrary() function. In this scenario, everything works perfectly in both 32-bit and 64-bit.

For the BPL library to work in 64-bit, do I have to make any changes to it?

I'm using Delphi 10.3.

Project1.dpr:

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
   System.SysUtils, Winapi.Windows;

type
   TMyFunc = function(const intA, intB: Integer): Integer; stdcall;

begin
   try
      var myLib := ExtractFilePath(ParamStr(0)) + 'Lib.bpl';
      var intHandle := LoadPackage(myLib);
      try
         if intHandle <> 0 then
         begin
            var myFnc: TMyFunc := GetProcAddress(intHandle, PWideChar('MyFunction'));
            if @myFnc <> nil then
               Writeln('Result: ', myFnc(5, 5));
         end;

         var strV: string := '';
         Readln(strV);
      finally
         if intHandle <> 0 then
            UnloadPackage(intHandle);
      end;

   except
      on E: Exception do
         Writeln(E.ClassName, ': ', E.Message);
   end;

end.

Lib.dpk:

package Lib;

{$R *.res}
{$IFDEF IMPLICITBUILDING This IFDEF should not be used by users}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO OFF}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION OFF}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES ON}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$DEFINE DEBUG}
{$ENDIF IMPLICITBUILDING}
{$IMPLICITBUILD ON}

requires
  rtl;

contains
  Unit1 in 'Unit1.pas';

end.

Unit1.pas:

unit Unit1;

interface

function MyFunction(const intA, intB: Integer): Integer; stdcall;

implementation

function MyFunction(const intA, intB: Integer): Integer;
begin
   Result := intA + intB;
end;

exports
   MyFunction;

end.
Passella
  • 640
  • 1
  • 8
  • 23
  • 2
    The error in question is `ERROR_BAD_EXE_FORMAT`, which usually happens when a 32bit process tries to load a 64bit DLL, or a 64bit process tries to load a 32bit DLL. Does your library have a dependency on another library that is not available for 64-bit? Maybe you are linking to that library dynamically when compiling as a BPL, but linking statically when compiling as a DLL? Is it difficult to answer your question without knowing your actual setup. – Remy Lebeau Jul 02 '20 at 19:50
  • @RemyLebeau where did you see this error: ERROR_BAD_EXE_FORMAT? – Passella Jul 02 '20 at 19:58
  • 1
    "*where did you see this error: ERROR_BAD_EXE_FORMAT*" - it is the [system error code](https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes) for the text message `"%1 is not a valid Win32 application"`. The debugger is showing you a popup message in this format: `"Project raised exception class with message ''"`, where the `EPackageError.Message` is in this format: `"Can't load package . "`, where `` is the text of the error code that `LoadLibrary()` reported - error 193 (`ERROR_BAD_EXE_FORMAT`). – Remy Lebeau Jul 02 '20 at 20:35
  • 1
    Code off-site is not acceptable here. Please provide it here, in the question itself, in the form of a [mre]. – Ken White Jul 02 '20 at 22:13
  • 1
    @KenWhite I made the change – Passella Jul 03 '20 at 00:31
  • Did you check output paths for both your exe and bpl? Do the binaries show up at the expected location in the debugger/task manager ? Somthing I wonder about: Is there a specific reason for exporting the function ? (usually with DPK's one would use the "compile with runtime libs" option, and exporting the function would not be required. Though this is not likely related to your problem) – H.Hasenack Jul 06 '20 at 07:11

0 Answers0