Thursday, October 1, 2009

Delphi 2010 Migration Woes No. 1

The migration to Delphi 2010 has been a smooth experience, so far. Most, if not all issues are caused by string conversions from AnsiString to Unicode. Here's one:

Windows call

Var
Buffer: array[0..1023] of char;
begin
Result := windows.GetTempPath (SizeOf(Buffer), Buffer));
end;

Since unicode chars are two bytes instead of one, the result of SizeOf is twice as long as expected.

Solve by using length, which results in the number of characters, not bytes.

Result := windows.GetTempPath(length(Buffer), Buffer));

No comments: