Get the length of the record field of an array of types

I am writing a wrapper to communicate with an external binary API. The API uses PDUs (Packed Binary Entries) for communication. Strings are AnsiChar arrays and end with zero:

type TSomePDU = packed record //... StringField: array[0..XYZ] of AnsiChar; //... end; PSomePDU = ^TSomePDU; 

I want to write a FillPDUString procedure that will take a String and fill the char array, but I want to avoid MaxLength tracking wherever this procedure is used, so I need to somehow get the size of the declared array taking into account the pointer to the field:

 function GetMaxSize(const Field: array of AnsiChar): Integer; begin // ??? end; //... GetMaxSize(ARecord.StringField); 

Is it possible?

+4
source share
2 answers

If I understand you correctly, you can use the Delphi Length function

Here's how to get the length:

 function GetMaxSize(const Value: PSomePDU): Integer; begin Result := Length(Value.StringField); end; 
+6
source

To get the number of elements contained in an array, use Length .

 ElementCount := Length(ARecord.StringField); 

Use low and high to get the borders of any Delphi array.

 MinIndex := low(ARecord.StringField); MaxIndex := high(ARecord.StringField); 

Using the latter approach, with low and high , you can not assume that the array is based on 0.

+6
source

Source: https://habr.com/ru/post/1403628/


All Articles