无法从 PascalScript 调用 Get_ProcAddress

发布于 2024-11-09 14:51:56 字数 3965 浏览 0 评论 0原文

以下是我的代码:

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

The following is my code:

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 is runtime package contains Unit3.pas.

How to invoke the Get_ProcAddress from the PascalScript?

I get the following error message during compile the script,

[Error] (7:7): Identifier expected

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2024-11-16 14:51:56

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

The error you're seeing at the seventh character of the seventh line has nothing to do with Get_ProcAddress. You have an @ character, and the interpreter tells you it expected an identifier. Double-check the syntax rules for assigning procedure pointers in Pascal Script (keeping in mind that they might not be the same as Delphi's).

☆獨立☆ 2024-11-16 14:51:56

您可能需要在 interface 部分中声明 Get_ProcAddress。目前它仅处于实现中。

You probably need to declare Get_ProcAddress in your interface section. Currently it's only in the implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文