0

I'm using SHGetFolderPath CSIDL_APPDATA to get path to the application data folder under current user account. It works fine, but if it is called from an installer, Windows change it to administrator's folder! How can I get the current user's data folder from an installer?

Vojtěch Melda Meluzín
  • 1,117
  • 3
  • 11
  • 22
  • 2
    We don't know anything about your installer. So, very hard to know why this is happening. You might want to ask yourself why you are writing to the current user's profile at install time. What if an admin installs the software for the user? – David Heffernan Jul 18 '14 at 11:40
  • You should be writing the per-user data when the user first runs the application, not at install time. Otherwise the application will only work properly for the user who installed it, which is wrong. – Harry Johnston Jul 19 '14 at 08:18

1 Answers1

1

SHGetFolderPath() has an hToken parameter for this exact situation. If hToken is NULL, the function uses the access token associated with the calling thread. Otherwise it can be set to an access token for another user account.

If the installer is running elevated as an admin, the installer will have to either:

  1. impersonate the desired user account before calling SHGetFolderPath().

  2. obtain an access token for the desired user account and pass it to hToken.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Ok thank you, but how do I get the token for "current user". Apparently NULL is admin in that case, -1 is "default user", but no other special value is used. – Vojtěch Melda Meluzín Jul 18 '14 at 16:40
  • Passing `NULL` is using the admin account because an admin is running the installer, not the current logged in user (assuming the admin and logged in user are not the same). One thing you could try is using [`WTSQuerySessionInformation()`](http://msdn.microsoft.com/en-us/library/aa383838.aspx) to get the session ID that the installer is running in, then use [`WTSQueryUserToken()`](http://msdn.microsoft.com/en-us/library/aa383840.aspx) to get the access token for the user logged in to that session. – Remy Lebeau Jul 18 '14 at 17:14