delphi - 无法从 PascalScript 调用 Get_ProcAddress

标签 delphi delphi-xe pascalscript

以下是我的代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, uPSComponent;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    FScripter: TPSScript;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.AfterConstruction;
begin
  inherited;
  FScripter := TPSScript.Create(nil);
  (FScripter.Plugins.Add as TPSPluginItem).Plugin := TPSImport_Test.Create(nil);
end;

procedure TForm1.BeforeDestruction;
begin
  inherited;
  while FScripter.Plugins.Count > 0 do
    (FScripter.Plugins.Items[0] as TPSPluginItem).Plugin.Free;
  FScripter.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
  Memo1.Clear;

  FScripter.Script.Text :=
    'var H: Cardinal; '                                     + #13#10 +
    '    P: procedure(const S: string); '                   + #13#10 +
    'begin '                                                + #13#10 +
    '  H := LoadPackage(''Package1.bpl''); '                + #13#10 +
    '  try '                                                + #13#10 +
    '    if H  0 then begin '                             + #13#10 +
    '      @P := Get_ProcAddress(H, ''TestProc''); '        + #13#10 +
    '      P(''12345''); '                                  + #13#10 +
    '    end; '                                             + #13#10 +
    '  finally '                                            + #13#10 +
    '    UnloadPackage(H); '                                + #13#10 +
    '  end; '                                               + #13#10 +
    'end.';

  if FScripter.Compile then begin
    if not FScripter.Execute then
      Memo1.Lines.Text := string(FScripter.ExecErrorToString);
  end else
    for i := 0 to FScripter.CompilerMessageCount - 1 do
      Memo1.Lines.Add(string(FScripter.CompilerMessages[i].MessageToString));
end;

end.


unit Unit2;

interface

uses uPSComponent;

type
  TPSImport_Test = class(TPSPlugin)
  public
    procedure CompileImport1(CompExec: TPSScript); override;
    procedure ExecImport1(CompExec: TPSScript; const ri: TPSRuntimeClassImporter);
        override;
  end;


implementation

uses Dialogs, SysUtils, Windows;

function Get_ProcAddress(const aHandle: Cardinal; const aProcName: string):
    Pointer;
begin
  Result := GetProcAddress(aHandle, PChar(aProcName));
end;

procedure TPSImport_Test.CompileImport1(CompExec: TPSScript);
begin
  CompExec.Comp.AddDelphiFunction('procedure ShowMessage(const Msg: string)');
  CompExec.Comp.AddDelphiFunction('function LoadPackage(const Name: string): cardinal');
  CompExec.Comp.AddDelphiFunction('procedure UnloadPackage(const Module: cardinal)');
  CompExec.Comp.AddDelphiFunction('function Get_ProcAddress(const aHandle: cardinal; const aProcName: string): ___Pointer');
end;

procedure TPSImport_Test.ExecImport1(CompExec: TPSScript; const ri:
    TPSRuntimeClassImporter);
begin
  CompExec.Exec.RegisterDelphiFunction(@ShowMessage,      'ShowMessage',      cdRegister);
  CompExec.Exec.RegisterDelphiFunction(@LoadPackage,      'LoadPackage',      cdRegister);
  CompExec.Exec.RegisterDelphiFunction(@UnloadPackage,    'UnloadPackage',    cdRegister);
  CompExec.Exec.RegisterDelphiFunction(@Get_ProcAddress,  'Get_ProcAddress',  cdRegister);
end;

end.


unit Unit3;

interface

implementation

uses Dialogs;

procedure TestProc(const S: string);
begin
  MessageDlg(S, mtInformation, [mbOK], 0);
end;

exports TestProc;

end.

Package1.bpl是包含Unit3.pas的运行时包。

如何从 PascalScript 调用 Get_ProcAddress?

在编译脚本期间收到以下错误消息,

[Error] (7:7): Identifier expected

最佳答案

您在第七行的第七个字符处看到的错误与 Get_ProcAddress 无关。您有一个 @ 字符,解释器告诉您它需要一个标识符。仔细检查 Pascal 脚本中分配过程指针的语法规则(请记住它们可能与 Delphi 的不同)。

关于delphi - 无法从 PascalScript 调用 Get_ProcAddress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105220/

相关文章:

json - Delphi FDQuery 转 Json

performance - 当我关闭 {$IMPORTEDDATA} 时,性能是否有真正的提升?

Delphi - 泛型类型检查是否已创建

delphi - Delphi中的UDP服务器和客户端

德尔福XE : TDBMemo Text Disappears

progress-bar - Inno Setup安装进度条未达到100%

inno-setup - 在 Inno Setup Pascal 脚本中使用 MediaInfo 库获取图像文件信息

inno-setup - 如何使 Inno Setup/DIR 命令行开关与自定义路径页面配合使用

delphi - 关于工作不正常的 Intraweb 问题(单选按钮)?

delphi - 处理在 TListView 后代中检查的项目