tags:

views:

195

answers:

1

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?

+1  A: 

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.

Paul-Jan
The Delphi version is Delphi 2010.
Bill
I changed the function to return all standard HRESULT codes as well as the HResult. In the Test PC that fails an error code of -2147024891 is returned.
Bill
That error code, 0x80070005, means E_ACCESSDENIED. Considering this, you might find http://bit.ly/9MEp7x helpful.
Michael Madsen
Thanks for the error code. I added E_ACCESSDENIED to error codes that GetShellFolder reports as an exception now. If I run the application on the test PC as administrator GetShellFolder finds the appdata folder but it is returned as my wife's user name... so now the question is why is access denied when I am the administrator of the PC and why does the AppData Path User not me?
Bill