2011-10-09 4 views
7
function GetDesktopFolder: string; 
var 
    buf: array[0..MAX_PATH] of Char; 
    pidList: PItemIDList; 
begin 
    Result := StrNoDesktopFolderFo; 
    SHGetSpecialFolderLocation(Application.Handle, CSIDL_DESKTOP, pidList); 
    if (pidList <> nil) then 
    if (SHGetPathFromIDList(pidList, buf)) then 
     Result := buf; 
end; 

procedure p; 
var 
    i: Integer; 
    IObject: IUnknown; 
    ISLink: IShellLink; 
    IPFile: IPersistFile; 
    PIDL: PItemIDList; 
    InFolder: array[0..MAX_PATH] of Char; 
    TargetName: string; 
    LinkName: string; 
begin 
    TargetName := 'c:\folder\exeFile.exe';//hardcoded example 

    IObject := CreateComObject(CLSID_ShellLink) ; 
    ISLink := IObject as IShellLink; 
    IPFile := IObject as IPersistFile; 

    with ISLink do 
    begin 
    SetDescription('what ever'); 
    SetPath(pChar(TargetName)) ; 
    SetWorkingDirectory(pChar(ExtractFilePath(TargetName))) ; 
    end; 

    SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL) ; 
    SHGetPathFromIDList(PIDL, InFolder) ; 

    LinkName := getDesktopFolder+'\'; 
    i := ; 

    LinkName:= linkname+ExtractFileName(TargetName)+'.lnk'; 

    if LinkName = StrNoDesktopFolderFo then 
    Exit; 
    if not FileExists(LinkName) then 
    IPFile.Save(PWChar(LinkName), False); 

    Application.Terminate; 
end; 

Le code ci-dessus provoque beaucoup d'erreurs dans Delphi et ne peut pas courir deux fois ...Comment créer un raccourci de fichier (fichier * .lnk) sur le bureau sous Windows?

Toutes les idées?

Btw. la source n'est pas à l'origine mienne, elle a été récupérée à partir d'endroits sur le web.

+3

pas le problème, mais vos tableaux doivent être 0..MAX_PATH-1 –

Répondre

11

Je le ferais par exemple. de cette façon

uses 
    ShlObj, ComObj, ActiveX; 

function GetDesktopFolder: string; 
var 
    PIDList: PItemIDList; 
    Buffer: array [0..MAX_PATH-1] of Char; 
begin 
    Result := ''; 
    SHGetSpecialFolderLocation(Application.Handle, CSIDL_DESKTOP, PIDList); 
    if Assigned(PIDList) then 
    if SHGetPathFromIDList(PIDList, Buffer) then 
     Result := Buffer; 
end; 

function CreateDesktopShellLink(const TargetName: string): Boolean; 
var 
    IObject: IUnknown; 
    ISLink: IShellLink; 
    IPFile: IPersistFile; 
    PIDL: PItemIDList; 
    LinkName: string; 
    InFolder: array [0..MAX_PATH-1] of Char; 
begin 
    Result := False; 

    IObject := CreateComObject(CLSID_ShellLink); 
    ISLink := IObject as IShellLink; 
    IPFile := IObject as IPersistFile; 

    with ISLink do 
    begin 
    SetDescription('Description ...'); 
    SetPath(PChar(TargetName)); 
    SetWorkingDirectory(PChar(ExtractFilePath(TargetName))); 
    end; 

    SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL); 
    SHGetPathFromIDList(PIDL, InFolder) ; 

    LinkName := IncludeTrailingBackslash(GetDesktopFolder); 
    LinkName := LinkName + ExtractFileName(TargetName) + '.lnk'; 

    if not FileExists(LinkName) then 
    if IPFile.Save(PWideChar(LinkName), False) = S_OK then 
     Result := True; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    if CreateDesktopShellLink('C:\Folder\ExeFile.exe') then 
    ShowMessage('Link has been created ...'); 
end; 
+1

Je pense que vous avez fait une erreur en lançant une piqûre dans PWideChar ... Il ne fonctionnait pas jusqu'à ce que j'ajouté ceci: LinkName : chaîne; WLinkName: chaîne large; myWideCharPtr: PWideChar; InFolder: array [0..MAX_PATH-1] de Char; puis WLinkName: = LinkName; myWideCharPtr: = Addr (WLinkName [1]); sinon FileExists (LinkName) puis si IPFile.Save (myWideCharPtr, False) = S_OK puis Résultat: = True; – nagylzs

+0

En Delphi 7 fonctionne avec les changements @nagylzs. – tuxar

0

Fonctions pour obtenir les emplacements des dossiers spéciaux, la création de raccourcis (liens), et beaucoup plus se trouvent dans la bibliothèque dsiWin32 freeware de 100+ procédures et fonctions. Téléchargez-le au http://gp.17slon.com/gp/dsiwin32.htm et merci à Primoz Gabrijelcic et à la communauté Delphi-SI de l'avoir mise à disposition.

Max

Questions connexes