How to Read a User/System Environment Variable Using c++
-
How to Read a User/System Environment Variable Using c++. getenv is not working.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
How to Read a User/System Environment Variable Using c++. getenv is not working.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
for Visual C++ try this otherwise getenv will work using namespace System; using namespace System::Collections; int main() { Console::WriteLine(); Console::WriteLine( "GetEnvironmentVariables: " ); IDictionary^ environmentVariables = Environment::GetEnvironmentVariables(); IEnumerator^ myEnum = environmentVariables->GetEnumerator(); while ( myEnum->MoveNext() ) { DictionaryEntry^ de = safe_cast<dictionaryentry^>(myEnum->Current); Console::WriteLine( " {0} = {1}", de->Key, de->Value ); } }
-
How to Read a User/System Environment Variable Using c++. getenv is not working.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
One minute of googling turned up this code:
#include
#include
extern char**_environ;int main(int argc, char *argv[], char *envp[])
{
char **next = _environ;while (*next)
{
printf("%s\n", *next);
next++;
}
return 0;
}If you want to get the value of a specific variable you can use
GetEnvironmentVariable()
-
How to Read a User/System Environment Variable Using c++. getenv is not working.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
VKupunaram wrote:
from VS2005 onward
This API has been around since Windows NT 3.1 which was released in 1993. What makes you think that it is supported only from VS2005 onwards?
-
One minute of googling turned up this code:
#include
#include
extern char**_environ;int main(int argc, char *argv[], char *envp[])
{
char **next = _environ;while (*next)
{
printf("%s\n", *next);
next++;
}
return 0;
}If you want to get the value of a specific variable you can use
GetEnvironmentVariable()
I had created variable in user section , using the following code 'defining variables dim objShell, colUserEnvVars 'initializing all variables Set objShell = WScript.CreateObject("WScript.Shell") Set colUserEnvVars = objShell.Environment("System") 'Output File for registry information. colUserEnvVars("rahul") = "rahul" but i cannot read the same from c++, for this i have to restart the system, but Wscript.Echo colUserEnvVars("DeloitteAuditShare123") read the value without restarting ths system.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
for Visual C++ try this otherwise getenv will work using namespace System; using namespace System::Collections; int main() { Console::WriteLine(); Console::WriteLine( "GetEnvironmentVariables: " ); IDictionary^ environmentVariables = Environment::GetEnvironmentVariables(); IEnumerator^ myEnum = environmentVariables->GetEnumerator(); while ( myEnum->MoveNext() ) { DictionaryEntry^ de = safe_cast<dictionaryentry^>(myEnum->Current); Console::WriteLine( " {0} = {1}", de->Key, de->Value ); } }
That's not C++, it looks like
Managed C++
(which isn't really C++ but a different language based on it). This is the wrong forum for code like that.Steve