VC ++不使用GetEnvironmentVariable检测新创建的env变量

时间:2021-08-10 11:37:01

I'm using the Win32 function GetEnvironmentVariable to retrieve the value of a variable that I just created. I'm running Windows XP and VC++ 2005. If I run the program from within Visual Studio, it can't find the new variable. If I run it from a command-prompt, it does. I restarted VC++ but same result. I even restarted all instances of Visual Studio but still the same problem. It might get resolved if I reboot the PC but I'm curious why this is so. Here's the code that I'm using:

我正在使用Win32函数GetEnvironmentVariable来检索我刚刚创建的变量的值。我正在运行Windows XP和VC ++ 2005.如果我从Visual Studio中运行程序,它找不到新的变量。如果我从命令提示符运行它,它会。我重新启动VC ++但结果相同。我甚至重新启动了Visual Studio的所有实例,但仍然是同样的问题。如果我重新启动电脑可能会得到解决,但我很好奇为什么会这样。这是我正在使用的代码:

#define BUFSIZE 4096
#define VARNAME TEXT("MY_ENV_NAME")

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR chNewEnv[BUFSIZE];

    DWORD dwEnv = ::GetEnvironmentVariable(VARNAME, chNewEnv, BUFSIZE);
    if (dwEnv == 0)
    {
        DWORD dwErr = GetLastError();
        if(dwErr == ERROR_ENVVAR_NOT_FOUND)
        {
            printf("Environment variable does not exist.\n");
            return -1;
        }
    }
    else
    {
        printf(chNewEnv);
    }

    return 0;
}

If I replace MY_ENV_NAME with something that must exist, such as TEMP, it works as expected. Any ideas? Thanks.

如果我用必须存在的内容替换MY_ENV_NAME,例如TEMP,它会按预期工作。有任何想法吗?谢谢。

4 个解决方案

#1


Expanding on what Anders and Martin said, environment variables are one thing that is inherited when starting an application. The new program basically gets a copy of the environment at the time it was started. When debugging, your exe is generally started by Visual Studio, so your application will have the same environment as Visual Studio. Visual Studio, is generally started by explorer. If you change the environment variables by going to System Properties->Advanced->Envronment Variables then you will have to restart Visual Studio to see the changes.

扩展Anders和Martin所说的,环境变量是启动应用程序时继承的一件事。新程序在启动时基本上获得了环境的副本。调试时,您的exe通常由Visual Studio启动,因此您的应用程序将具有与Visual Studio相同的环境。 Visual Studio,通常由资源管理器启动。如果通过转到“系统属性” - >“高级” - >“环境变量”来更改环境变量,则必须重新启动Visual Studio才能查看更改。

If you need to see the envronment variables that Visual Studio is seeing you can (at least for VS2005/2008) go to Tools...->options...->Projects and Solutions->VC++ Project Settings and set Show Environment in Log to 'Yes.' This will print out all the environment variables to the build log (ctrl+click on the link in your build output). You have to build to get this info, but this is the best way I know of seeing the VS environment.

如果您需要查看Visual Studio看到的环境变量(至少对于VS2005 / 2008),请转到工具...->选项...->项目和解决方案 - > VC ++项目设置并设置显示环境登录'是'。这将打印出所有环境变量到构建日志(ctrl +单击构建输出中的链接)。你必须构建以获取此信息,但这是我知道看到VS环境的最佳方式。

If you really need to change environment variables then run and are having a hard time debugging, you can build your debug exe and have a call to DebugBreak() somewhere near where you want to start debugging from. Then you can set your environment how you want, start the exe from explorer or the command prompt. Then (assuming you have JIT debugging enabled) you will get a popup when your code gets to the DebugBreak() call and you can attach to it with VS and debug normally from there.

如果你真的需要更改环境变量然后运行并且很难调试,你可以构建你的调试exe并在你想要开始调试的地方调用DebugBreak()。然后,您可以根据需要设置环境,从资源管理器或命令提示符启动exe。然后(假设您启用了JIT调试),当您的代码进入DebugBreak()调用时,您将获得一个弹出窗口,您可以使用VS附加到它并从那里正常调试。

#2


Make sure you restart the application before you can read the environment variable. The same happens if you have a console window open and change the environment variables on My Computer, these are not noted in any existing console windows. You need to restart them to get a copy of the new environment variables.

确保在读取环境变量之前重新启动应用程序。如果打开控制台窗口并更改“我的电脑”上的环境变量,则会发生同样的情况,这些在任何现有控制台窗口中都没有记录。您需要重新启动它们才能获得新环境变量的副本。

