-2

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...

Tokazio
  • 516
  • 2
  • 18
  • 7
    -1 You already asked this question. Stijn spent his valuable time writing an answer. You then deleted the question thus making it impossible to reward Stijn for his efforts. That's very bad manners. – David Heffernan Aug 23 '13 at 13:14
  • Big thanks to Stijn Sanders! But i think the previous post was blocked by you 'put on hold as off-topic' and i created this new instead of editing. Sorry. That's my first question on stack overflow... – Tokazio Aug 23 '13 at 16:44
  • Yes, put on hold. Which is the queue for you to edit it and fix its deficiencies. So that it can then be re-opened. – David Heffernan Aug 23 '13 at 17:03
  • I don't mind. I wouldn't even have noticed except it was exactly my 8000th rep point and I wondered why I suddenly dropped to 7999 again. – Stijn Sanders Aug 24 '13 at 20:59

2 Answers2

2

Try this:

vComps := swAssembly.GetComponents(true);
for i := VarArrayLowBound(vComps, 1) to VarArrayHighBound(vComps, 1) do
begin
  comp := vComps[i] as IComponent2;
  ...
end;
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
-2
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'.
Tokazio
  • 516
  • 2
  • 18
  • i've inserted a breakpoint after getComponents and i see in the compiler that v is unasigned and length(v) return 0 – Tokazio Aug 26 '13 at 09:36