IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API, COM et SDKs Delphi Discussion :

Problème en essayant de lire le contenu d'un .lnk


Sujet :

API, COM et SDKs Delphi

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    63
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 63
    Par défaut Problème en essayant de lire le contenu d'un .lnk
    Rebonjour,

    Je tente de lire le contenu d'un fichier lnk, plus exactement de récupérer
    le path complet au fichier d'origine.

    Pour ce faire, j'ai trouvé sur ngscan le code suivant proposé par Stephane Grobety :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    function GetShortcutArguments(ShortcutFileName: WideString): string; 
    var 
      MyObject : IUnknown; 
      MySLink : IShellLink; 
      MyPFile : IPersistFile; 
    begin 
      MyObject := CreateComObject(CLSID_ShellLink); 
      MySLink := MyObject as IShellLink; 
      MyPFile := MyObject as IPersistFile; 
     
      OleCheck(MyPFile.Load(PWideChar(ShortcutFileName), 0)); 
      SetLength(result, 1024); 
      OleCheck(MySLink.GetArguments(PChar(result), 1024)); 
      result := trim(result); 
    end;
    Je l'ai juste adapté comme suit pour pouvoir appeler avec un string :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    function TForm1.GetShortcutArguments(lnk : string): string;
    var
      aws : array[0..MAX_PATH] of WideChar;
      MyObject : IUnknown;
      MySLink : IShellLink;
      MyPFile : IPersistFile;
    begin
      MultiByteToWideChar(CP_ACP, 0, PChar(lnk), -1, @aws, MAX_PATH);
     
      MyObject := CreateComObject(CLSID_ShellLink);
      MySLink := MyObject as IShellLink;
      MyPFile := MyObject as IPersistFile;
     
      OleCheck(MyPFile.Load(aws, 0));
      SetLength(result, 1024);
      OleCheck(MySLink.GetArguments(PChar(result), 1024));
      result := trim(result);
    end;
    Si le passe un chemin de lnk inexistant, j'ai un message d'erreur stipulant
    que Le fichier spécifié est introuvable, ce qui me laisse à penser
    que l'appel OleCheck(MyPFile.Load(aws, 0)); fonctionne bien.


    Si je passe un chemin de lnk valide, je n'ai pas de message d'erreur,
    mais en revanche la variable result après execution de
    OleCheck(MySLink.GetArguments(PChar(result), 1024));
    a l'air non initialisée et ne contient rien d'attendu.

    Qu'est-ce qui ne va pas ?

    Merci.

  2. #2
    Membre Expert
    Avatar de Lung
    Profil pro
    Analyste-programmeur
    Inscrit en
    Mai 2002
    Messages
    2 700
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste-programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 700
    Par défaut
    Moi, j'utilise ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    const
       TAILLE_BUFFER = 1024;
     
    type
       ArrayChar = array[0..TAILLE_BUFFER] of Char;
     
    ...
     
       function Infos_raccourci(szChemin: String): TStringList;
       const
          IID_IPersistFile: TGUID = (D1: $0000010B; D2: $0000; D3: $0000; D4: ($C0, $00, $00, $00, $00, $00, $00, $46));
       var
          PersistFile: IPersistFile;
          FileNameW: array[0..MAX_PATH] of WideChar;
          ShellLink: IShellLink;
          fd: TWin32FindData;
          ProLink: ArrayChar;
          I: Word;
          J: Integer;
       begin
          Result := TStringList.Create;
          CoInitialize(nil);
          try
             OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, ShellLink));
             try
                OleCheck(ShellLink.QueryInterface(IID_IPersistFile, PersistFile));
                try
                   MultiByteToWideChar(CP_ACP, 0, @szChemin[1], -1, FileNameW, MAX_PATH);
                   OleCheck(PersistFile.Load(FileNameW, STGM_READ));
                   ShellLink.GetPath(ProLink, Max_Path, fd, SLGP_UNCPRIORITY);
                   Result.Add(ProLink);
                finally
                end;
             finally
             end;
          finally
             CoUninitialize;
          end;
       end;
    Si ca peut t'aider ...

    L'urgent est fait, l'impossible est en cours, pour les miracles prévoir un délai. :bug: ___ "http://club.developpez.com/regles/#LIII-A"Écrivez dans un français correct !!

    C++Builder 5 - Delphi 6#2 Entreprise - Delphi 2007 Entreprise - Delphi 2010 Architecte - Delphi XE Entreprise - Delphi XE7 Entreprise - Delphi 10 Entreprise - Delphi 10.4.2 Entreprise - Delphi 11.3 Entreprise - Visual studio 2022
    OpenGL 2.1 - Oracle 10g - Paradox - Interbase (XE) - PostgreSQL (15.7)

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2002
    Messages : 16
    Par défaut Oui Mais...
    Bonjour

    Effectivement, le code me renvoie bien la cible du fichier "LNK" mais..

    Comment avoir les arguments ? je ne trouve pas..

    Merci de répondre si vous avez.. La réponse

  4. #4
    Membre Expert
    Avatar de Lung
    Profil pro
    Analyste-programmeur
    Inscrit en
    Mai 2002
    Messages
    2 700
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste-programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 700
    Par défaut
    Faut regarder les fonctions de l'objet ShellLink.
    Par exemple ShellLink.GetArguments() (pas testé).
    L'urgent est fait, l'impossible est en cours, pour les miracles prévoir un délai. :bug: ___ "http://club.developpez.com/regles/#LIII-A"Écrivez dans un français correct !!

    C++Builder 5 - Delphi 6#2 Entreprise - Delphi 2007 Entreprise - Delphi 2010 Architecte - Delphi XE Entreprise - Delphi XE7 Entreprise - Delphi 10 Entreprise - Delphi 10.4.2 Entreprise - Delphi 11.3 Entreprise - Visual studio 2022
    OpenGL 2.1 - Oracle 10g - Paradox - Interbase (XE) - PostgreSQL (15.7)

  5. #5
    Expert éminent
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    14 026
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 14 026
    Par défaut
    J'avais le GetPath mais pas l'autre dans TSLTShellLink
    Tient il est aussi dans la
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
      /// <summary>TSLTShellLink encapsule l'utilisation de l'interface IShellLink</summary>
      TSLTShellLink = class(TObject)
      private
        const
          IID_IPersistFile: TGUID = '{0000010B-0000-0000-C000-000000000046}';
      public
        class function ExtractTarget(const AShellLinkFileName: TFileName): TFileName;
        class function ExtractArgument(const AShellLinkFileName: TFileName): string;
      end;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
     
    { TSLTShellLink }
     
    //------------------------------------------------------------------------------
    class function TSLTShellLink.ExtractTarget(const AShellLinkFileName: TFileName): TFileName;
    // Voir la fonction cachée "GetFileNameFromLink" dans Vcl.ExtDlgs
    // Ne pase inclure Winapi.Ole2 qui contient IID_IPersistFile, cela devient trop pénible les conflits entre TGUID de Delphi et TGUID de Winapi.Ole2
    var
      LResult: HRESULT;
      LSHellLink: IShellLink;
      LPersistFile: IPersistFile;
      LFindData: TWin32FindData;
      FileName: array[0..MAX_PATH - 1] of Char;
    begin
      Result := '';
      LResult := CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkW, LShellLink);
      if LResult = S_OK then
      begin
        if Supports(LShellLink, IID_IPersistFile, LPersistFile) then
        begin
          LResult := LPersistFile.Load(PChar(AShellLinkFileName), STGM_READ);
          if LResult = S_OK then
          begin
            LResult := LSHellLink.GetPath(FileName, MAX_PATH, LFindData, SLGP_UNCPRIORITY);
            if LResult = S_OK then
              Result := Trim(FileName);
          end;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    class function TSLTShellLink.ExtractArgument(const AShellLinkFileName: TFileName): string;
    var
      LResult: HRESULT;
      LSHellLink: IShellLink;
      LPersistFile: IPersistFile;
      LFindData: TWin32FindData;
      Arguments: array[0..Winapi.CommCtrl.INFOTIPSIZE - 1] of Char;
    begin
      Result := '';
      LResult := CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkW, LShellLink);
      if LResult = S_OK then
      begin
        if Supports(LShellLink, IID_IPersistFile, LPersistFile) then
        begin
          LResult := LPersistFile.Load(PChar(AShellLinkFileName), STGM_READ);
          if LResult = S_OK then
          begin
            LResult := LSHellLink.GetArguments(Arguments, Winapi.CommCtrl.INFOTIPSIZE);
            if LResult = S_OK then
              Result := Trim(Arguments);
          end;
        end;
      end;
    end;
    Petit Essai en XE2 sur Win7

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    procedure TForm1.Panel1Click(Sender: TObject);
    var
      P, S: string;
      sr: TSearchRec;
    begin
      P := 'C:\Users\Public\Desktop\';
      // Les Fichiers !
      if FindFirst(P+'*.lnk', faNormal, sr) = 0 then
      begin
        try
          repeat
            S := S + sr.Name + ' : ' + TThomShellLink.ExtractTarget(P+sr.Name) + ' - ' + TThomShellLink.ExtractArgument(P+sr.Name) + sLineBreak;
          until FindNext(sr) <> 0;
        finally
          FindClose(sr);
        end;
      end;
     
      ShowMessage(S);
    end;
    et le résultat

    Code texte : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Acrobat Reader*DC.lnk : C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe - 
    Delphi 10.1 Berlin.lnk : C:\Program Files (x86)\Embarcadero\Studio\18.0\bin\bds.exe - -pDelphi
    FileZilla Client.lnk : C:\Program Files\FileZilla FTP Client\filezilla.exe - 
    Firefox.lnk : C:\Program Files (x86)\Mozilla Firefox\firefox.exe - 
    Google Chrome.lnk : C:\Program Files (x86)\Google\Chrome\Application\chrome.exe - 
    iTunes.lnk : C:\Program Files (x86)\iTunes\iTunes.exe - 
    Remote Control.lnk : C:\Program Files (x86)\LANDesk\ServerManager\RCViewer\isscntr.exe - -c"Remote Control"
    TeamViewer 12.lnk : C:\Program Files (x86)\TeamViewer\TeamViewer.exe - 
    Toad for Oracle 12.10.lnk : C:\Program Files (x86)\Dell\Toad for Oracle 2016 R2 Suite\Toad for Oracle 12.10\Toad.exe - 
    VLC media player.lnk : C:\Program Files (x86)\VideoLAN\VLC\vlc.exe - 
    Zebra Setup Utilities.lnk : C:\Program Files (x86)\Zebra Technologies\Zebra Setup Utilities\App\PrnUtils.exe -
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

Discussions similaires

  1. Problème pour lire le contenu d'une url
    Par eric41 dans le forum Langage
    Réponses: 3
    Dernier message: 25/12/2012, 23h30
  2. Réponses: 1
    Dernier message: 09/11/2007, 18h40
  3. [Mail] Lire le contenu d'un mail
    Par hdd dans le forum Langage
    Réponses: 5
    Dernier message: 31/10/2004, 13h30
  4. [Fichier] Lire le contenu d'un fichier
    Par bart64 dans le forum Entrée/Sortie
    Réponses: 6
    Dernier message: 19/09/2004, 18h18
  5. [VB.NET] Impossible de lire le contenu du datagrid
    Par jagdjg dans le forum ASP.NET
    Réponses: 2
    Dernier message: 05/05/2004, 21h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo