Connaitre la cible d'un raccourci

Résolu
esigvb Messages postés 24 Date d'inscription mercredi 16 mars 2005 Statut Membre Dernière intervention 25 mars 2009 - 31 mai 2006 à 08:14
korntex5 Messages postés 10 Date d'inscription jeudi 7 août 2003 Statut Membre Dernière intervention 25 mai 2008 - 25 mai 2008 à 10:42
Bonjour,
Je recherche à connaitre la cible d'un raccourci.
Voici comment je fais mon raccourci :

procedure CreerRacourci(Repertoire_du_raccourci, Cible_du_raccourci, Description : string);
var
ShellLink : IShellLink;
begin
If UpperCase(extractFileExt(Cible_du_raccourci)) <> '.LNK' Then Cible_du_raccourci := Cible_du_raccourci+'.lnk';
ShellLink := CreateComObject(CLSID_ShellLink) as IShellLink;
ShellLink.SetDescription(PAnsiChar(Description));
ShellLink.SetPath(PAnsiChar(Repertoire_du_raccourci));
ShellLink.SetShowCmd(SW_SHOW);
(ShellLink as IpersistFile).Save(StringToOleStr(Cible_du_raccourci), true);
end;

Je suppose que pour connaitre le raccourci après, il faut utiliser ShellLink, mais je ne sais pas quoi mettre après.

Aidé moi SVP.

Merci.

4 réponses

Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
31 mai 2006 à 14:00
Salut,

voici un petit bout de code que j'avais utilisé il y a longtemps déjà
la base du code n'est pas de moi (je l'avais récuperé je ne sais plus ou sur le net) je l'ai juste complété et réduit.

il existe peut être méthode plus courte mais en attendant elle fonctionne (testé Ok sous D4 pro):

Tu mets un Bouton un Label et un Mémo sur une form et tu complètes comme ceci :

type
  TForm1 = class (TForm)
    Button1: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure GetLink(LinkPath: String);
  private
    { Déclarations privées }


...



implementation




{$R *.DFM}




Uses  ComObj,   //     pour IUnknown
      ActiveX,  //     pour IPersistFile
      ShlObj,   //     pour IShellLink
      Menus;    //     pour ShortCutToKey



procedure TForm1.GetLink(LinkPath: String);
var
    AnObj                            : IUnknown;
    ShLink                           : IShellLink;
    PFile                            : IPersistFile;
    WFileName                        : WideString;
    WorkingDir,Target ,IconLocation,
    Description, Arguments           : String;
    X1                               : Array [0..255] Of Char;
    Data                             : TWin32FindData;
    App1 ,ShowCmd,IconNumber         : Integer;
    W ,HotKey                        : Word;
    TheKey                           : Word;
    TheShiftState                    : TShiftState;



begin

  if UpperCase(ExtractFileExt(LinkPath)) <> '.LNK'then
  begin
     ShowMessage('Erreur: '+LinkPath+' n''est pas unRacourci valide');
     exit; // Si ce n'est pas un racourci on quitte
  end;
  // access to the two interfaces of the object
  AnObj : = CreateComObject(CLSID_ShellLink);
  ShLink := AnObj as IShellLink;
  PFile := AnObj as IPersistFile;


  // Load with a WideString filename
  WFileName := LinkPath;
  PFile.Load(PWChar(WFileName),STGM_READ);


  Shlink.GetDescription(@X1,MAX_PATH);
  Description  := StrPas(@X1);


  SHlink.GetIconLocation(@X1,MAX_PATH,App1);
  IconLocation := StrPas(@X1);
  IconNumber   := App1;
  App1         := 0;


  SHlink.GetShowCmd(App1);
  ShowCmd      := App1;
 
  W            := 0;
  SHlink.GetHotKey(W);
  HotKey       := W;


  SHlink.GetArguments(@X1,MAX_PATH );
  Arguments    := StrPas(@X1);


  SHLink.GetWorkingDirectory(@X1,MAX_PATH );
  WorkingDir   := StrPas(@X1);


  SHlink.GetPath( @X1,MAX_PATH,Data,SLGP_UNCPRIORITY);
  Target       := StrPas(@X1);


  Label1.Caption:=StrPas(@X1)+' '+IntToStr(App1);
  ShortCutToKey(HotKey, TheKey, TheShiftState);


  With Memo1.Lines do
    Begin
      Clear;
      Add('Cible        :  '+  Target  );
      Add('Repertoire   :  '+  WorkingDir );
      Add('Icon         :  '+  IconLocation );
      Add('N° Icon      :  '+  IntToStr(IconNumber) );
      Add('Hot Key      :  '+  ShortCutToText(W) );
      Add('Arguments    :  '+  Arguments );
      Add('Description  :  '+  Description );
      Add('ShowCmd      :  '+  IntToStr(ShowCmd) );
    End;



