Niezgodność typów danych

0

Witam,

Następująco zdefiniowaną funkcję:

function ReadList(var PID: LongInt; res: array of ListEntry ; var adrServerList_CollectionStart: AddressPath): LongInt;

Gdzie :

type
  ListEntry = record
    id: LongInt;
    entry_address: LongInt;
    name_address: LongInt;
    name: string;
    server: string;
  end;

W środku funkcji chciałbym ustawić długość tablicy res poprzez :

var
resSize : LongInt;

  resSize := QMemory_Read4Bytes(pid, Pointer(adrCharList + $C));
  bytesPerElement := resSize * 4;
  SetLength(Tmp, BytesPerElement - 1);
  QMemory_ReadNBytes(pid, Pointer(adrCharListStart), tmp);

  tamStruct := 60;
  SetLength(res, resSize - 1);         /////////////// tutaj wyrzuca błąd niezgodności typów

Mógłby mi ktoś podpowiedzieć, w czym tkwi problem? czemu się kłóci z niezgodnością?

Pozdrawiam

2
type arrayOfListEntry= array of ListEntry;

function ReadList(var PID: LongInt; res: arrayOfListEntry ; var adrServerList_CollectionStart: AddressPath): LongInt;
1
function ReadList(var PID: LongInt; res: array of TArrayOfListEntry; var adrServerList_CollectionStart: AddressPath): LongInt;

Tak powinno wyglądać definicja parametrów funkcji

function ReadList(var PID: LongInt; res:  TArrayOfListEntry; var adrServerList_CollectionStart: AddressPath): LongInt;
0

Przy tej funkcji problem się rozwiązał - dziękuję.

Powstał natomiast problem przy próbie odczytania danych z tej tablicy...

procedure TfrmMain.cmdReadAccPwClick(Sender: TObject);
var
  PID: LongInt;
  totalItems : LongInt;
  res : TArrayOfListEntry;
  lastEntry : LongInt;
  i : Integer ;
begin
  PID := StrToInt(cmbPIDs.Text);
  if PID = -1 then
  begin
    MessageDlg('Please open a client first!', mtWarning, [mbOk], 0);
    Exit;
  end;

  //  LIST
  lastEntry := ReadList(pid, res, adrServerList_CollectionStart);
  lstChars.Clear;

  If lastEntry = -1 Then
    ShowMessage('This client does not contain char list info at this moment');

  For i := 0 To lastEntry do
     lstChars.Items.Add(res.name[i] + ' / ' + res.server[i]);  // [Error] FrmMainUnit.pas(147): Record, object or class type required
end;

edit :
zmieniłem na :

lstChars.Items.Add(res[i].name + ' / ' + res[i].server);  

i się kompiluje, natomiast funkcja ciągle nie działa. Będę musiał szukać problemu dalej.
Dziękuję wszystkim za pomoc.

2

a nie res[i].name i res[i].server ?

0

Tylko możecie mi powiedzieć, czemu przy wywołaniu tej funkcji wyrzuca błąd? Wcześniej gdy się nie kompilowała, przy tej 1 linii (za komentowałem ją) ale próbowałem wywołać to prawidłowo zwracała ilość listy, teraz natomiast niby wszystko ładnie się kompiluje, ale program się wyrzuca przy próbie wywołania jej.

procedure TfrmMain.cmdReadClick(Sender: TObject);
var
  PID: LongInt;
  totalItems : LongInt;
  res : TArrayOfListEntry;
  lastEntry : LongInt;
  i : Integer ;
begin
  PID := StrToInt(cmbPIDs.Text);
  if PID = -1 then
  begin
    MessageDlg('Please open a client first!', mtWarning, [mbOk], 0);
    Exit;
  end;
 
  //  LIST
  lastEntry := ReadList(pid, res, adrServerList_CollectionStart); // przy próbie wywołania tej funkcji wyrzuca błąd to ta funkcja którą wcześniej pomagaliście mi poprawić
  lstChars.Clear;
 
  If lastEntry = -1 Then
    ShowMessage('This client does not contain char list info at this moment');
 
  For i := 0 To lastEntry do
     lstChars.Items.Add(res[i].name + ' / ' + res[i].server);  // [Error] FrmMainUnit.pas(147): Record, object or class type required
