1

I want to access the system TEMP variable in my C++ code. When I used getenv("TEMP") it gives the values of TEMP in user variable ("C:\Users\xxxx\AppData\Local\Temp\") and not system variable.

Similarly GetTempPath also returns the C:\Users\xxxx\AppData\Local\Temp\ where as I want to have the path of the TEMP variable from system (C:\Windows\Temp).

How to get it?

user6771624
  • 55
  • 2
  • 8
  • 1
    Please clarify your expectation of the `TEMP` variable; what should it contain? – Thomas Matthews Nov 09 '20 at 20:08
  • Very difficult to know what your expectation is. My guess is that you have some code that executes in a particular context, perhaps a service, and you want to get the temp directory that this code works with. What is this code, and what is the execution context? – David Heffernan Nov 09 '20 at 20:20
  • Sorry I dont under why it is difficult. I have an thrid party exe which writes some files to the path configured to TEMP system variable (not user varaible). I want to check whether the file is present or not. So I have to get the path of TEMP system variable. – user6771624 Nov 09 '20 at 21:23
  • @user6771624 - open token of this exe process and use `CreateEnvironmentBlock`. difficult because you poorly formulated the question - unclear – RbMm Nov 09 '20 at 21:57
  • I dont know whats unclear in this? Question is simple? How do you get the value of system variable TEMP – user6771624 Nov 09 '20 at 23:09
  • @user6771624 - question is not clear. what is "system" variable ? and i already 2 time give you answer - you need token of process for which you want get *tmp* – RbMm Nov 10 '20 at 14:27
  • @RbMm, There are 2 environment variables TEMP one in user variable section and other as system variable section. I want to get the value of TEMP environment variable which is in under system variable. Hope this is clear. I dont understand your answer on opening the token exe of exe. – user6771624 Nov 10 '20 at 15:58
  • @RbMm, you are saying the question is not clear and you gave 2 time answer? – user6771624 Nov 10 '20 at 15:59
  • @user6771624 - you say about some process *which writes some files to the path configured to TEMP system variable* - this process probably use value of *TMP* variable from self environment block. so question - you can identify this process ? by name o somehow else ? if yes - you need **open** this process, and then or direct read environment block from this process,or open process token (what unclear here ?) and use this token in call `CreateEnvironmentBlock`. this is most correct answer which i and give you. not what you mean under "system" variables. question - process is known ? – RbMm Nov 10 '20 at 18:54
  • @Rbmm, Ok I got you now.But the process is already expired and I dont have a handle to that process to open its environment block. Thanks for the detailed reply.The only thing I know is that the process based on a particular condition, it will write a file in the path configured to the TEMP system environment variable. I need to check if the file present in that path take action A, otherwise action B. – user6771624 Nov 10 '20 at 18:58
  • *configured to the TEMP system environment* - this is your mistake. it not configured do this. it simply query *TMP* variable direct or indirect via *GetTempPath*. more correct say that process run under *LocalSystem* token and have the same environment as any process with this token – RbMm Nov 10 '20 at 19:00

3 Answers3

1

There is no Win32 API to read (or edit) system environment variables. The reference documentation for Environment Variables points to the registry key where the system environment is stored for manipulating system variables.

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment".

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • This is programmatically set or modify. I dont see anything to query. I checked SystemParametersInfo as well. No flag to query the environment – user6771624 Nov 09 '20 at 20:43
  • 1
    There is no API to query the *system* environment. You would have to use standard Registry APIs (`RegOpenKeyEx()`, `RegGetValue()`/`RegQueryValueEx()`) to query values from the documented key. – Remy Lebeau Nov 09 '20 at 20:52
  • @user6771624 You just read the value of TEMP with the usual registry functions, see for example [How to read a value from the Windows registry](https://stackoverflow.com/a/35717). Though reading through the followups on the question I am no longer clear if you actually mean the system TEMP variable vs. the TEMP variable in a particular running process, which is not necessarily the same. – dxiv Nov 09 '20 at 23:04
1

The %USERPROFILE%\AppData\Local\Temp\ path IS the correct TEMP folder you should be using in most situations. That is the folder that APIs like getenv("%TEMP%"|"%TMP%") and GetTempPath() return on a per-user basis, by design. You should not be using the C:\Windows\Temp\ folder at all, as that is a system folder meant for Windows' internal use.

That being said, if you really want to get the system %TEMP% path, then you can either:

  • obtain the path of the Windows installation folder by using getenv("%WINDIR%"|"%SYSTEMROOT%"), GetWindowsDirectory(), SHGetFolderPath(CSIDL_WINDOWS), or SHGetKnownFolderPath(FOLDERID_Windows), and then append Temp to the end of that path.

  • read the "TEMP" or "TMP" value in the HKLM\System\CurrentControlSet\Control\Session Manager\Environment Registry key.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks. Yes Im not using it for writing. I need to query the path of TEMP of system variable to look for a file. The other route is kind of work around only i mean getting the GetWindowsDirectory and appending temp to it. Because if for some reason it is being edited to some other path ( not set to C:\Windows\Temp ) , then its wrong – user6771624 Nov 09 '20 at 20:52
  • 2
    @user6771624 Can you explain what you mean by "look for a variable"? Directories contain files, not variables. What variable are you looking for in the TMEP directory? – Raymond Chen Nov 09 '20 at 20:57
  • @RaymondChen the OP didn't say "look for a variable", they said "look for a file" in the folder specified by the system `TEMP` variable. – Remy Lebeau Nov 09 '20 at 20:59
  • Sorry it was a typo. Im not looking for a variable. Im looking for a file. – user6771624 Nov 09 '20 at 21:03
  • @user6771624 what is value of *tmp* depend from token. if you know process for which you look *tmp* - you can open it token, and call `CreateEnvironmentBlock`. but what sense of it ? – RbMm Nov 09 '20 at 21:07
  • 1
    Better would be to have the code that creates the file tell you where it is, rather than making you go searching for it. The code that creates the file can also set up the ACLs so that you have access to it once you get it. – Raymond Chen Nov 09 '20 at 21:12
  • The code which creates the file simply tells me that the file will be written to path configured to the TEMP of system variable. – user6771624 Nov 09 '20 at 21:24
  • @user6771624 it doesn't give you the actual full path to the file? – Remy Lebeau Nov 09 '20 at 22:12
  • No it did not give – user6771624 Nov 09 '20 at 23:08
  • Ok the only I found which is working is as @RemyLebeau mentioned, read the registry key value – user6771624 Nov 10 '20 at 18:22
0

This: GetEnvironmentVariable(String, EnvironmentVariableTarget) states that you can get it for Machine:

The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment key in the Windows operating system registry. This value should be used on .NET implementations running on Windows systems only.

This leads me to believe that there IS a Win32 API to get it...

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • 2
    If you look at the source code, you'll see that [when asked for Machine target, the code just reads the registry directly, no special API](https://github.com/microsoft/referencesource/blob/f461f1986ca4027720656a0c77bede9963e20b7e/mscorlib/system/environment.cs#L868). – Raymond Chen Nov 09 '20 at 20:36
  • @RaymondChen - great! Thank you very much! I'm bookmarking it :) – Vlad Feinstein Nov 09 '20 at 22:55