在Delphi 2010下面使用时会出现内存溢出,原始函数如下:
function LongNameToShortName(const FileName: string): string;
var
Buf: PChar;
BufSize: Integer;
begin
BufSize := GetShortPathName(PChar(FileName), nil, 0) + 1;
GetMem(Buf, BufSize);
try
GetShortPathName(PChar(FileName), Buf, BufSize);
Result := Buf;
finally
FreeMem(Buf);
end;
end;
似乎这里用PChar有点不妥,我将其修改如下,似乎问题解决了,希望管理员能检查下,谢谢了!
function LongNameToShortName(const FileName: string): string;
var
ShortName:string;
BufSize: Integer;
begin
BufSize := GetShortPathName(PChar(FileName), nil, 0);
SetLength(ShortName,BufSize);
GetShortPathName(PChar(FileName),PChar(ShortName),BufSize);
Result := ShortName;
end;