0

In the Windows System Properties | Environment Variables, there is a variable "AppStatus=status1".

There is an exe named MyApp.exe. In the implementation it changes the variable with the code.

int ret = putenv("AppStatus=status2"); // Change the environment variable.

If use API char * pStatus = getenv("AppStatus");, the returned value is "status2".

What I want to get is the original value defined in the OS ("AppStatus=status1") not in the process block. To implement this I can query the registry key

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

But it should be better if there is an API that supports it. Is anybody aware of it?

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
Jeffrey
  • 4,436
  • 9
  • 38
  • 54
  • You should tag your question with WinAPI (or Windows), and the answer is operating system specific (and actually, your question probably has no sense on Linux). Your question is more Windows related than C++ related. – Basile Starynkevitch Dec 30 '11 at 08:30
  • Can you change the implementation? Why not just store the value of `AppStatus` at the process start separately? – vstm Dec 30 '11 at 08:32
  • Interesting info [here](http://stackoverflow.com/questions/631664/accessing-environment-variables-in-c) (not dup, but pointers to Win32 functions and differences between `getenv` and the native Win32 equivalent, and a .Net function that does what you want if you were using .Net) – Mat Dec 30 '11 at 08:53

1 Answers1

0

Just call GetEnvironmentVariable(). It works on the process state as maintained by Windows, not the CRT state modified by putenv().

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I tried this API. It returns the same value as getenv. It is "status2". From msdn here http://msdn.microsoft.com/en-us/library/ms683188%28VS.85%29.aspx. It retrieves the contents of the specified variable from the environment block of the calling process. – Jeffrey Dec 30 '11 at 10:15
  • You're linking with /MD by any chance? It seems CRT-dependent. – MSalters Dec 30 '11 at 10:20