Jak wyciągnąć ścieżkę do pliku ze skrótu (czyli z pliku *.lnk)

iran

Wymagane moduły (uses): ComObj, ShlObj oraz ActiveX

Funkcja prezentuje się tak:

function ExtractShortcutFile(const Shortcut: String): String;
var
  I_UNK: IUnknown;
  I_SL: IShellLink;
  I_PerF: IPersistFile;

  W32FD: TWin32FindData;
  buffer: array[0..MAX_PATH] of Char;
begin
  I_UNK := CreateComObject(CLSID_ShellLink);
  I_SL := I_UNK as IShellLink;
  I_PerF := I_UNK as IPersistFile;

  I_PerF.Load(PWideChar(Shortcut), STGM_READ);

  I_SL.Resolve(0, SLR_ANY_MATCH or SLR_NO_UI);
  I_SL.GetPath(buffer, MAX_PATH, W32FD, SLGP_RAWPATH);

  Result := buffer;
  I_UNK._Release;
end;

Przykładowe wywołanie kodu:

ExtractShortcutFile('C:\Users\xyz\Desktop\Program.lnk');

Może zwrócić np.C:\Program Files\Prog\Program.exe.

Przydatne linki:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774944%28v=vs.85%29.aspx

FAQ

0 komentarzy