When what you set is not what you get : SetEnvironmentVariable and getenv

Mixing SetEnvironmentVariable and getenv is asking for trouble, as we recently found to our dismay (and exasperation!). It took some serious debugging to figure out the actual problem – let’s see if you can figure it out. Here’s some C++/CLI code that uses getenv to read the value of an environment variable and print it to the console. 1: public ref class EnvironmentVariablePrinter 2: { 3: public: 4: void PrintEnv(String ^key) 5: { 6: IntPtr ptr = Marshal::StringToHGlobalAnsi(key); 7: const char *ckey = (const char *)ptr.ToPointer(); 8: const char *cval = getenv(ckey); 9: string val = cval == NULL ? … Continue reading When what you set is not what you get : SetEnvironmentVariable and getenv