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

发布于 2024-09-15 08:51:56 字数 588 浏览 9 评论 0原文

我需要将旧的Win98短路径名更改为长路径名。我有一个在 Delphi 4 中运行良好的例程,但是当我升级到 Delphi 2009 和 Unicode 时,它​​无法与 Unicode 字符串一起运行。

我环顾四周,找不到它的 Unicode 兼容版本。

正确的例程似乎是 GetLongPathName from the WinAPI。但它似乎不在Delphi 2009的SysUtils库中,我一直无法弄清楚如何正确声明它以访问WinAPI例程。

另外,看起来确实很难打电话,因为我已经阅读了SO问题: Delphi TPath.GetTempPath 结果被裁剪,但这并没有帮助我到达一垒。

有人可以解释一下如何声明这个函数并在 Delphi 2009 中正确使用它传递 Unicode 字符串吗?

I need to change the old Win98 short path names to long path names. I had a routine that worked fine with Delphi 4, but when I upgraded to Delphi 2009 and Unicode, it didn't work with Unicode strings.

I looked around and couldn't find a Unicode-compatible version of it.

It appears that the correct routine to use is GetLongPathName from the WinAPI. But it doesn't seem to be in the SysUtils library of Delphi 2009 and I have not been able to figure out how to declare it properly to access the WinAPI routine.

Also, it does seem that it may be tricky to call, because I've read the SO Question: Delphi TPath.GetTempPath result is cropped but that didn't help me get to first base.

Can someone please explain how to declare this function and use it properly passing a Unicode string in Delphi 2009?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2024-09-22 08:51:56

当然。您不需要单独的单元,并且可以在任何地方声明 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;

Sure. You do need not a separate unit and can declare GetLongPathName anywhere:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文