• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1549
  • Last Modified:

Delphi 2010: Get Description from .lnk file

Hi everybody,

I need to read the Description from a Windows .lnk file. I have seen plenty of code to get the target filename, but I need the Description field.

The code that I found for reading the Description field always used a hardcoded filename for the .Lnk file, but when I try to use a filename that is stored in a string it doesn't work (array is incompatible with widestring).

I'd like to just call a method like this:
  LinkDescription := GetLinkDescription( aLinkFileNameInAString );

It should run in Delphi 2010.
Thanks for your help.
0
tacojoosterkamp
Asked:
tacojoosterkamp
  • 2
1 Solution
 
thiagoblimeiraCommented:
function GetShortcutInfo (LinkFile: widestring; var ShortPath, ShortDescription: widestring): boolean;
var var
SL: IShellLinkW; SL: IShellLinkW;
PF: IPersistFile; PF: IPersistFile;
FindData: _WIN32_FIND_DATAW; FindData: _WIN32_FIND_DATAW;
AStr: array[0..MAX_PATH] of WideChar; AStr: array [0 .. MAX_PATH] of WideChar;
IconIndex,ShowCmd:integer; IconIndex, ShowCmd: integer;
HotKey:Word; HotKey: Word;
begin begin
try try
result:=false; result: = false;
OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, OleCheck (CoCreateInstance (CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,
IShellLink, SL)); IShellLink, SL));
PF := SL as IPersistFile; PF: = SL as IPersistFile;
OleCheck(PF.Load(PWideChar(LinkFile), STGM_READ)); OleCheck (PF.Load (PWideChar (LinkFile), STGM_READ));
OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_SHORTPATH)); OleCheck (SL.GetPath (AStr, MAX_PATH, FindData, SLGP_SHORTPATH));
ShortPath := AStr;//‡ö ShortPath: = AStr; / / file name
OleCheck(SL.GetDescription(AStr, MAX_PATH)); OleCheck (SL.GetDescription (AStr, MAX_PATH));
ShortDescription := AStr;//Ïð ShortDescription: = AStr; / / Description
Result:=true; Result: = true;
except except
Result:=false; Result: = false;
end; end;
end; end;
0
 
thiagoblimeiraCommented:
Sorry more readable code here:

function GetShortcutInfo (LinkFile: widestring; var ShortPath, ShortDescription: widestring): boolean;
var
SL: IShellLinkW;  
PF: IPersistFile;
FindData: _WIN32_FIND_DATAW;
AStr: array[0..MAX_PATH] of WideChar; ;
IconIndex,ShowCmd:integer;
HotKey:Word;
begin
try  
result:=false;  
OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, SL));
PF := SL as IPersistFile;  
OleCheck(PF.Load(PWideChar(LinkFile), STGM_READ));
OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_SHORTPATH));
ShortPath := AStr; / / file name
OleCheck(SL.GetDescription(AStr, MAX_PATH));
ShortDescription := AStr; / / Description
Result:=true;  
except
Result: = false;
end;
end;
0
 
tacojoosterkampAuthor Commented:
Thanks thiagoblimeira, that was a great solution!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now