locked
Get TEMP folder path of another user after "impersonate" RRS feed

  • Question

  • How to get the TEMP folder path of another user account?

    I have user accounts "Foo" and "Bar", and now I logged in as "Foo"

    I'm trying to do the following to get the TEMP folder path of user "Bar":

    HANDLE h = 0;
    LogonUser(L"Bar", L".", L"password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &h);
    ImpersonateLoggedOnUser(h);

    GetTempPath(...)

    However the returned TEMP path is still for user "Foo".

    Then I tried this way:
    get path string "%USERPROFILE%\AppData\Local\Temp" from registry "HKEY_CURRENT_USER\Environment", pass that to "ExpandEnvironmentStrings"... oops...it still gives me "Foo" user TEMP folder.

    This actually also happens for service account, without impersonation. When I call "GetTempPath" in the service process directly (no "impersonate"), the result is the TEMP folder for the current logged-on user, not for the service account.

    Does any of you who is familiar with win32 api know how to solve this: To get the TEMP folder of another user account?

    Thanks.

    Tuesday, October 21, 2008 5:22 PM

Answers

  • I imagine that GetTempPath() would use the %TEMP% environment setting.  I seriously doubt that Logonuser/Impersonate changes the environment, that's not how it works.  You'll have to pick another one, something like AppData perhaps.
    Hans Passant.
    • Marked as answer by jack 321 Monday, October 27, 2008 2:42 AM
    Wednesday, October 22, 2008 12:49 AM

All replies

  • I imagine that GetTempPath() would use the %TEMP% environment setting.  I seriously doubt that Logonuser/Impersonate changes the environment, that's not how it works.  You'll have to pick another one, something like AppData perhaps.
    Hans Passant.
    • Marked as answer by jack 321 Monday, October 27, 2008 2:42 AM
    Wednesday, October 22, 2008 12:49 AM
  • Actually, it does. For example, in C# when I use Path.GetTempPath() under impersonated account I get a path different from the call to the function before impersonation.
    Thursday, September 12, 2013 4:32 PM