Alex Rivera | Logout

Get all env variables in C\C++ on Windows

Asked 2012-03-02T14:42:06.477
10

The signature for the main function in C\C++ can include 3 arguments:

main( int argc, char *argv[ ], char *envp[ ] )

The third is the environment variables.

I'm compiling a library under VS10 and therefor I have no main(). How can I get the environment variables in exactly the same type as is in char *envp[]? I rather not use .NET as to decrease dependencies and perhaps one day be open to portability.

Edit
Report

1 Answer

4

I don't know about windows, but on Linux this variable:

extern char **environ;

is exactly what you are looking for.

#include <stdio.h>
#include <assert.h>

extern char **environ;

int main (int ac, char **av, char **envp) {

  assert(envp == environ);

}
answered 2012-03-02T14:55:34.040

Your Answer