主题
设置环境变量 - RegistrySetEnvironmentVariable
函数简介
设置环境变量,支持用户级和系统级环境变量。
接口名称
RegistrySetEnvironmentVariableDLL调用
cpp
int32_t RegistrySetEnvironmentVariable(int64_t instance, const char* name, const char* value, int32_t systemWide);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| name | 字符串 | 环境变量名称。 |
| value | 字符串 | 环境变量值。 |
| systemWide | 整数型 | 是否为系统级环境变量。1 系统级(所有用户可见),0 当前用户级。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int ret = ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);csharp
using OLAPlug;
var ola = new OLAPlugServer();
int ret = ola.RegistrySetEnvironmentVariable("MY_APP_HOME", @"C:\Apps\OLA", 0);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
ret = ola.RegistrySetEnvironmentVariable("MY_APP_HOME", r"C:\Apps\OLA", 0)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);cpp
var ola = com("OlaPlug.OlaSoft")
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);text
.局部变量 ola, OLAPlug
ola.创建 ()
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);aardio
import OLAPlugServer;
var ola = OLAPlugServer();
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
ola.RegistrySetEnvironmentVariable("MY_APP_HOME", "C:\\Apps\\OLA", 0);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
RegistrySetEnvironmentVariable(instance, "MY_APP_HOME", "C:\\Apps\\OLA", 0);csharp
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int RegistrySetEnvironmentVariable(long ola, string name, string value, int scope);
long instance = CreateCOLAPlugInterFace();
int ret = RegistrySetEnvironmentVariable(instance, "MY_APP_HOME", @"C:\Apps\OLA", 0);python
from ctypes import CDLL, c_int, c_int64
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ola.RegistrySetEnvironmentVariable(instance, b"MY_APP_HOME", b"C:\\Apps\\OLA", 0)返回值
| 返回值 | 说明 |
|---|---|
1 | 成功。 |
0 | 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 管理员权限 | 设置系统级环境变量(systemWide=1)需要管理员权限。 |
| 环境变量设置后可能需要重新启动应用程序或系统才能 | 环境变量设置后可能需要重新启动应用程序或系统才能生效。 |
| 修改PATH等系统变量时建议先读取现有值 | 修改PATH等系统变量时建议先读取现有值,再追加新值。 |
| 环境变量名称不区分大小写 | 环境变量名称不区分大小写。 |
| 系统级环境变量存储在 HKEY_LOCAL_MA | 系统级环境变量存储在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment。 |
| 用户级环境变量存储在 HKEY_CURRENT_ | 用户级环境变量存储在 HKEY_CURRENT_USER\Environment。 |
