【问题标题】:How Do I Properly Call GetLongPathName Using Delphi 2009 and Unicode Strings?如何使用 Delphi 2009 和 Unicode 字符串正确调用 GetLongPathName?
【发布时间】:2010-08-19 23:58:32
【问题描述】:

我需要将旧的 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 字符串?

【问题讨论】:

    标签: delphi winapi unicode delphi-2009


    【解决方案1】:

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

    【讨论】:

    • 我相信最后一个函数声明给出了:[DCC 错误] 函数需要结果类型
    • @lkessler:不,如果已经在接口部分声明了函数参数和结果类型,则无需在实现部分重新声明它们。
    • 谢谢@Serg。它在所有情况下都不能正常工作,但经过轻微修复后,我让它工作了。我已经更新了您的答案以正确执行。这真的很有帮助。
    猜你喜欢
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多