Delphi BPL fails to dynamically load a 64-bit package

693 Views Asked by At

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.
0

There are 0 best solutions below