Mega Code Archive

 
Categories / Delphi / Examples
 

A tricky way to use setlength[]

Several Windows API functions that retrieve a string get a PChar as an argument and return the number of bytes retrieved. This return value can be used for SetLength(). An example for that is the function GetWindowsDirectory() that is used in the following piece of code: {$IFDEF WIN32} function GetWindowsDir: string; begin SetLength(Result, MAX_PATH); SetLength(Result, GetWindowsDirectory(PChar(Result), Length(Result))); end; {$ENDIF}