delphi - 如何使用 Delphi 2009 和 Unicode 字符串正确调用 GetLongPathName?

标签 delphi winapi unicode delphi-2009

我需要将旧的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/

相关文章:

delphi - 如何使用 Delphi 从 Web 服务器下载文件文件夹

Delphi Indy相当于Python的Socket.recv?

python - 如何在 python 中检查 Unicode 字符

c++ - std::wcout 打印 unicode 字符但它们被隐藏

c# - Unicode 文字串

delphi - InheritsFrom 和 is 运算符有什么区别?

excel - Delphi 6 - 从 Delphi 应用程序创建 Excel 图表 - 数据和图表在同一页面上

c++ - 是否可以在 directx dc/buffer 上使用 opengl 进行绘制?

C++ - 基本的 WinAPI 问题

c# - 在 Windows 操作系统上将屏幕转为灰度