Mega Code Archive

 
Categories / Delphi / Files
 

Get Long File Name

Title: Get Long File Name Question: Get the corresponing long file name (LFN) if you only have a short one. Works on Win 9x and NT/2000. Answer: function GetLongPathName(const ShortName : string) : string; var pcBuffer: PChar; iHandle, iLen : Integer; GetLongPathName: function (ShortPathName: PChar; LongPathName: PChar; cchBuffer: Integer): Integer stdcall; begin Result := ShortName; iHandle := GetModuleHandle('kernel32.dll'); if (iHandle 0) then begin @GetLongPathName := GetProcAddress(iHandle, 'GetLongPathNameA'); if Assigned(GetLongPathName) then begin pcBuffer := StrAlloc(MAX_PATH + 1); iLen := GetLongPathName(PChar(ShortName), pcBuffer, MAX_PATH); // if result = 0 : conversion failed // if result MAX_PATH == conversion failed, buffer not large enough if (iLen 0) and (iLen Result := StrPas(pcBuffer); end; // if (iLen StrDispose(pcBuffer); end; // if Assigned(GetLongPathName) then FreeLibrary(iHandle); end; // if (iHandle 0) then end;