18

I use GetEnvironmentString() to get the program's environment variables.

Every program has such result in the first:

=::=::\

I don't know what does it mean?

Here is the code :

LPWCH lpEnvString=GetEnvironmentStringsW();
 LPWSTR lpszVariable=(LPWSTR)lpEnvString;
 while (*lpszVariable)
 {
     wprintf(L"%s\n",lpszVariable);
     lpszVariable+=wcslen(lpszVariable)+1;
 }
 FreeEnvironmentStringsW(lpEnvString);

Also if we start listing such variables we would see stuff like:

=::=::\
=C:=C:\Users\username\value
=ExitCode=00000001
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\artik\AppData\Roaming
CommonProgramFiles=C:\Program Files (x86)\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
...

On the other hand, getenv("=ExitCode") or getenv("=C:") returns NULL.

Can you provide a proper documentation of this "feature", for example getenv() ignores such strings and how such values should be treated?

Artyom
  • 31,019
  • 21
  • 127
  • 215
unixstudio
  • 189
  • 1
  • 4
  • 2
    @Nick All you need to do is call `GetEnvironmentStrings()` – David Heffernan May 03 '12 at 12:57
  • I guess I was wanting to check what he was doing with the return value in order to get the string provided. Also - you're assuming I have a windows PC :) – Nick May 03 '12 at 13:12
  • 23
    [What are these strange =C: environment variables?](http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx) – Raymond Chen May 03 '12 at 13:49
  • 8
    @RaymondChen: You should post this as an answer. – Tim Pietzcker May 03 '12 at 13:58
  • @TimPietzcker It's not an answer. It's a link to an answer. If the OP thinks it's an answer, they can answer their own question and accept it. – Raymond Chen May 03 '12 at 13:59
  • 1
    It's half an answer. I knew that the environment variable `=X:` is the current directory for `X:`, but what is `=::`? – arx May 03 '12 at 23:04
  • @RaymondChen what exactly do you mean by "it's a bug"? That's what the output should look like. A quick google search of getEnvironmentStrings() turned up this example with the same output: http://spottedtiger.tripod.com/D_Language/D_Win32_GetEnvironmentStrings.html – Brad Jun 05 '12 at 20:23
  • 1
    It's a bug in Windows that it created the bogus environment variable in the first place. – Raymond Chen Jun 05 '12 at 23:17
  • 2
    [What are these strange =C: environment variables?](https://devblogs.microsoft.com/oldnewthing/20100506-00/?p=14133) (Microsoft just loves breaking hyperlinks...) – IInspectable Nov 30 '22 at 20:03

1 Answers1

13

They are leftovers from cmd.exe emulating ms-dos directory handling, they basically have little use, and are more archaic than anything. Essentially, it keeps track of a per drive current directory, and is kept as an environment variable to pass to other processes with ease.

Ryan
  • 2,755
  • 16
  • 30
  • The question is more for documentation of how for example getenv should handle them etc. Pointers to docs required. – Artyom Jun 05 '12 at 11:04
  • 2
    @Artyom, I think the takeaway is that anything starting with `=` is private to the shell and intentionally undocumented, and should be ignored. – Mark Ransom Jun 05 '12 at 19:58
  • 3
    @MarkRansom the problem is that these strings appear in the GetEnvironmentStrings list and such a behavior seems to be undocumented on unclear about how to handle them – Artyom Jun 06 '12 at 18:02
  • 1
    @Artyom, is ignoring them not a sufficient way of handling them? The entire philosophy of environment strings is that you use the ones you care about, and ignore the rest. – Mark Ransom Jun 06 '12 at 19:30