Jolyon I imagine you have an opinion on doing this your way rather than like this example below – I would be interested in hearing your reasoning.
A similar example – this is the sort of code I have been using....
setlength(temp, 100); //has to be big enough first
setlength(temp, getEnvironmentVariable(PChar('USERNAME'), PChar(temp), length(temp)));
From: Jolyon Smith
Sent: Monday, June 27, 2011 10:07 AM
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] Variable in String
Sorry Bob, I meant to include an example of your code tweaked to use “raw” a char array with the Windows API routine. Here it is (this version displays results rather than storing in a variable, but you get the idea J ) :
dir: array of Char;
s := ‘’;
SetLength(dir, MAX_PATH + 1);
if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, @dir)) then
s := PChar(dir);
ShowMessageFmt(s + ' (%d chars)', [Length(s)]);
// Outcome: s has both the right length *and* is null terminated correctly