olevariant到一个对象数组

时间:2013-08-23 12:11:57

标签: arrays delphi variant

我试图获得一个olevariant'对象数组'我有这个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,但我发现它们,我有一些困难...

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:-1)

v := o[I] as IComponent2;

给出编译器错误

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

同样的事情

v := Icomponent2(o[I]);

只有for statment我可以编译,但进入循环时出错

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

class EVariantInvalidArgError with message 'Invalid argument'.