Adding Stringlist values to a Psafearray

Asked

Viewed 137 times

1

I need to take the values that are stored in a Tstringlist and add these values in Psafearray, which will later be sent as parameters to a dll.

I have tried in many ways, but all to no avail.

Could someone help me.

1 answer

2

After several consultations I managed to do what I needed.

Below how to fill it out, as well as the reading:

procedure TForm1.TratarSafeArray(Lista: TStringList);
var
  VSafeArry : psafearray ;
  saBound :  TSafeArrayBound;
  Index : array [0..0] of LongInt ;// 1 dimension
  Valor : wideString;

  LBound, UBound, I: LongInt;
  W: WideString;


begin

  Memo2.Clear;

  //Gravando dados PSafeArray
  saBound.lLbound := 0 ;
  saBound.cElements := Lista.Count;

  VSafeArry := SafeArrayCreate (VT_BSTR , 1 , saBound) ;
  if VSafeArry = nil then begin
    Exit;
  end;

  for i := 0 to Lista.Count -1 do begin
    Index[0] := i;
    Valor := Lista[i];
    SafeArrayPutElement(VSafeArry, index, Pchar(Valor)^);
  end;

  //Lendo dados do PSafeArray
  SafeArrayGetLBound(VSafeArry, 1, LBound);
  SafeArrayGetUBound(VSafeArry, 1, UBound);

  for I := LBound to UBound do begin
    SafeArrayGetElement(VSafeArry, I, W);
    memo2.Lines.add(UpperCase(W));
  end;

  SafeArrayDestroy(VSafeArry);

end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.