end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  GetLink('C:\Documents and Settings\Administrateur\Bureau\PSPad.lnk');
end;



end.

Voilà c'est tout





<hr />




n'oubliez pas de cliquer sur
Réponse Acceptée





si elle vous convient






<hr />













@+
Cirec
1
esigvb Messages postés 24 Date d'inscription mercredi 16 mars 2005 Statut Membre Dernière intervention 25 mars 2009
31 mai 2006 à 15:52
Salut Cirec,

Merci de ta réponse. Ca marche super bien (Delphi 2005), ta méthode donne toutes les informations du raccourci. Je n'avais besoin que de connaitre la cible, alors le code est très cours. Je garde bien ta source de côté...


Ci-dessous, la procédure au minimum pour connaitre la cible du raccourci.

procedure TForm2.Connaitre_la_cible(Chemin_du_raccourci: String);
var
Unknown : IUnknown;
ShellLink : IShellLink;
PersistFile : IPersistFile;
WFileName : WideString;
Cible_du_raccourci : String;
X1 : Array [0..255] Of Char;
Data : TWin32FindData;
begin

if UpperCase(ExtractFileExt(Chemin_du_raccourci)) <> '.LNK' then
begin
ShowMessage('Erreur: '+Chemin_du_raccourci+' n''est pas unRacourci valide');
exit; // Si ce n'est pas un racourci on quitte
end;

// Accéder aux deux interfaces de l'objet
Unknown := CreateComObject(CLSID_ShellLink);
ShellLink := Unknown as IShellLink;
PersistFile := Unknown as IPersistFile;

// Chargement du raccourci
WFileName := Chemin_du_raccourci;
PersistFile.Load(PWChar(WFileName),STGM_READ);

// Récupère le path de la cible
ShellLink.GetPath( @X1,MAX_PATH,Data,SLGP_UNCPRIORITY);
Cible_du_raccourci := StrPas(@X1);

// Affiche le résultat
Label1.Caption:='Cible : '+ Cible_du_raccourci;
end;


Merci encore à toi Cirec, pour ta réponse claire comme d'habitude.
Merci.
0
Tituit2007 Messages postés 4 Date d'inscription dimanche 9 décembre 2007 Statut Membre Dernière intervention 24 mars 2008
24 mars 2008 à 14:19
merci!!!!!!!!!!!!
0
korntex5 Messages postés 10 Date d'inscription jeudi 7 août 2003 Statut Membre Dernière intervention 25 mai 2008
25 mai 2008 à 10:42
Je remet en forme

procedure TForm2.Connaitre_la_cible(Chemin_du_raccourci: String);
  var Unknown : IUnknown;
  ShellLink : IShellLink;
  PersistFile : IPersistFile;
  WFileName : WideString;
  Cible_du_raccourci : String;
  X1 : Array [0..255] Of Char;
  Data : TWin32FindData;
begin
  if UpperCase(ExtractFileExt(Chemin_du_raccourci)) <> '.LNK' then
  begin
    ShowMessage('Erreur: '+Chemin_du_raccourci+' n''est pas unRacourci valide');
    exit; // Si ce n'est pas un racourci on quitte
  end;
// Accéder aux deux interfaces de l'objet
  Unknown := CreateComObject(CLSID_ShellLink);
  ShellLink := Unknown as IShellLink;
  PersistFile := Unknown as IPersistFile; 
// Chargement du raccourci
  WFileName := Chemin_du_raccourci; PersistFile.Load(PWChar(WFileName),STGM_READ);
// Récupère le path de la cible
  ShellLink.GetPath( @X1,MAX_PATH,Data,SLGP_UNCPRIORITY);
  Cible_du_raccourci := StrPas(@X1); // Affiche le résultat Label1.
  Caption:='Cible : '+ Cible_du_raccourci;
end;
0
Rejoignez-nous