Subject: CnCommon单元LongNameToShortName函数有问题? [Print This Page]
Author:
yinsim Time: 2010-12-22 18:53 Subject: CnCommon单元LongNameToShortName函数有问题?
在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;
Author:
CCED136 Time: 2010-12-23 10:30
不会吧, 实际上,原始的代码中, 首先获取了长度,然后 GetMem+1, 最后 FreeMem , 这里没问题哦
Author:
yinsim Time: 2010-12-23 15:16
在我这里总出错,显示堆栈溢出.
Author:
Passion Time: 2010-12-23 17:18
D2010下,楼主试一试把GetMem(Buf, BufSize);改为GetMem(Buf, BufSize * SizeOf(Char));看看?
我们也会修改此问题。
Author:
yinsim Time: 2010-12-23 19:04
问题解决,谢谢!
又是一个双字节问题,呵呵~
Welcome to CnPack Forum (http://bbs.cnpack.org/) |
Powered by Discuz! 5.0.0 |