#3


It all depends on how you set the environment variable:

这一切都取决于你如何设置环境变量:

  • If you ran set MY_ENV_NAME=val in a command prompt, then you have set MY_ENV_NAME for that instance of cmd.exe and any child processes it executes in the future. The environment of existing child processes is not modified.

    如果您在命令提示符下运行了设置MY_ENV_NAME = val,那么您已为该cmd.exe实例设置了MY_ENV_NAME以及将来执行的所有子进程。不修改现有子进程的环境。

    In this case, exiting the Visual Studio IDE and starting it from the command line (not Explorer) should cause it and its child processes to see the new environment variable.

    在这种情况下,退出Visual Studio IDE并从命令行(而不是资源管理器)启动它应该使它及其子进程看到新的环境变量。

  • If you used the System or Users control panel or the setx command to set MY_ENV_NAME, then you have set MY_ENV_NAME persistently, and it will be set for all processes after you reboot your computer. In addition, you may find that some processes, such as Explorer, pick up the environment variable change immediately.

    如果您使用“系统”或“用户”控制面板或setx命令设置MY_ENV_NAME,则表明您已经永久设置了MY_ENV_NAME,并且在重新启动计算机后将为所有进程设置它。此外,您可能会发现某些进程(如Explorer)会立即获取环境变量。

    This works by storing the new environment variable in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment or HKEY_CURRENT_USER\Environment, depending on whether you chose to set a system environment variable or a per-user environment variable. Existing processes are notified that there was an environment variable change by broadcasting the WM_SETTINGCHANGE message with lParam=="Environment". This message causes them to re-read the persistent environment variables from the registry if they know how. KB104011 has more details.

    这通过将新环境变量存储在HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment或HKEY_CURRENT_USER \ Environment下的注册表中,具体取决于您是选择设置系统环境变量还是每用户环境变量。通过使用lParam ==“Environment”广播WM_SETTINGCHANGE消息,通知现有进程存在环境变量更改。此消息使他们如果知道如何从注册表重新读取持久性环境变量。 KB104011有更多细节。

    As a result, if you use the System or Users control panel to set a new environment variable, exiting the Visual Studio IDE and starting it again from Explorer (not a command prompt) should cause it and its child processes to see the new environment variable.

    因此,如果使用“系统”或“用户”控制面板设置新的环境变量,退出Visual Studio IDE并从资源管理器(而不是命令提示符)再次启动它应该使它及其子进程看到新的环境变量。

#4


Thanks for all the responses. As I mentioned in my question, I tried restarting everything, short of rebooting the PC. It turns out that because my environment variable was a SYSTEM variable, VS doesn't recognize it without rebooting the PC. When I moved the env variable from SYSTEM to USER and restarted VS, it worked fine.

感谢所有的回复。正如我在我的问题中提到的,我尝试重新启动所有内容,而不是重新启动PC。事实证明,因为我的环境变量是一个SYSTEM变量,所以如果不重新启动PC,VS就无法识别它。当我将env变量从SYSTEM移动到USER并重新启动VS时,它工作正常。

#1


Expanding on what Anders and Martin said, environment variables are one thing that is inherited when starting an application. The new program basically gets a copy of the environment at the time it was started. When debugging, your exe is generally started by Visual Studio, so your application will have the same environment as Visual Studio. Visual Studio, is generally started by explorer. If you change the environment variables by going to System Properties->Advanced->Envronment Variables then you will have to restart Visual Studio to see the changes.

扩展Anders和Martin所说的,环境变量是启动应用程序时继承的一件事。新程序在启动时基本上获得了环境的副本。调试时,您的exe通常由Visual Studio启动,因此您的应用程序将具有与Visual Studio相同的环境。 Visual Studio,通常由资源管理器启动。如果通过转到“系统属性” - >“高级” - >“环境变量”来更改环境变量,则必须重新启动Visual Studio才能查看更改。

If you need to see the envronment variables that Visual Studio is seeing you can (at least for VS2005/2008) go to Tools...->options...->Projects and Solutions->VC++ Project Settings and set Show Environment in Log to 'Yes.' This will print out all the environment variables to the build log (ctrl+click on the link in your build output). You have to build to get this info, but this is the best way I know of seeing the VS environment.

