【问题标题】:olevariant to an array of object对象数组的 olevariant
【发布时间】:2013-08-23 12:11:56
【问题描述】:

我试图获得一个 olevariant 'array of object' 我有这个 c++ 代码可以完成这项工作,但我未能将它转化为 delphi 代码

VARIANT vComps;
HRESULT hr = swAssembly->GetComponents(VARIANT_TRUE, &vComps);
IDispatch* HUGEP *pDispData;
HRESULT hr = SafeArrayAccessData(vComps.parray, (void**)&pDispData);
long bound = 0;
hr = SafeArrayGetUBound(vComps.parray, 1, &bound);
for (int i = 0; i < count; i++)
{
  IComponent2 *nextComp = NULL;
  hr = pDispData[i]->QueryInterface(IID_IComponent2, (void**)&nextComp);
  //do stuff with Component pointer
}

Stijn Sanders 建议我这样翻译:

var
  vComps:OleVariant;
  i:integer;
  comp:IComponent2;
begin
  swAssembly.GetComponents(true,vComps);
  for i:=VarArrayLowBound(vComps,1) to VarArrayHighBound(vComps,1) do
   begin
     comp:=IUnknown(vComps[i]) as IComponent2;
     //do stuff with component
   end;

但函数是从 tbl 文件导入的。

swAssembly.getComponents(const toplevelonly:boolean)

而'getcomponents'只有一个参数然后我做不到

swAssembly.GetComponents(true,vComps);

我试过了

vComps:=swAssembly.getComponents(true);

使用 vComps 作为 olevariant 类型(因为编译器只允许这种类型) 执行此行时没有错误,但是当我尝试读取 vComps 时

Icomponent2(vComps[i])

我有一个访问错误... 我尝试了 safearray,但我发现了它们并且我遇到了一些困难......

【问题讨论】:

  • -1 你已经问过这个问题了。 Stijn 花了宝贵的时间写了一个答案。然后,您删除了该问题,因此无法奖励 Stijn 的努力。这是非常不礼貌的行为。
  • 非常感谢 Stijn Sanders!但我认为上一篇文章被你“搁置为题外话”阻止了,我创建了这个新的而不是编辑。对不起。这是我关于堆栈溢出的第一个问题......
  • 是的,暂停。这是您编辑它并修复其缺陷的队列。以便它可以重新打开。
  • 我不介意。我什至都不会注意到,除非这正是我的第 8000 个重复点,我想知道为什么我突然又跌到了 7999。

标签: arrays delphi variant


【解决方案1】:

试试这个:

vComps := swAssembly.GetComponents(true);
for i := VarArrayLowBound(vComps, 1) to VarArrayHighBound(vComps, 1) do
begin
  comp := vComps[i] as IComponent2;
  ...
end;

【讨论】:

  • +1 但是如果你解释一下safecall的方法重写会更好
  • 函数 GetComponents(TopLevelOnly: WordBool): OleVariant;安全呼叫;
【解决方案2】:
v := o[I] as IComponent2;

给出编译错误

[DCC Error] Unit1.pas(62): E2015 Operator not applicable to this operand type

同样的事情

v := Icomponent2(o[I]);

只有 for 语句我可以编译,但进入循环时出现错误

for I := VarArrayLowBound(o, 1) to VarArrayHighBound(o, 1) do
begin
end;

class EVariantInvalidArgError with message 'Invalid argument'.

【讨论】:

  • 我在 getComponents 之后插入了一个断点,我在编译器中看到 v 是无符号的并且 length(v) 返回 0
猜你喜欢
  • 2015-12-01
  • 2011-02-15
  • 1970-01-01
  • 2013-11-09
  • 2010-12-18
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多