1

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\?

xMRi
  • 14,982
  • 3
  • 26
  • 59
user1633272
  • 2,007
  • 5
  • 25
  • 48

1 Answers1

2

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:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. 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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • You are right. Do you know a way to get the one in environment of system settings (which the application can not affect)? – user1633272 Jun 13 '18 at 09:47
  • @user1633272: I'm not aware of an API, that allows you to inspect the system-global environment settings. You'd probably have to parse those out of the registry, although this is starting to sound like an [XY Problem](http://xyproblem.info). What are you really trying to accomplish? Why does it matter, what value you get from `GetTempPath`? – IInspectable Jun 13 '18 at 10:07
  • A injected dll to target application and my application need to negotiate a path to export file/read file. Currently I'm using temp path to construct the file path, but the temp path is changed by the target application on one of my clients computer. – user1633272 Jun 13 '18 at 10:11
  • 1
    @user1633272: If those processes need to agree on a location, they should communicate that location with each other (pipes, shared memory, etc.). Relying on state outside your control isn't going to work reliably. – IInspectable Jun 13 '18 at 10:14
  • My initial idea is using a filename constructed by process id under temp path. A communication mechanism should work, just looks overkill. I will try to read the value from registry or construct it by user name. – user1633272 Jun 13 '18 at 10:21
  • https://stackoverflow.com/questions/573817/where-are-environment-variables-stored-in-registry – user1633272 Jun 13 '18 at 10:33