I using Delphi XE, i've the following setup:
Both Mydll.dll and Package1.bpl (runtime package) contains Unit3.pas
unit Unit3;
interface
implementation
uses Dialogs;
procedure TestProc(const S: string); stdcall;
begin
MessageDlg(S, mtInformation, [mbOK], 0);
end;
exports TestProc;
end.
Case 1:
procedure TestProc(const S: string); stdcall; external 'mydll.dll';
procedure TForm1.Button3Click(Sender: TObject);
begin
TestProc('Button3');
end;
Case 2:
procedure TestProc(const S: string); stdcall; external 'Package1.bpl';
procedure TForm1.Button3Click(Sender: TObject);
begin
TestProc('Button3');
end;
Case 3:
procedure TForm1.Button3Click(Sender: TObject);
var H: THandle;
P: procedure(const S: string); stdcall;
begin
H := LoadPackage('Package1.bpl');
try
@P := GetProcAddress(H, PChar('TestProc'));
if Assigned(P) then
P('Button3');
finally
UnloadPackage(H);
end;
end;
Case 1 and Case 3 passed but Case 2 will raise Access Violation.
My question as below,
1. Case 2 is not supported?
2. Except the Case 3, is there anyway to invoke the TestProc from Pa开发者_如何学Gockage1.bpl similar as Case1?Yes the case 2 is supported but you must call the LoadPackage
function too to load the package in memory.
try this code
procedure TestProc(const S: string); stdcall; external 'Package1.bpl';
var
hPackage : Cardinal;
procedure TForm1.Button1Click(Sender: TObject);
begin
TestProc('Button3');
end;
initialization
hPackage := LoadPackage('Package1.bpl');
finalization
if hPackage<>0 then
UnloadPackage(hPackage);
For case 2, you could also just simply build your project with runtime packages and include Package1 in the list of runtime packages for your project (in Project Options\Packages\Runtime packages). Then you can remove the import (external
declaration) and just use the unit which contains the function.
精彩评论