如何在Delphi 2010中使用IFont和IStrings?

宗努兹


我想在Delphi 2010中编写一个DCOM客户端/服务器,该客户端/服务器使用IStrings作为测试服务器中的属性传递Memo1.Lines(作为TStrings)。该服务器具有TMemo组件,我想通过服务器的IMemoIntf设置或获取其Memo1.Lines属性。

开箱即用的Delphi 2010不接受1-In RIDL编辑器中的IStrings。因此,我首先注册了stdvcl40.dll,并将其添加到编辑器的“ uses”部分,以便能够添加IStrings类型的属性。

2然后,我实现了两个函数Set_Text和Get_Text来设置和获取服务器的Memo1.Lines,如下所示:

procedure TMemoIntf.Set_Text(const Value: IStrings);
begin
  SetOleStrings(Form1.GetText, Value);
end;


function TMemoIntf.Get_Text: IStrings;
begin
  GetOleStrings(Form1.GetText, Result);
end;

IMemoIntf是TMemoIntf实现的接口。它是自动定义的,如下所示:

// *********************************************************************//
// Interface: IMemoIntf
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {2B6BD766-5FB6-413F-B8E2-4AB05D87E669}
// *********************************************************************//
  IMemoIntf = interface(IDispatch)
    ['{2B6BD766-5FB6-413F-B8E2-4AB05D87E669}']
    function Get_Text: IStrings; safecall;
    procedure Set_Text(const Value: IStrings); safecall;
    property Text: IStrings read Get_Text write Set_Text;
  end;

和TMemoIntf如下:

TMemoIntf = class(TAutoObject, IMemoIntf)
protected
  function Get_Text: IStrings; safecall;
  procedure Set_Text(const Value: IStrings); safecall;
end;

在客户端中调用fMemo.Set_Text时,一切都很好并且可以正常工作,并且客户端将服务器Memo1的内容设置为自己的内容,但是当我调用fMemo.Get_Text以获取服务器Memo1的内容时,出现以下错误信息。

地址在模块'combase.dll'中的访问冲突...读取地址...

客户端具有显示服务器的私有字段fMemo和用于显示Set / Get_Text调用结果的Memo1。

TForm2 = class(TForm)
...
  btnSetText: TButton;
...
  btnGetText: TButton;
  Memo1: TMemo;
...
  procedure btnSetTextClick(Sender: TObject);
...
  procedure btnGetTextClick(Sender: TObject);
private
  fMemo : IMemoIntf;
end;

// it gives me the error
procedure TForm2.btnGetTextClick(Sender: TObject);
var
  Strings : IStrings;
begin
  Strings := fMemo.Get_Text;
  SetOleStrings(Memo1.Lines, Strings);
end;

// it works fine
procedure TForm2.btnSetTextClick(Sender: TObject);
var
  Strings : IStrings;
begin
  GetOleStrings(Memo1.Lines, Strings);
  fMemo.Set_Text(Strings);
end;

相同的想法也适用于IFont,但是当我实现与TFont和TColor相同的东西来工作时,OLE_COLOR可以正常工作(我知道OLE_COLOR作为自动化编组类型直接受支持,并且不同于两者)。

我做错了还是在Delphi 2010中出现了?
如何使用IFont和IStrings缓解Delphi 2010中的问题?

宗努兹

好的,我发现了什么地方出了问题,就是我。

“ .ridl”文件的定义非常重要。对于“ getter函数”,“ out”参数应为“ IStrings **”,对于“ putter函数”,参数应为“ IStrings *”。编译器会自动更新“ TLB”文件并向接口定义添加属性,并且使用“ IMemoIntf”的以前的实现,双向都可以正常运行。

希望对您有所帮助。我不知道如何追加,如果有兴趣的人告诉我如何追加整个项目,看看我做了什么。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章