A la base de ces codes, un gars du forum anglais (zappé le nom) et Nico. (pour CreateShellLink() et GetShellLinkTarget() )
J'ai juste remis propre et uniforme, j'ai ajouté la compatibilité UNICODE, et j'ai finis le lot de fonctions.
Créer un raccourci
CreateShellLink()
Lire le contenu d'un raccourci
La cible d'un raccourci : GetShellLinkTarget()
Les arguments (ou paramètres) pour lancer la cible : GetShellLinkArgument()
Le dossier d'exécution : GetShellLinkWorkingDirectory()
Le fichier qui contient l'icône du raccrouci : GetShellLinkIconFile()
Et l'index de l'icône dans le fichier : GetShellLinkIconIndex()
Code : Tout sélectionner
DataSection
CLSID_ShellLink :
; 00021401-0000-0000-C000-000000000046
Data.l $00021401
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
IID_IShellLinkA :
Data.l $000214EE
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
IID_IShellLinkW :
Data.l $000214F9
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
IID_IPersistFile :
Data.l $0000010B
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
EndDataSection
ProcedureDLL CreateShellLink(ShortcutFile.s, Target.s, Argument.s, Description.s, WorkingDirectory.s, ShowCommand.l, HotKey.l, IconFile.s, IconIndex.l) ; Créer un raccourci
CoInitialize_(0)
CompilerIf #PB_Compiler_Unicode
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl.IShellLinkW)
CompilerElse
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl.IShellLinkA)
CompilerEndIf
If Instance = 0
; The file TO which is linked ( = target for the Link )
psl\SetPath( @Target)
; Arguments for the Target
psl\SetArguments( @Argument)
; Working Directory
psl\SetWorkingDirectory( @WorkingDirectory)
; Description ( also used as Tooltip for the Link )
psl\SetDescription( @Description)
; Show command:
; SW_SHOWNORMAL = Default
; SW_SHOWMAXIMIZED = aehmm... Maximized
; SW_SHOWMINIMIZED = play Unreal Tournament
psl\SetShowCmd(ShowCommand)
; Hotkey:
; The virtual key code is in the low-order byte,
; and the modifier flags are in the high-order byte.
; The modifier flags can be a combination of the following values:
; HOTKEYF_ALT = ALT key
; HOTKEYF_CONTROL = CTRL key
; HOTKEYF_EXT = Extended key
; HOTKEYF_SHIFT = SHIFT key
psl\SetHotkey(HotKey)
; Set Icon for the Link:
; There can be more than 1 icons in an icon resource file,
; so you have to specify the index.
psl\SetIconLocation( @IconFile, IconIndex)
; Query IShellLink For the IPersistFile interface For saving the
; shortcut in persistent storage.
If psl\QueryInterface(?IID_IPersistFile, @ppf.IPersistFile) = 0
CompilerIf #PB_Compiler_Unicode
; Save the link by calling IPersistFile::Save.
hres = ppf\Save( @ShortcutFile, #True)
CompilerElse
; Ensure that the string is Unicode.
Mem.s = Space(1000) ; AllocateMemory(1,1000)
MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, -1, Mem, 1000)
; Save the link by calling IPersistFile::Save.
hres = ppf\Save( @Mem, #True)
CompilerEndIf
Result = 1
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn Result
EndProcedure
ProcedureDLL.s GetShellLinkTarget(ShortcutFile.s) ; Récupérer la cible d'un raccourci
; Pour récupérer la cible du raccourci
CoInitialize_(0)
#STGM_READ = 0
#SLGP_SHORTPATH = 2
CompilerIf #PB_Compiler_Unicode
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl.IShellLinkW)
CompilerElse
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl.IShellLinkA)
CompilerEndIf
If Instance = 0
If psl\QueryInterface(?IID_IPersistFile, @ppf.IPersistFile) = 0
CompilerIf #PB_Compiler_Unicode
If ppf\Load( @ShortcutFile, #STGM_READ) = 0
Target.s = Space(1024)
psl\GetPath(Target, StringByteLength(Target), 0, #SLGP_SHORTPATH)
EndIf
CompilerElse
Size.l = MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, -1, 0, 0)
Dim unicode.w(Size)
MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, Len(ShortcutFile), unicode(), Size)
If ppf\Load(unicode(), #STGM_READ) = 0
Target.s = Space(1024)
psl\GetPath( @Target, 1024, 0, #SLGP_SHORTPATH)
EndIf
CompilerEndIf
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn Target
EndProcedure
ProcedureDLL.s GetShellLinkArgument(ShortcutFile.s) ; Récupérer l'argument d'un raccourci
; Pour récupérer la cible du raccourci
CoInitialize_(0)
#STGM_READ = 0
CompilerIf #PB_Compiler_Unicode
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl.IShellLinkW)
CompilerElse
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl.IShellLinkA)
CompilerEndIf
If Instance = 0
If psl\QueryInterface(?IID_IPersistFile, @ppf.IPersistFile) = 0
CompilerIf #PB_Compiler_Unicode
If ppf\Load( @ShortcutFile, #STGM_READ) = 0
Argument.s = Space(1024)
psl\GetArguments(Argument, StringByteLength(Argument))
EndIf
CompilerElse
Size.l = MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, -1, 0, 0)
Dim unicode.w(Size)
MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, Len(ShortcutFile), unicode(), Size)
If ppf\Load(unicode(), #STGM_READ) = 0
Argument.s = Space(1024)
psl\GetArguments( @Argument, 1024)
EndIf
CompilerEndIf
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn Argument
EndProcedure
ProcedureDLL.s GetShellLinkWorkingDirectory(ShortcutFile.s) ; Récupérer l'argument d'un raccourci
; Pour récupérer la cible du raccourci
CoInitialize_(0)
#STGM_READ = 0
CompilerIf #PB_Compiler_Unicode
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl.IShellLinkW)
CompilerElse
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl.IShellLinkA)
CompilerEndIf
If Instance = 0
If psl\QueryInterface(?IID_IPersistFile, @ppf.IPersistFile) = 0
CompilerIf #PB_Compiler_Unicode
If ppf\Load( @ShortcutFile, #STGM_READ) = 0
WorkingDirectory.s = Space(1024)
psl\GetWorkingDirectory(WorkingDirectory, StringByteLength(WorkingDirectory))
EndIf
CompilerElse
Size.l = MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, -1, 0, 0)
Dim unicode.w(Size)
MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, Len(ShortcutFile), unicode(), Size)
If ppf\Load(unicode(), #STGM_READ) = 0
WorkingDirectory.s = Space(1024)
psl\GetWorkingDirectory( @WorkingDirectory, 1024)
EndIf
CompilerEndIf
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn WorkingDirectory
EndProcedure
ProcedureDLL.s GetShellLinkIconFile(ShortcutFile.s) ; Récupérer le fichier icône d'un raccourci
; Pour récupérer la cible du raccourci
CoInitialize_(0)
#STGM_READ = 0
CompilerIf #PB_Compiler_Unicode
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl.IShellLinkW)
CompilerElse
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl.IShellLinkA)
CompilerEndIf
If Instance = 0
If psl\QueryInterface(?IID_IPersistFile, @ppf.IPersistFile) = 0
CompilerIf #PB_Compiler_Unicode
If ppf\Load( @ShortcutFile, #STGM_READ) = 0
IconFile.s = Space(1024)
psl\GetIconLocation(IconFile, StringByteLength(IconFile), @IconIndex.l)
EndIf
CompilerElse
Size.l = MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, -1, 0, 0)
Dim unicode.w(Size)
MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, Len(ShortcutFile), unicode(), Size)
If ppf\Load(unicode(), #STGM_READ) = 0
IconFile.s = Space(1024)
psl\GetIconLocation( @IconFile, StringByteLength(IconFile), @IconIndex.l)
EndIf
CompilerEndIf
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn IconFile
EndProcedure
ProcedureDLL.l GetShellLinkIconIndex(ShortcutFile.s) ; Récupérer l'index de l'icône d'un raccourci
; Pour récupérer la cible du raccourci
CoInitialize_(0)
#STGM_READ = 0
CompilerIf #PB_Compiler_Unicode
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkW, @psl.IShellLinkW)
CompilerElse
Instance = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLinkA, @psl.IShellLinkA)
CompilerEndIf
If Instance = 0
If psl\QueryInterface(?IID_IPersistFile, @ppf.IPersistFile) = 0
CompilerIf #PB_Compiler_Unicode
If ppf\Load( @ShortcutFile, #STGM_READ) = 0
IconFile.s = Space(1024)
psl\GetIconLocation(IconFile, StringByteLength(IconFile), @IconIndex.l)
EndIf
CompilerElse
Size.l = MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, -1, 0, 0)
Dim unicode.w(Size)
MultiByteToWideChar_(#CP_ACP, 0, ShortcutFile, Len(ShortcutFile), unicode(), Size)
If ppf\Load(unicode(), #STGM_READ) = 0
IconFile.s = Space(1024)
psl\GetIconLocation( @IconFile, StringByteLength(IconFile), @IconIndex.l)
EndIf
CompilerEndIf
ppf\Release()
EndIf
psl\Release()
EndIf
CoUninitialize_()
ProcedureReturn IconIndex
EndProcedure
Ensuite, vous avez un lanceur et vous souhaitez exécuter un raccourci :
faire
Code : Tout sélectionner
RunProgram(Raccourci.s)
L'erreur est bizarre, windows ne connait pas le programme pour ouvrir les fichiers *.lnk
La solution est encore plus bizarre car il faut remplacer les "\" par "\\" dans le chemin du raccourci. Comme ceci :
Code : Tout sélectionner
Fichier.s = "D:\PureBasic\Développements\Utilitaires\Barre & Menu\Barre & Menu.lnk"
RunProgram(ReplaceString(Fichier, "\", "\\", 0, 3))