6
4
Occasionally, I find my TEMP and TMP environment variables set to C:\Windows\TEMP
. They should be set to %USERPROFILE%\AppData\Local\Temp
, and are configured correctly in System Properties.
This manifests itself as error messages like the following:
---> System.InvalidOperationException: Unable to generate a temporary class
(result=1).
error CS2001: Source file 'C:\Windows\TEMP\gb_pz65v.0.cs' could not be found
error CS2008: No inputs specified
...which occurs in various .NET applications (in particular Visual Studio 2010 or SQL Server Management Studio). Alternatively, SQL Server Management Studio will report:
Value cannot be null.
Parameter name: viewInfo (Microsoft.SqlServer.Management.SqlStudio.Explorer)
If I run PowerShell elevated, then $env:TEMP
is set correctly. If I run PowerShell non-elevated, then it's not. I believe that it should be set correctly in both cases. If not, it's the wrong way round.
The same is true for CMD.EXE.
Rebooting fixes it, temporarily, until something breaks it again. Presumably something loaded into Explorer.exe is messing with its environment variables, but what?
The values in the registry are correct, even while this is happening:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
hasTEMP
=%SYSTEMROOT%\Temp
HKCU\Environment
hasTEMP
=%USERPROFILE%\AppData\Local\Temp
By setting a breakpoint on shell32!RegenerateUserEnvironment
with WinDbg, I'm able to trap it when it happens, but I still don't know why explorer.exe
is reading the wrong environment variables.
I can reproduce it consistently by broadcasting a WM_SETTINGCHANGE
message (I wrote a one-line C++ program to do this). Watching the activity in Process Monitor shows that explorer.exe doesn't even look at HKCU\Environment
.
What is going on?
what did you use to set a breakpoint on
shell32!RegenerateUserEnvironment
? whatever it is, it sounds like it might be an even more powerful tool than sysinternals process monitor. – barlop – 2016-02-05T08:58:36.433@barlop, I used WinDbg. – Roger Lipscombe – 2016-02-05T10:30:15.307
You're right, that IS backwards - running code as "SYSTEM" or elevated without impersonation, should cause the C:\windows\temp thing. If you look at the Environment variables in 'advanced' computer properties, you should be able to see the SYSTEM variables using c:\windows\temp and the USER variables using %appdata%\local\temp, etc. I wish I knew what to tell you, maybe create a new user on the machine that is NOT an admin (and thus can't elevate) and play around with that. – Mark Allen – 2012-04-19T19:06:42.107
If explorer.exe is having its environment variables messed with, that'd explain it. Elevation bounces via a different process (WinInit, I think), so it will get different environment variables. I think. This would explain why the non-elevated processes are broken and the elevated ones are OK. Maybe. – Roger Lipscombe – 2012-04-20T10:08:10.540