I really don’t understand why this function doesn’t work:
function GetNomRepertoireTemporaire:WideString;
var
PathLocal : array[0..MAX_PATH+1] of WideChar;
begin
Result := '';
if GetTempPath(SizeOf(PathLocal)-1, PathLocal)>0 then
begin
Result := PathLocal;
end;
end;
When I call it like:
var
t : wideString;
initialization
t := GetNomRepertoireTemporaire;
I wait something like 10 seconds then I get an AV at 0x000000 address 0000000
Anybody could explain me what I’m doing wrong?
You should use Length instead of SizeOf in your code:
The above code assumes that you are using Unicode Delphi version. As David mentioned in the comment you can change your function to make it compatible with both Unicode and Non-Unicode Delphi:
Explanation:
GetTempPathfunction fills with zeroes the whole buffer it receives. The OP code sets invalid buffer size (twice the actual size), so the function zeroes the memory behindPathLocalvariable, which results in AV.