end;

Treść błędu w załączniku

0

Użyj debugeraa w ciemno to chyba powinno być:

For i := 0 To lastEntry -1 do
1

No to może jednak jak sugerował @Patryk27 tablica nie ma mieć rozmiaru nadanego przez:

SetLength(res, resSize - 1);

tylko:

SetLength(res, resSize);

co zresztą jest oczywiste widząc pętlę.

0

Rzeczywiście. Zmieniłem kod - wygląda następująco. (Tam gdzie były poprawki dałem komentarz obok)

function ReadList(var PID: LongInt; res: TArrayOfCharListEntry; var adrServerList_CollectionStart: AddressPath): LongInt;
var
  i : integer;
  tmpElement : ListEntry;
  adrCOLLECTION_START : LongInt;
  adrCharList : LongInt;
  adrType : LongINt;
  adrCharListStart : LongInt;
  resSize : LongInt;
  bytesPerElement : LongInt;
  lastI : LongInt;
  tamStruct : LongInt;
  tmpAdr : LongInt;
  tmp,resStruct : Array of Byte;
begin
  adrCOLLECTION_START := ReadCurrentAddress(pid, adrServerList_CollectionStart, -1, False);
  adrCharList := QMemory_Read4Bytes(pid, Pointer(adrCOLLECTION_START + 8));
 
  adrType := QMemory_Read4Bytes(pid, Pointer(adrCharList));
  If adrType = -1 Then
  begin
    Result := -1;
    Exit;
  End;
 
  if (adrType > 1000) Then
    adrCharList := QMemory_Read4Bytes(pid, Pointer(adrType));
 
  resSize := QMemory_Read4Bytes(pid, Pointer(adrCharList + $C));
  adrCharListStart := adrCharList + $10;
 
  If (resSize = 0) Then
  begin
    Result := -1;
    Exit;
  end;
 
  bytesPerElement := resSize * 4;
  SetLength(Tmp, BytesPerElement);   //  było SetLength(Tmp, BytesPerElement - 1); 
  QMemory_ReadNBytes(pid, Pointer(adrCharListStart), tmp);
 
  tamStruct := 60;
  SetLength(res, resSize);    // było SetLength(res, resSize - 1);
 
  lastI := resSize - 1;
 
  For i := 0 To resSize - 1 do   // było  For i := 0 To resSize do 
  begin
    tmpElement.Id := i;
    tmpElement.entry_address := BitConverter_ToInt32(tmp, 4 * i);
    SetLength(resStruct, tamStruct - 1);
    QMemory_ReadNBytes(pid, Pointer(tmpElement.entry_address), resStruct);
    tmpAdr := QMemory_Read4Bytes(pid, Pointer(tmpElement.entry_address + $10));
    tmpElement.name_address := tmpAdr;
    tmpElement.name := QMemory_ReadString(pid, Pointer(tmpAdr));
    tmpAdr := QMemory_Read4Bytes(pid, Pointer(tmpElement.entry_address + $14));
    tmpElement.server := QMemory_ReadString(pid, Pointer(tmpAdr));
    res[i] := tmpElement;  
   end; 
    Result := resSize - 1;
end;

Gdy wywołuje poprzez showmessage -

tmpElement.server := QMemory_ReadString(pid, Pointer(tmpAdr));

oraz

tmpElement.name := QMemory_ReadString(pid, Pointer(tmpAdr));

Wszystkie dane pokolei ładnie się wyświetlają. Problem natomiast jest przy próbie wyświetlenia tych danych ...

For i := 0 To lastEntry do
     lstChars.Items.Add(res[i].name + ' / ' + res[i].server);

// wyrzuca przy próbie dodania tych danych z tablicy res

0

jaki problem ?

0

A tu nie ma być

For i := 0 To lastEntry -1 do

jak pisałem kila postów wcześniej? I w ogóle co za błąd?

1

Aaaaaa to już wiem var

function ReadList(var PID: LongInt; var res: TArrayOfCharListEntry; var adrServerList_CollectionStart: AddressPath): LongInt;
0

Działa!!
Dziękuję bardzo wszystkim za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1