Char
is AnsiChar
(SizeOf(Char)=1
) in Delphi 2007 and earlier, but is WideChar
(SizeOf(Char)=2
) in Delphi 2009 and later.
GetTempPath()
expects the first parameter to specify the number of characters that your buffer can hold, but you are specifying the number of bytes instead.
In Delphi 2007 and earlier, SizeOf(tempFolderChars)
and Length(tempFolderChars)
will be the same value, but in Delphi 2009 and later they will not be the same. In that latter case, you are telling GetTempPath()
that you can accept twice as many characters as you really can.
You need to change SizeOf(tempFolderChars)
to Length(tempFolderChars)
. You also need to pay attention to the return value of GetTempPath()
, as it tells you how many characters were actually written into the buffer.
Try this instead:
procedure Log(msg : String);
var
tempFolderChars : array [0..MAX_PATH] of Char;
tempFolder : string;
len: DWORD;
...
begin
len := GetTempPath(Length(tempFolderChars), tempFolderChars);
if len = 0 then Exit;
SetString(tempFolder, tempFolderChars, len);
tempFolder := IncludeTrailingPathDelimiter(tempFolder);
...
end;