Why does ShGetFolderPath return nil when the folder exists on some Vista pc's
While testing our apps we found that using ShGetFolderPath to return the AppData path the function returns nil even though the folder exists on a test PC. On the development PC ShGetFolderPath returns the AppData path with no error.
The development PC and the test PC are running Vista.
function GetShellFolder( ID: Cardinal; Create: Boolean = False ): string;
// This function is a superset of SHGetSpecialFolderPath, included with
// earlier versions of the Shell. On systems preceeding those including
// Shell32.dll version 5.0 (Windows Millennium Edition (Windows Me) and
// Windows 2000), SHGetFolderPath was obtained through SHFolder.dll,
// distributed with Microsoft Internet Explorer 4.0 and later versions.
// Takes开发者_运维百科 the CSIDL of a folder and returns the path or 'Could not determine
// folder path' if it does not exist. Creates the folder if it does not
// exist if Create is true.
var
Res: HResult;
Path: array [ 0 .. Max_Path ] of Char;
begin
if Create then
ID := ID or csidl_Flag_Create;
Res := ShGetFolderPath( 0, ID, 0, shgfp_Type_Current, Path );
if S_OK <> Res then
begin
Result := 'Could not determine folder path';
raise Exception.Create( 'Could not determine folder path' );
end;
Result := Path;
end;
GetShellFolder( CSIDL_LOCAL_APPDATA, False );
On the development machine the CSIDL_LOCAL_APPDATA path is returned successfully, but on a test PC the CSIDL_LOCAL_APPDATA folder is not returned.
Does anyone know why the CSIDL_LOCAL_APPDATA folder is not returned on the test PC even though the folder exists on the hard drive? The test machine returns the history folder with CSIDL_HISTORY, yet it does not return the local appdata folder with CSIDL_LOCAL_APPDATA.
On the test PC explorer shows the CSIDL_LOCAL_APPDATA folder as users\user\AppData\Local. On the test PC explorer shows the CSIDL_HISTORY folder as users\user\AppData\Local\Microsoft\Windows\History.
if we call GetShellFolder( CSIDL_LOCAL_APPDATA, True) the function still does not return the folder path.
What am I doing wrong or how can I fix this problem?
Some additional info that may come in handy: 1. what delphi version (unicode or ansi) 2. is the exception raised or not? If it is, what is the exact result of the shGetFolderPat call? call (we now it's not S_OK, but what is it?)
As to the actual answer, according to the specs, PATH-should be a zero-terminated string of length MAX_PATH. Currently, ít isn't initialized at all (local variable), which might explain the difference between the two machines. You might want to try fillcharring it to zero first. Long shot, I admit.
精彩评论