GetTempPath returns C:\Users\sam\AppData\Local\Temp\
in my application A on Windows 10.
But it returns C:\Users\sam\AppData\Local\Temp\2\
in another application B (a dll hooked in another application prints value of GetTempPath) on same computer. I guess the application B change the temp path.
The boost filesystem behaves the same.
auto tempDirPath = boost::filesystem::temp_directory_path();
return tempDirPath.native();
How does this happen? How could I get exactly the same temp path C:\Users\sam\AppData\Local\Temp\
?
The way the GetTempPath API determines the path of the directory designated for temporary files is documented:
The GetTempPath
function checks for the existence of environment variables in the following order and uses the first path found:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable.
- The path specified by the USERPROFILE environment variable.
- The Windows directory.
If 2 calls to this API from different processes return different results, then those processes have different environments. By default, a process inherits the environment from its parent process, but CreateProcess allows you to explicitly specify an environment block. Likewise, SetEnvironmentVariable can be used to change an environment variable in the calling process.
You can use tools like Process Explorer to inspect the environment of any given process.