我需要将旧的Win98短路径名更改为长路径名。我有一个在 Delphi 4 中运行良好的例程,但是当我升级到 Delphi 2009 和 Unicode 时,它无法与 Unicode 字符串一起运行。
我环顾四周,找不到它的 Unicode 兼容版本。
看来正确的例程是 GetLongPathName from the WinAPI 。但它似乎不在Delphi 2009的SysUtils库中,我一直无法弄清楚如何正确声明它以访问WinAPI例程。
此外,看起来确实很难打电话,因为我已经读过SO问题:Delphi TPath.GetTempPath result is cropped但这并没有帮助我到达一垒。
有人可以解释一下如何声明这个函数并在 Delphi 2009 中正确使用它传递 Unicode 字符串吗?
最佳答案
当然。您不需要单独的单元,并且可以在任何地方声明 GetLongPathName:
function GetLongPathName(ShortPathName: PChar; LongPathName: PChar;
cchBuffer: Integer): Integer; stdcall; external kernel32 name 'GetLongPathNameW';
function ExtractLongPathName(const ShortName: string): string;
begin
SetLength(Result, GetLongPathName(PChar(ShortName), nil, 0));
SetLength(Result, GetLongPathName(PChar(ShortName), PChar(Result), length(Result)));
end;
procedure Test;
var
ShortPath, LongPath: string;
begin
ShortPath:= ExtractShortPathName('C:\Program Files');
ShowMessage(ShortPath);
LongPath:= ExtractLongPathName(ShortPath);
ShowMessage(LongPath);
end;
关于delphi - 如何使用 Delphi 2009 和 Unicode 字符串正确调用 GetLongPathName?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3527221/