为什么这不能在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.
答案 0 :(得分:8)
因为您在Shell32
来电中提供hShell32
而不是GetProcAddress
。
如果你想知道为什么它失败
没有可以使用这些参数
调用的'GetProcAddress'的重载版本
您可以按住Ctrl键单击第一个参数,IDE将转到编译器为Shell32
找到的常量。
答案 1 :(得分:3)
对于其他想要使用上述代码的人来说,Delphi 2010(可能是2009年?)有一个名为KnownFolders.pas的单元,其中包含所有其他FOLDERID_常量 例如: FOLDERID_RoamingAppData:TGUID ='{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}';
答案 2 :(得分:2)
在Delphi 2010中,所有SHGetKnownFile函数都在单元“shlobj”中定义 FOLDERID常量位于KnownFolders
中