如何将interfaced对象传递给Pascal Script函数调用?

TLa*_*ama 7 delphi inno-setup delphi-2009 remobjects pascalscript

德尔福部分:

我有一个关于事件的类,从那个事件我需要调用一个将接口对象传递给它的过程.它在Delphi中工作正常,但我在Pascal脚本中声明它有问题.

在后台 - IGPGraphics接口是一部分Delphi GDI+ library而没有方法定义如下:

type
  IGdiplusBase = interface
  ['{24A5D3F5-4A9B-42A2-9F60-20825E2740F5}']
  IGPGraphics = interface(IGdiPlusBase)
  ['{57F85BA4-CB01-4466-8441-948D03588F54}']
Run Code Online (Sandbox Code Playgroud)

以下是我在Pascal脚本中需要做的简化Delphi伪代码:

type
  TRenderEvent = procedure(Sender: TObject; const GPGraphics: IGPGraphics) of object;
  TRenderClass = class(TGraphicControl)
  private
    FOnRender: TRenderEvent;
  public
    property OnRender: TRenderEvent read FOnRender write FOnRender;
  end;

// when the TRenderClass object instance fires its OnRender event I want to call 
// the RenderObject procedure passing the IGPGraphics interfaced object to it; I
// hope I'm doing it right, I'm just a newbie to this stuff - but it works so far
// in Delphi (since I didn't get it to work in Pascal Script)

procedure TForm1.RenderClass1Render(Sender: TObject; const GPGraphics: IGPGraphics);
begin
  RenderObject(GPGraphics, 10, 10);
end;

// what I need in Pascal Script is between these two lines; just pass the interface
// object from the event fired by component to the procedure called from inside it

procedure RenderObject(const GPGraphics: IGPGraphics; X, Y);
begin
  // and here to work with the interfaced object somehow
end;
Run Code Online (Sandbox Code Playgroud)

Pascal脚本编译部分:

我的目标是让Pascal脚本中的事件可用,并且需要像上面那样将该接口对象传递给该过程,所以我第一次尝试为编译时声明这个(但我甚至不确定是否这是正确的方法):

// the interface
PS.AddInterface(Cl.FindInterface('IUnknown'), StringToGuid('{57F85BA4-CB01-4466-8441-948D03588F54}'), 'IGPGraphics');
// the type for the event
PS.AddTypeS('TRenderEvent', 'procedure(Sender: TObject; const GPGraphics: IGPGraphics)');
// and the class with the event itself
with PS.AddClassN(PS.FindClass('TGraphicControl'), 'TRenderClass') do
begin
  RegisterProperty('OnRender', 'TRenderEvent', iptrw);
end;
Run Code Online (Sandbox Code Playgroud)

Pascal脚本运行时部分:

我绝对迷失的地方是运行时部分.我无法弄清楚如何从调用堆栈中获取interfaced对象并将其传递给我的RenderObject过程:

function RenderClassProc(Caller: TPSExec; Proc: TPSExternalProcRec; Global, 
  Stack: TPSStack): Boolean;
var
  PStart: Cardinal;
begin
  PStart := Stack.Count-1;
  Result := True;
  if Proc.Name = 'RENDEROBJECT' then
  begin
    // how do I get the interfaced object from Stack (or whatever else) and pass 
    // it to the RenderObject proc here ? I can't find anything related about it
    // except functions that has no parameter index
    RenderObject(Stack.Get ?, Stack.GetInt(PStart-2), Stack.GetInt(PStart-3));
  end;
end;
Run Code Online (Sandbox Code Playgroud)

问题是:

任何人都可以建议我如何正确定义此案例的编译和运行时部分或通过某种方式传递接口对象来纠正我?

PS为InnoSetup标签抱歉,但也许有人从那里尝试自订InnoSetup这种方式.

非常感谢!

jth*_*man 1

如果我明白你在问什么,你想将接口作为参数传递给方法。不幸的是,我没有确切的答案,但我确实知道如何将接口分配给 PascalScript 的全局变量。这是我在卡斯塔利亚的做法:

在 PSScript OnCompile 事件中,使用 PS.Comp.AddInterface 添加接口,然后添加每个必要的方法。之后,添加一个接口类型的变量。例如,它看起来像这样:

with PS.Comp.AddInterface(ARunner.Comp.FindInterface('IUnknown'),
  StringToGUID('{0346F7DF-CA7B-4B15-AEC9-2BDD680EE7AD}'),
  'ICastaliaMacroClipboardAccess') do
begin
  RegisterMethod('function GetText: string', cdRegister);
  RegisterMethod('procedure SetText(AText: string)', cdRegister);
end;
PS.AddRegisteredVariable('Clipboard', 'ICastaliaMacroClipboardAccess');
Run Code Online (Sandbox Code Playgroud)

然后,在 OnExectute 事件中,将先前创建的变量绑定到实例:

P := PS.GetVariable('Clipboard'); //P is type PIFVariant
SetVariantToInterface(P, Implementing object as ICastaliaMacroClipboardAccess);    
Run Code Online (Sandbox Code Playgroud)

完成此操作后,脚本可以通过变量访问接口对象,因此在本例中,脚本可以包含对 Clipboard.GetText 的调用,并且它可以按您的预期工作。

这是经过测试并且有效的。

现在,我推测您也许可以使用 TPSScript.ExecuteFunction,从上面传入 PIFVariant,以更接近您想要的结果。然而,这不是我测试过的。

祝你好运!