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.