Extract path of a windows shortcut lazarus / 13-General ---------------------------------------------------------------------- fgsav · 2005-12-21 19:46 Hi everyone and thanks for trying to help me   I work under Windows XP and i'd like to get the path of a .lnk file. I don't find anyway to do this except doing it myself with opendialog... After searching the net I found a solution for delphi with IShellLink, but it seems there's no way to get it work under lazarus. If someone has a suggestion... THX !  :wink: ---------------------------------------------------------------------- totya · 2016-10-12 22:13 This is my question is too... I want to load (and doesn't execute) a file from windows shortcut. Thanks! ---------------------------------------------------------------------- lainz · 2016-10-12 23:11 Use this unit taken from here: http://www.informit.com/articles/article.aspx?p=26940&seqNum=4 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit WinShell; interface uses SysUtils, Windows, Registry, ActiveX, ShlObj; type EShellOleError = class(Exception);  TShellLinkInfo = record  PathName: string;  Arguments: string;  Description: string;  WorkingDirectory: string;  IconLocation: string;  IconIndex: integer;  ShowCmd: integer;  HotKey: word; end;  TSpecialFolderInfo = record  Name: string;  ID: Integer; end; const SpecialFolders: array[0..29] of TSpecialFolderInfo = (  (Name: 'Alt Startup'; ID: CSIDL_ALTSTARTUP),  (Name: 'Application Data'; ID: CSIDL_APPDATA),  (Name: 'Recycle Bin'; ID: CSIDL_BITBUCKET),  (Name: 'Common Alt Startup'; ID: CSIDL_COMMON_ALTSTARTUP),  (Name: 'Common Desktop'; ID: CSIDL_COMMON_DESKTOPDIRECTORY),  (Name: 'Common Favorites'; ID: CSIDL_COMMON_FAVORITES),  (Name: 'Common Programs'; ID: CSIDL_COMMON_PROGRAMS),  (Name: 'Common Start Menu'; ID: CSIDL_COMMON_STARTMENU),  (Name: 'Common Startup'; ID: CSIDL_COMMON_STARTUP),  (Name: 'Controls'; ID: CSIDL_CONTROLS),  (Name: 'Cookies'; ID: CSIDL_COOKIES),  (Name: 'Desktop'; ID: CSIDL_DESKTOP),  (Name: 'Desktop Directory'; ID: CSIDL_DESKTOPDIRECTORY),  (Name: 'Drives'; ID: CSIDL_DRIVES),  (Name: 'Favorites'; ID: CSIDL_FAVORITES),  (Name: 'Fonts'; ID: CSIDL_FONTS),  (Name: 'History'; ID: CSIDL_HISTORY),  (Name: 'Internet'; ID: CSIDL_INTERNET),  (Name: 'Internet Cache'; ID: CSIDL_INTERNET_CACHE),  (Name: 'Network Neighborhood'; ID: CSIDL_NETHOOD),  (Name: 'Network Top'; ID: CSIDL_NETWORK),  (Name: 'Personal'; ID: CSIDL_PERSONAL),  (Name: 'Printers'; ID: CSIDL_PRINTERS),  (Name: 'Printer Links'; ID: CSIDL_PRINTHOOD),  (Name: 'Programs'; ID: CSIDL_PROGRAMS),  (Name: 'Recent Documents'; ID: CSIDL_RECENT),  (Name: 'Send To'; ID: CSIDL_SENDTO),  (Name: 'Start Menu'; ID: CSIDL_STARTMENU),  (Name: 'Startup'; ID: CSIDL_STARTUP),  (Name: 'Templates'; ID: CSIDL_TEMPLATES)); function CreateShellLink(const AppName, Desc: string; Dest: Integer): string;function GetSpecialFolderPath(Folder: Integer; CanCreate: Boolean): string;procedure GetShellLinkInfo(const LinkFile: WideString; var SLI: TShellLinkInfo);procedure SetShellLinkInfo(const LinkFile: WideString; const SLI: TShellLinkInfo); implementation uses ComObj; function GetSpecialFolderPath(Folder: Integer; CanCreate: Boolean): string;var FilePath: widestring; PFilePath: PWideChar; //array[0..MAX_PATH] of char;begin SetLength(FilePath, MAX_PATH); PFilePath := Addr(FilePath[1]); { Get path of selected location } SHGetSpecialFolderPathW(0, PFilePath, Folder, CanCreate); Result := FilePath;end; function CreateShellLink(const AppName, Desc: string; Dest: Integer): string;{ Creates a shell link for application or document specified in }{ AppName with description Desc. Link will be located in folder }{ specified by Dest, which is one of the string constants shown }{ at the top of this unit. Returns the full path name of the  }{ link file. }var SL: IShellLink; PF: IPersistFile; LnkName: WideString;begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,  IShellLink, SL)); { The IShellLink implementer must also support the IPersistFile } { interface. Get an interface pointer to it. } PF := SL as IPersistFile; OleCheck(SL.SetPath(PChar(AppName))); // set link path to proper file if Desc <> '' then  OleCheck(SL.SetDescription(PChar(Desc))); // set description { create a path location and filename for link file } LnkName := GetSpecialFolderPath(Dest, True) + '\' +       ChangeFileExt(AppName, 'lnk'); PF.Save(PWideChar(LnkName), True);     // save link file Result := LnkName;end; procedure GetShellLinkInfo(const LinkFile: WideString; var SLI: TShellLinkInfo);{ Retrieves information on an existing shell link }var SL: IShellLink; PF: IPersistFile; FindData: TWin32FindData; AStr: array[0..MAX_PATH] of char;begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,  IShellLink, SL)); { The IShellLink implementer must also support the IPersistFile } { interface. Get an interface pointer to it. } PF := SL as IPersistFile; { Load file into IPersistFile object } OleCheck(PF.Load(PWideChar(LinkFile), STGM_READ)); { Resolve the link by calling the Resolve interface function. } OleCheck(SL.Resolve(0, SLR_ANY_MATCH or SLR_NO_UI)); { Get all the info! } with SLI do begin  OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_SHORTPATH));  PathName := AStr;  OleCheck(SL.GetArguments(AStr, MAX_PATH));  Arguments := AStr;  OleCheck(SL.GetDescription(AStr, MAX_PATH));  Description := AStr;  OleCheck(SL.GetWorkingDirectory(AStr, MAX_PATH));  WorkingDirectory := AStr;  OleCheck(SL.GetIconLocation(AStr, MAX_PATH, IconIndex));  IconLocation := AStr;  OleCheck(SL.GetShowCmd(ShowCmd));  OleCheck(SL.GetHotKey(HotKey)); end;end; procedure SetShellLinkInfo(const LinkFile: WideString; const SLI: TShellLinkInfo);{ Sets information for an existing shell link }var SL: IShellLink; PF: IPersistFile;begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,  IShellLink, SL)); { The IShellLink implementer must also support the IPersistFile } { interface. Get an interface pointer to it. } PF := SL as IPersistFile; { Load file into IPersistFile object } OleCheck(PF.Load(PWideChar(LinkFile), STGM_SHARE_DENY_WRITE)); { Resolve the link by calling the Resolve interface function. } OleCheck(SL.Resolve(0, SLR_ANY_MATCH or SLR_UPDATE or SLR_NO_UI)); { Set all the info! } with SLI, SL do begin  OleCheck(SetPath(PChar(PathName)));  OleCheck(SetArguments(PChar(Arguments)));  OleCheck(SetDescription(PChar(Description)));  OleCheck(SetWorkingDirectory(PChar(WorkingDirectory)));  OleCheck(SetIconLocation(PChar(IconLocation), IconIndex));  OleCheck(SetShowCmd(ShowCmd));  OleCheck(SetHotKey(HotKey)); end; PF.Save(PWideChar(LinkFile), True);  // save fileend; end. The unit is somewhat changed to work with FPC. And use as this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  data: TShellLinkInfo;begin  WinShell.GetShellLinkInfo('C:\Users\Public\Desktop\Downloads.lnk', data);  ShowMessage(data.PathName); ---------------------------------------------------------------------- wp · 2016-10-12 23:20 Found this in an old Delphi project. Windows-only, of course, but produces a valid result with Laz / fpc 3.0: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Windows, ActiveX, ShlObj; function GetShortcutTarget(const ShortcutFilename: string): string;var  Psl: IShellLink;  Ppf: IPersistFile;  WideName: array[0..MAX_PATH] of WideChar;  pResult: array[0..MAX_PATH-1] of ansiChar;  Data: TWin32FindData;const  IID_IPersistFile: TGUID = (    D1:$0000010B; D2:$0000; D3:$0000; D4:($C0,$00,$00,$00,$00,$00,$00,$46));begin  CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, psl);  psl.QueryInterface(IID_IPersistFile, ppf);  MultiByteToWideChar(CP_ACP, 0, pAnsiChar(ShortcutFilename), -1, WideName, Max_Path);  ppf.Load(WideName, STGM_READ);  psl.Resolve(0, SLR_ANY_MATCH);  psl.GetPath(@pResult, MAX_PATH, Data, SLGP_UNCPRIORITY);  Result := StrPas(pResult);end; ---------------------------------------------------------------------- lainz · 2016-10-12 23:27 I fixed the first code, it works too. ---------------------------------------------------------------------- totya · 2016-10-12 23:57 Hi! Thanks guys for the answers! lainz: "Your" code can open anything, even link with UTF8 filename, and the destination is UTF8 filename, but the result (.PathName) is DOS format, for example: any~1.xls, this is ugly, but as I said it't working. wp: Your code doesn't work with UTF8 filename. Thanks again! Edit: The (test) filename is: Quoteöüóőúéáí_ÖÜÓŐÚÉÁÍ_仙は、一度死ぬことができます.xls ---------------------------------------------------------------------- lainz · 2016-10-13 00:50 There is a line with a parameter that asks for short filename. Try to change that. ---------------------------------------------------------------------- totya · 2016-10-13 04:42 Quote from: lainz on October 13, 2016, 02:50:08 amThere is a line with a parameter that asks for short filename. Try to change that. Unfortunatelly No SLGP_LONGPATH in the param list Edit: But as I see, there is code on the net convert to longpath from the short path.. ---------------------------------------------------------------------- totya · 2016-10-13 23:23 Hi guys, I created working procedures to convert from DOS path, tested with UTF8 filenames, for example: Quoteöüóőúéáí_ÖÜÓŐÚÉÁÍ_仙は、一度死ぬことができます.xls Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit unit_WindowsUtils; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Windows, LazUTF8; function Windows_ExtractLongPathName (const ShortPath: string): string; implementation function Windows_GetLongPathNameWide (ShortPath: PWideChar;                                    LongPath: PWideChar;                                    Buffer: DWord): DWord; stdcall; external kernel32 name 'GetLongPathNameW'; // Convert DOS Path to UTF8 Path// by totya, 2016.10.14.function Windows_ExtractLongPathName (const ShortPath: string): string;const  MaxPathUnicode = 32767; // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980var  Buffer: array [0..MaxPathUnicode] of WideChar;  Required: DWord;begin  Result:= '';   Required:= Windows_GetLongPathNameWide (PWideChar(UTF8ToUTF16(ShortPath)), Buffer{%H-}, Length(Buffer));   if Required > 0 then    Result:=UTF16ToUTF8(StrPas(Buffer));end; end. ---------------------------------------------------------------------- petevick · 2024-03-16 09:42 I know this is an oldish topic, but I've just used the code posted by Lainz in the 3rd reply above, and had to change line 129 from this..... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_SHORTPATH)); to this...... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_RAWPATH)); ....as SLGP_SHORTPATH was returning a shortened path string such as "C:\PROGA~1\FOLDER\" whereas SLGP_RAWPATH returns the full path string such as "C:\PROGRAM FILES\FOLDER\". Apart from that it works like a charm, thanks Lainz. ---------------------------------------------------------------------- lainz · 2024-03-16 12:47 I can't even remember writing that reply  Nice it works.