How to properly call GetLongPathName using Delphi 2009 and Unicode Strings?

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

I looked around and could not find its Unicode compatible version.

It seems that the correct procedure to use is GetLongPathName from WinAPI . But it seems that this is not in the SysUtils Delphi 2009 library, and I could not figure out how to correctly declare its access to the WinAPI routine.

It also seems like it can be difficult to call because I read the SO question: Delphi TPath.GetTempPath result is clipped , but it doesn’t, t help me get to first base.

Can someone explain how to declare this function and use it correctly by passing the Unicode string in Delphi 2009?

+3
source share
1 answer

Of course. You do not need a separate device and you 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;
+4
source

Source: https://habr.com/ru/post/1760655/


All Articles