Why does this code not work in D2010 but not D7?

Why does this code get an access error on the result line: = Buffer in D2010, but not D7?

Something, I think, with UniCode, but the compiler does not generate any warnings.

Any suggestions for an elegant workaround?

Edit: Ouch: calling GetTempPath causes the stack to split, as evidenced by the fact that after the GetTempPath line the extension remains empty, but not before ... Yikes.

function GetTempPathAndFileName( const Extension: string): string; var Buffer: array[0..MAX_PATH] of Char; begin repeat GetTempPath(SizeOf(Buffer) - 1, Buffer); GetTempFileName(Buffer, '~', 0, Buffer); Result := Buffer; // <--- crashes on this line, Result := ChangeFileExt(Result, Extension); until not FileExists(Result); end; { GetTempPathAndFileName } 
+6
delphi
source share
2 answers

GetTempPath expects the number of characters in the buffer for the first argument, not the size in bytes. Change SizeOf to Length and it will work.

+14
source share

To make it work like in D7, replace "string" with "AnsiString" and "Char" with "AnsiChar". Also, call GetTempPathA and GetTempFileNameA, not GetTempPath and GetTempFileName.

But Mason's approach is probably better, as it will support Unicode file names.

+5
source share

All Articles