如果您需要查看Visual Studio看到的环境变量(至少对于VS2005 / 2008),请转到工具...->选项...->项目和解决方案 - > VC ++项目设置并设置显示环境登录'是'。这将打印出所有环境变量到构建日志(ctrl +单击构建输出中的链接)。你必须构建以获取此信息,但这是我知道看到VS环境的最佳方式。

If you really need to change environment variables then run and are having a hard time debugging, you can build your debug exe and have a call to DebugBreak() somewhere near where you want to start debugging from. Then you can set your environment how you want, start the exe from explorer or the command prompt. Then (assuming you have JIT debugging enabled) you will get a popup when your code gets to the DebugBreak() call and you can attach to it with VS and debug normally from there.

如果你真的需要更改环境变量然后运行并且很难调试,你可以构建你的调试exe并在你想要开始调试的地方调用DebugBreak()。然后,您可以根据需要设置环境,从资源管理器或命令提示符启动exe。然后(假设您启用了JIT调试),当您的代码进入DebugBreak()调用时,您将获得一个弹出窗口,您可以使用VS附加到它并从那里正常调试。

#2


Make sure you restart the application before you can read the environment variable. The same happens if you have a console window open and change the environment variables on My Computer, these are not noted in any existing console windows. You need to restart them to get a copy of the new environment variables.

确保在读取环境变量之前重新启动应用程序。如果打开控制台窗口并更改“我的电脑”上的环境变量,则会发生同样的情况,这些在任何现有控制台窗口中都没有记录。您需要重新启动它们才能获得新环境变量的副本。

#3


It all depends on how you set the environment variable:

这一切都取决于你如何设置环境变量:

  • If you ran set MY_ENV_NAME=val in a command prompt, then you have set MY_ENV_NAME for that instance of cmd.exe and any child processes it executes in the future. The environment of existing child processes is not modified.

    如果您在命令提示符下运行了设置MY_ENV_NAME = val,那么您已为该cmd.exe实例设置了MY_ENV_NAME以及将来执行的所有子进程。不修改现有子进程的环境。

    In this case, exiting the Visual Studio IDE and starting it from the command line (not Explorer) should cause it and its child processes to see the new environment variable.

    在这种情况下,退出Visual Studio IDE并从命令行(而不是资源管理器)启动它应该使它及其子进程看到新的环境变量。

  • If you used the System or Users control panel or the setx command to set MY_ENV_NAME, then you have set MY_ENV_NAME persistently, and it will be set for all processes after you reboot your computer. In addition, you may find that some processes, such as Explorer, pick up the environment variable change immediately.

    如果您使用“系统”或“用户”控制面板或setx命令设置MY_ENV_NAME,则表明您已经永久设置了MY_ENV_NAME,并且在重新启动计算机后将为所有进程设置它。此外,您可能会发现某些进程(如Explorer)会立即获取环境变量。

    This works by storing the new environment variable in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment or HKEY_CURRENT_USER\Environment, depending on whether you chose to set a system environment variable or a per-user environment variable. Existing processes are notified that there was an environment variable change by broadcasting the WM_SETTINGCHANGE message with lParam=="Environment". This message causes them to re-read the persistent environment variables from the registry if they know how. KB104011 has more details.

    这通过将新环境变量存储在HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment或HKEY_CURRENT_USER \ Environment下的注册表中,具体取决于您是选择设置系统环境变量还是每用户环境变量。通过使用lParam ==“Environment”广播WM_SETTINGCHANGE消息,通知现有进程存在环境变量更改。此消息使他们如果知道如何从注册表重新读取持久性环境变量。 KB104011有更多细节。

    As a result, if you use the System or Users control panel to set a new environment variable, exiting the Visual Studio IDE and starting it again from Explorer (not a command prompt) should cause it and its child processes to see the new environment variable.

    因此,如果使用“系统”或“用户”控制面板设置新的环境变量,退出Visual Studio IDE并从资源管理器(而不是命令提示符)再次启动它应该使它及其子进程看到新的环境变量。

#4


Thanks for all the responses. As I mentioned in my question, I tried restarting everything, short of rebooting the PC. It turns out that because my environment variable was a SYSTEM variable, VS doesn't recognize it without rebooting the PC. When I moved the env variable from SYSTEM to USER and restarted VS, it worked fine.

感谢所有的回复。正如我在我的问题中提到的,我尝试重新启动所有内容,而不是重新启动PC。事实证明,因为我的环境变量是一个SYSTEM变量,所以如果不重新启动PC,VS就无法识别它。当我将env变量从SYSTEM移动到USER并重新启动VS时,它工作正常。