-2

我试图获得一个 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,但我发现了它们并且我遇到了一些困难...

4

2 回答 2

2

尝试这个:

vComps := swAssembly.GetComponents(true);
for i := VarArrayLowBound(vComps, 1) to VarArrayHighBound(vComps, 1) do
begin
  comp := vComps[i] as IComponent2;
  ...
end;
于 2013-08-23T12:21:50.807 回答
-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'.
于 2013-08-26T06:57:36.540 回答