Updating a system environment variable from C ++

I am currently writing an unmanaged C ++ program that works with a system environment variable. I get the value using GetEnvironmentVariable (...). Now I have a C # program that can change this variable at any time, for example. eg:

Environment.SetEnvironmentVariable("CalledPath", System.Windows.Forms.Application.ExecutablePath, EnvironmentVariableTarget.Machine);

The problem is that the C ++ program does not update this variable (or its environment block as a whole) automatically, so I still work with the old value unless I restart the program, which is not very good. Is there a way to update the environment block or, preferably, another way to read system environment variables?

Thanks in advance, Russo

+3
source share
6

, , , , . , GetEnvironmentVariable, , .

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

RegOpenKeyEx (...) RegQueryValueEx (...), .

+2

, - - .

, . , (C ++) - . / getenv() _putenv(), . , , , , .

, , . , Windows (, WM_COPYDATA), , , , .. , 't .

+4

. . , .

+1

, , , . . MSDN EnvironmentVariableTarget.

EnvironmentVariableTarget.Machine, , , , :

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment Windows .

, , . - , .

, . , . [ .]

0

putenv() - C-runtime ( MS _putenv() _putenv_s() - ++ .

getenv() .

-1

, _putenv() Windows.

-1

Source: https://habr.com/ru/post/1733017/


All Articles