how to get shortcut link target in delphi 2009

eNarc
eNarc used Ask the Experts™
on
Hi how do I get the Target of a Shortcut link?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
have a look in here, you will find complete code on how to find link target if this is what you need. also you can download the sample code!

Author

Commented:
doesn't work in delphi 2009, been there already.

if I change PChar(LinkFileName),  to pansichar it brings nothing back, though it wont complie otherwise.
Commented:
try this one I tested and it works...

I got it from http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10289022.html

create this function
function FileNameFromLink(const LinkName: String): String;
// dereferences the given link name to the file/folder name associated with the link
var
  AObject: IUnknown;
  ASLink: IShellLink;
  APFile: IPersistFile;
  WFileName: WideString;
  PFD: TWin32FindData;
begin
  // create a helper interface to dereference the link (because this is local it will
  // be freed automatically on exit)
  AObject := CreateComObject(CLSID_ShellLink);
  ASLink := AObject as IShellLink;
  APFile := AObject as IPersistFile;
  // convert to wide string
  WFileName := LinkName;
  APFile.Load(PWideChar(WFileName), 0);
  SetLength(Result, MAX_PATH);
  ASLink.GetPath(PChar(Result), MAX_PATH, PFD, 0);
end;

Open in new window


and then

ShowMessage(FileNameFromLink ('D:\SPDecr - Shortcut.lnk'));

Open in new window

this will give you original file path

for me original path was: C:\Users\Barlet\Desktop\SPDecr.mdb and shortcut was on 'D:\'
Ephraim WangoyaSoftware Engineer
Commented:
Use this modification
function TForm1.GetTarget(const LinkFileName: string): string;
var
  ShellLink: IShellLink;
  PersistFile: IPersistFile;
  Info: array[0..MAX_PATH] of Char;
  FindData: TWin32FindData;
begin
  Result := '';
  CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, ShellLink);
  if ShellLink.QueryInterface(IPersistFile, PersistFile) = 0 then
  begin
    PersistFile.Load(PChar(LinkFileName), STGM_READ);
    ShellLink.GetPath(@Info, MAX_PATH, FindData, SLGP_UNCPRIORITY);
    Result := Info;
  end;
end;

Open in new window

Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial