C++设置和获取环境变量

来源:互联网 发布:matable软件 编辑:程序博客网 时间:2024/04/29 17:35
设置环境变量:使用函数getenv_putenv,使用的环境变量以PYTHONHOME作为例子,编译环境为VC10.0


#include <stdlib.h>int main(int argc, char **argv){ const char* python_home = getenv("PYTHONHOME"); char new_python_home[256]; new_python_home[0] = 0; if(python_home) {#ifdef _DEBUG #ifdef _WIN64  sprintf(new_python_home, "PYTHONHOME=%s\\win64\\debug", python_home);  //printf("win64 debug\n"); #elif defined(_WIN32)  sprintf(new_python_home, "PYTHONHOME=%s\\win32\\debug", python_home);  //printf("win32 debug\n"); #endif#else #ifdef _WIN64  sprintf(new_python_home, "PYTHONHOME=%s\\win64\\release", python_home);  //printf("win32 release\n"); #elif defined(_WIN32)  sprintf(new_python_home, "PYTHONHOME=%s\\win32\\release", python_home);  //printf("win32 release\n"); #endif#endif if(new_python_home[0])  _putenv(new_python_home); } printf("%s\n", getenv("PYTHONHOME"));}