GetTempPath在不同的过程中返回不同的路径 [英] GetTempPath returns different path in different process

查看:68
本文介绍了GetTempPath在不同的过程中返回不同的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetTempPath在Windows 10上的应用程序A中返回 C:\ Users \ sam \ AppData \ Local \ Temp \ .

GetTempPath returns C:\Users\sam\AppData\Local\Temp\ in my application A on Windows 10.

但是它在同一台计算机上的另一个应用程序B(挂接到另一个应用程序中的dll打印GetTempPath的值)中返回 C:\ Users \ sam \ AppData \ Local \ Temp \ 2 \ .我猜应用程序B会更改临时路径.

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.

boost文件系统的行为相同.

The boost filesystem behaves the same.

auto tempDirPath = boost::filesystem::temp_directory_path();
return tempDirPath.native();

这是怎么发生的?我如何获得完全相同的临时路径 C:\ Users \ sam \ AppData \ Local \ Temp \ ?

How does this happen? How could I get exactly the same temp path C:\Users\sam\AppData\Local\Temp\?

推荐答案

GetTempPath API确定记录了为临时文件指定的目录的路径:

The way the GetTempPath API determines the path of the directory designated for temporary files is documented:

GetTempPath 函数按以下顺序检查是否存在环境变量,并使用找到的第一个路径:

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  1. TMP环境变量指定的路径.
  2. TEMP环境变量指定的路径.
  3. 由USERPROFILE环境变量指定的路径.
  4. Windows目录.

如果2个从不同进程对该API的调用返回不同的结果,则这些进程具有不同的环境.默认情况下,进程从其父进程继承环境,但是CreateProcess 允许您显式指定环境块.同样,可以使用 SetEnvironmentVariable 来更改环境调用过程中的变量.

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.

您可以使用 Process Explorer 之类的工具进行检查任何给定进程的环境.

You can use tools like Process Explorer to inspect the environment of any given process.

这篇关于GetTempPath在不同的过程中返回不同的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