سؤال

Im trying to get a olevariant 'array of object' I have this c++ code that do the job but i failed to traduce it to delphi code

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 suggest me this translation:

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;

but functions were imported from a tbl file.

swAssembly.getComponents(const toplevelonly:boolean)

and 'getcomponents' has only one parameter then i can't do

swAssembly.GetComponents(true,vComps);

i tried

vComps:=swAssembly.getComponents(true);

with vComps as olevariant type (because compiler allow only this type) No error when this line is executed but when i try to read vComps

Icomponent2(vComps[i])

i have an access error... i tried safearray's but i discover them and i have some difficulties...

هل كانت مفيدة؟

المحلول

Try this:

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

نصائح أخرى

v := o[I] as IComponent2;

give the compiler error

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

same thing for

v := Icomponent2(o[I]);

with only the for statment i can compile but i have an error when entering the loop

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

class EVariantInvalidArgError with message 'Invalid argument'.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top