如何用代码设置环境变量?

BraveHeart2222 2004-06-21 12:52:48
我现正用VC开发一个项目,其中要设置几个环境变量;请问在VC中如何用代码设置环境变量?我想这应该不难,但我不知道,请高手们帮帮忙,谢谢!!!!(结贴马上给分)
...全文
2310 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2004-06-21
  • 打赏
  • 举报
回复
SUMMARY
You can modify user environment variables by editing the following Registry key:


HKEY_CURRENT_USER \
Environment
You can modify system environment variables by editing the following Registry key:

HKEY_LOCAL_MACHINE \
SYSTEM \
CurrentControlSet \
Control \
Session Manager \
Environment
Note that any environment variable that needs to be expanded (for example, when you use %SYSTEM%) must be stored in the registry as a REG_EXPAND_SZ registry value. Any values of type REG_SZ will not be expanded when read from the registry.

Note that RegEdit.exe does not have a way to add REG_EXPAND_SZ. Use RegEdt32.exe when editing these values manually.

However, note that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.

To effect these changes without having to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so that any interested applications (such as Program Manager, Task Manager, Control Panel, and so forth) can perform an update.
BraveHeart2222 2004-06-21
  • 打赏
  • 举报
回复
我需要的是:代码执行后,在[我的电脑]==>[属性]==>[高级]==>[环境变量]中能看到它们
dreamy 2004-06-21
  • 打赏
  • 举报
回复
找到了:
ExpandEnvironmentStrings(
LPCTSTR lpSrc,
LPTSTR lpDst,
DWORD nSize
);
pomelowu 2004-06-21
  • 打赏
  • 举报
回复
另外

环境变量由一对字符串组成,格式为:变量=关联值。例如,

PATH=C:\WINDOWS

在DOS和WINDOWS中,你可以从命令行或在批处理文件中定义环境变量,方法是使用SET命令,后面跟赋值表达式。下面是一个创建环境变量的例子,

SET VERSION=1.1.3

在Unix中可以省略前面的SET,写为:

VERSION=1.1.3

读取环境变量

程序只有在启动时才能访问到环境变量,环境变量由一个字符串数组构成,其形式为“VAR=value”。可以用getenv()和putenv()函数从程序中访问环境变量。

ANSI C 的函数getenv()提供了轻松简便的访问环境变量的方法,这个函数在
<stdlib.h>中声明,原型如下:

char * getenv(const char * name);

如果函数调用成功,它返回环境变量的字符串值。如果变量没有被定义,函数返回空(NULL)。例如:
#include <stdlib.h>
#include <stdio.h>
int main()
{
char * descr = getenv("PATH");
if (descr)
printf("value of PATH is: %s", descr);
else
printf("variable not defined");
}

设置环境变量

为了定义环境变量或者改变现存的变量值,使用putenv()函数。这个函数不是由标准的ANSI/ISO定义的。在实际应用中,Unix和Windows都将这个函数作为非标准扩展定义在<stdlib.h>文件中,它是可移植的。其原型如下:

int putenv(const char * var);

参数var必须是字符串,而且格式必须是“VAR=VAL”。putenv()将变量VAR添加到当前的环境变量中,并赋值VAL。如果变量已存在, putenv()覆盖现存的值。如果你不想覆盖现存变量的值,就先调用getenv()来检查变量是否存在,然后再调用putenv()。下面的例子是添加一个名为TEMP的环境变量,赋值为C:\TEMP(注意反斜杠在C/C++字符串中的表示方法)。如果调用成功,putenv()返回0,否则返回-1。
int stat = putenv("TEMP=C:\\TEMP");
if (!stat)
{
printf("环境变量定义失败");
}

支持大字符集变量

Win32中也能定义getenv()和putenv()的大字符集版本。注意_wputenv()使用于NT和Win2000:

wchar_t * _wgetenv(const wchar_t * name);
int _wputenv(const wchar_t * var);
pomelowu 2004-06-21
  • 打赏
  • 举报
回复
2楼的方法其实很好,如果你不喜欢就改注册表吧:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Session Manager\Environment\path
dreamy 2004-06-21
  • 打赏
  • 举报
回复
Marked
pomelowu 2004-06-21
  • 打赏
  • 举报
回复
TO:zhtrue(zhtrue)
看看这个:

SetEnvironmentVariable

......

Remarks
This function has no effect on the system environment variables or the environment variables of other processes

不行哦
zhaolaoxin 2004-06-21
  • 打赏
  • 举报
回复
可以直接用system( "set path=c:\mydocs;" );
zhtrue 2004-06-21
  • 打赏
  • 举报
回复
不是用这个吗?SetEnvironmentVariable

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