在将应用程序和库都编译为64位时,我无法动态加载库。
以下语句:
intHandle: = LoadPackage (PWideChar (strFileName));
导致以下错误:
Project Project1.exe引发异常类EPackageError,并显示消息“无法加载程序包F:\ New Projects \ bpls \ exe \ Win64 \ Debug \ libs \ MinhaLib.bpl。
%1不是有效的Win32应用程序”。
如果我将应用程序和库重新编译为32位,则一切正常。
我将库更改为DLL,并使用了Loadlibrary()
函数。在这种情况下,一切都可以在32位和64位上完美运行。
要使bpl库以64位工作,我是否需要对其进行任何更改?
我正在使用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.