Do environment variables occur after impersonating and loading a user profile in a service?

I have a Windows service running under the LocalSystem account, and I'm trying to access some environment variables for a specific user. When I call ExpandEnvironmentStrings ("% AppData%"), I get "C: \ windows \ system32 \ config \ systemprofile \ AppData \ Roaming".

I realized that impersonating and loading a user profile should solve this problem, so I called:

  • LogonUser ()
  • LoadUserProfile ()
  • CreateEnvironmentBlock ()
  • ImpersonateLoggedOnUser ()

Still, ExpandEnvironmentStrings ("% AppData%") returns the system folder instead of "C: \ Users \ Username \ AppData \ Roaming".

So, I searched something else and came across SHGetFolderPath (CSIDL_LOCAL_APPDATA), which worked as an impersonation after a spell.

My question is not what I should do (e.g. SHGetFolderPath), but more about how environment variables work in services. I think my problem is:

  • My LoadUserProfile () code is still missing, although everything returns to success. My ignorant side wants to convince me that theoretically loading a user profile should make ExpandEnvironmentStrings () return the loaded user values.

  • Based on some indications, the environment variables in the services are read only once ( http://support.microsoft.com/kb/887693 ) at system startup, so my service ONLY knows the system environment variables.

2. , - , - .

+5
1

, # 2 . , :

, .

MSDN ExpandEnvironmentStringForUser(), .

+7

All Articles