2009-06-10 5 views
1

Pourquoi cela ne se compilera-t-il pas dans Delphi 2009?Obtenir les chemins des dossiers Vista

unit VistaFolders; 

interface 

uses Windows, ShellAPI, ShlObj; 

type 
    KNOWNFOLDERID = TGuid; 

const 
    FOLDERID_ProgramData: KNOWNFOLDERID = 
    '{374DE290-123F-4565-9164-39C4925E467B}'; // downloads folder 

var 
    SHGetKnownFolderPathFunc: function(const rfid: KNOWNFOLDERID; 
    dwFlags: DWORD; hToken: THandle; var ppszPath: PWideChar): HResult; stdcall; 
    SHGetKnownFolderIDListFunc: function(const rfid: KNOWNFOLDERID; 
    dwFlags: DWORD; hToken: THandle; var ppidl: PItemIDList): HResult; stdcall; 

    function GetDownloadsFolderPath: string; 

implementation 

uses ActiveX; 

function PathFromIDList(Pidl: ShlObj.PItemIdList): string; 
var 
    Path: array[ 0..MAX_PATH ] of Char; 
begin 
    if SHGetPathFromIDList(Pidl, Path) then 
    Result := Path 
    else 
    Result := ''; 
end; 

function GetDownloadsFolderPath: string; 
var 
    Path: PWideChar; 
    Pidl: PItemIdList; 
begin 
    Result := ''; 
    if @SHGetKnownFolderPathFunc <> nil then 
    begin 
    if Succeeded(SHGetKnownFolderPathFunc(FOLDERID_ProgramData, 0, 0, Path)) then 
     begin 
     try 
      Result := Path; 
     finally; CoTaskMemFree(Path); end; 
     Exit; 
     end; 
    end 
    else if @SHGetKnownFolderIDListFunc <> nil then 
    begin 
    if Succeeded(SHGetKnownFolderIDListFunc(FOLDERID_ProgramData, 0, 0, Pidl)) then 
     begin 
     try 
      Result := PathFromIDList(Pidl); 
     finally; CoTaskMemFree(Pidl); end; 
     Exit; 
     end; 
    end; 
    if Succeeded(SHGetFolderLocation(0, CSIDL_PROFILE, 0, 0, Pidl)) then 
    try 
     Result := PathFromIDList(Pidl) + '\Downloads'; 
    finally; CoTaskMemFree(Pidl); end; 
end; 

procedure InitVistaFunctions; 
var 
    hShell32: THandle; 
begin 
    hShell32 := GetModuleHandle('SHELL32'); 
    @SHGetKnownFolderPathFunc := Windows.GetProcAddress(Shell32, 'SHGetKnownFolderPath'); 
    @SHGetKnownFolderIDListFunc := Windows.GetProcAddress(Shell32, 'SHGetKnownFolderIDList'); 
end; 

initialization 
    InitVistaFunctions; 

end. 
+0

-vous ... Remerciez cela fonctionne parfaitement maintenant. –

+3

Le compilateur vous DIT généralement pourquoi quelque chose ne compilera pas. Ne faites pas deviner tout le monde. Inclure le message d'erreur du compilateur et l'emplacement lorsque vous demandez quel est le problème. –

+0

@Bill Miller: si la réponse de mghie était utile, il serait courtois pour vous de «l'accepter» en cochant la case à côté de sa réponse. – Argalatyr

Répondre

8

Parce que vous donnez Shell32 au lieu de hShell32 dans les GetProcAddress appels.

Si vous vous demandez pourquoi il échoue avec

Il n'y a pas de version surchargée de « GetProcAddress » qui peut être appelée avec ces arguments

vous pouvez ctrl-clic sur le premier paramètre, et l'EDI vous amène à la constante que le compilateur trouve pour Shell32.

4

Juste aux notes pour quiconque de penser à l'aide du code ci-dessus, Delphi 2010 (? Peut-être 2009) a une unité appelée KnownFolders.pas contenant toutes les autres constantes FOLDERID_ par exemple: FOLDERID_RoamingAppData: TGUID = « {3EB685DB-65F9 -4CF6-A03A-E3EF65729F3D} ';

2

Dans Delphi 2010, toutes les fonctions de SHGetKnownFile sont définies dans l'unité « shlobj » Les constantes de folderID sont KnownFolders

Questions connexes