#include <iostream>
#include "stdlib.h"
#pragma warning(disable:4996)
class Env
{
public:
//sample: char * libvar = getenv( "LIB" ); // C4996
//Return 0 if successful or -1 in the case of an error.
static int put(const char *envstring)
{
return _putenv(envstring);
}
static int put(const wchar_t *envstring)
{
return _wputenv(envstring);
}
//sample: _putenv( "LIB=c:\\mylib;c:\\yourlib" ); // C4996
//return value is NULL if varname is not found in the environment table
static char *get(const char *varname)
{
return getenv(varname);
}
static wchar_t *get(const wchar_t *varname)
{
return _wgetenv(varname);
}
static wchar_t ** table()
{
return _wenviron;//In a program that uses main, _wenviron is initially NULL
}
static char ** table_narrow()
{
return _environ;//in a program that uses wmain, _environ is initially NULL
}
};
using namespace std;
//简单测试
int wmain()
{
wchar_t * libvar = Env::get(L"path");
if(libvar)
wcout << libvar <<L"\n\n"<< endl;
//附加 C:\\Dir 到 %path% 末尾
wstring pathenv = wstring(L"path=") + wstring(libvar) + L";" + L"C:\\Dir;";
Env::put(pathenv.c_str());
//列出所有环境变量
wchar_t ** estrp = Env::table();
if (estrp)
{
while (*estrp)
{
wcout << *estrp << endl;
estrp++;
}
}
//char ** estrp2 = Env::table_narrow();
//if (estrp2)
//{
// while (*estrp2)
// {
// cout << *estrp2 << endl;
// estrp2++;
// }
//}
}
//int main()
//{
// std::cout << "Hello World!\n";
//
// wchar_t ** estrp = Env::table(); //return Null
// if (estrp)
// {
// while (*estrp)
// {
// std::wcout << *estrp << std::endl;
// estrp++;
// }
// }
//
//
// char ** estrp2 = Env::table_narrow();
// if (estrp2)
// {
// while (*estrp2)
// {
// std::cout << *estrp2 << std::endl;
// estrp2++;
// }
// }
//
//}