为什么在Delphi 2009中无法编译?

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.

最佳答案

因为您在Shell32调用中提供hShell32而不是GetProcAddress

如果您想知道为什么它失败了


没有可以使用这些参数调用的'GetProcAddress'的重载版本


您可以ctrl单击第一个参数,IDE将带您进入编译器为Shell32找到的常量。

关于delphi - 获取Vista文件夹路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/976509/

10-09 19:17