CnPack Forum


 
Subject: CnCommon单元LongNameToShortName函数有问题?
yinsim
新警察
Rank: 1



UID 50783
Digest Posts 0
Credits 48
Posts 16
点点分 48
Reading Access 10
Registered 2010-8-17
Status Offline
Post at 2010-12-22 18:53  Profile | Blog | P.M. 
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;

Top
CCED136
新警察
Rank: 1



UID 42209
Digest Posts 0
Credits 29
Posts 9
点点分 29
Reading Access 10
Registered 2009-4-17
Status Offline
Post at 2010-12-23 10:30  Profile | Blog | P.M. 
不会吧, 实际上,原始的代码中, 首先获取了长度,然后 GetMem+1, 最后 FreeMem , 这里没问题哦
Top
yinsim
新警察
Rank: 1



UID 50783
Digest Posts 0
Credits 48
Posts 16
点点分 48
Reading Access 10
Registered 2010-8-17
Status Offline
Post at 2010-12-23 15:16  Profile | Blog | P.M. 
在我这里总出错,显示堆栈溢出.
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6838
Posts 3591
点点分 6838
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2010-12-23 17:18  Profile | Blog | P.M. 
D2010下,楼主试一试把GetMem(Buf, BufSize);改为GetMem(Buf, BufSize * SizeOf(Char));看看?

我们也会修改此问题。
Top
yinsim
新警察
Rank: 1



UID 50783
Digest Posts 0
Credits 48
Posts 16
点点分 48
Reading Access 10
Registered 2010-8-17
Status Offline
Post at 2010-12-23 19:04  Profile | Blog | P.M. 
问题解决,谢谢!
又是一个双字节问题,呵呵~
Top
 




All times are GMT++8, the time now is 2024-11-22 05:44

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.010091 second(s), 9 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP