最近开始学Java。我们主要学Java Application和Java Applet两个分类。为了在任意目录下都能编译和运行Java程序,需要对path和classpath环境变量进行相关设置。终于知道环境变量用处之一了。想用程序来实现一下设置环境变量。好不容易用注册表函数写好,结果不行。晕。。。后来想到应该要刷新一下。注销和重起都可以。但我想实现的是立即生效。后来上网搜了一下,得到如下结果: 通过修改注册表,修改了系统的环境变量,但是不会立即生效,除非在【我的电脑】上点击右键,然后打开环境变量的窗口,点击【OK】后,才能生效。后来用Spy++工具捕获消息,发现在点击【OK】按钮时,发送了WM_SETTINGCHANGE消息。 这里有两种方法可以实现: 1.SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)TEXT("Environment")); 2.SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, 0); 终于成功了,高兴哇。后来无意中发现用VBS配置环境变量并立即生效简单得要命,当场晕倒。。。 下面把两种实现方法都贴出来吧。 - //////////配置JAVA环境变量////////
- #include <windows.h>
- #include <string.h>
- #include <stdlib.h>
- int SetValue (char * valueName, char *AddValue);
- DWORD nLength = MAX_PATH;
- char DirectoryName[MAX_PATH];
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- char pValue[MAX_PATH];
- ::ZeroMemory(DirectoryName,MAX_PATH);
- ::ZeroMemory(pValue,MAX_PATH);
- // 得到当前执行文件的路径名
- ::GetCurrentDirectory(nLength,DirectoryName);
- // 设置path环境变量
- strcat(pValue,";");
- strcat(pValue,DirectoryName);
- strcat(pValue,"\\bin");
- SetValue("path",pValue);
- // 重置pValue
- ::ZeroMemory(pValue,MAX_PATH);
- // 设置classpath环境变量
- strcat(pValue,".;");
- strcat(pValue,DirectoryName);
- strcat(pValue,"\\lib\\tool.jar;");
- strcat(pValue,DirectoryName);
- strcat(pValue,"\\lib\\dt.jar");
- SetValue("classpath",pValue);
- return 0;
- }
- int SetValue(char * valueName, char *AddValue)
- {
- DWORD keyType = REG_SZ;
- BYTE getValue[1024];
- DWORD DataLen = 1024;
- HKEY hRoot = HKEY_LOCAL_MACHINE;
- HKEY hKey;
- char *pIsset;
- ::ZeroMemory(getValue,MAX_PATH);
- char *szSubKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
-
- // 打开键
- DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
- LONG lRet = ::RegCreateKeyEx(hRoot, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
- KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
- if (lRet != ERROR_SUCCESS)
- return -1;
-
- // 读取键值
- lRet = ::RegQueryValueEx(hKey,valueName,NULL,&keyType,getValue,&DataLen);
-
- pIsset = strstr((char*)getValue, DirectoryName);
- // 如果已设置则返回
- if (pIsset)
- {
- char MsgText[80];
- strcpy(MsgText,valueName);
- strcat(MsgText,"环境变量已配置好,不需再配置!");
- ::MessageBox(NULL,MsgText,"fc提示",MB_OK);
- return -1;
- }
- strcat((char*)getValue,AddValue);
-
- // 写入键值
- lRet = ::RegSetValueEx(hKey, valueName, 0, REG_SZ, (BYTE*)getValue, strlen((char*)
- getValue));
- if (lRet == ERROR_SUCCESS)
- {
- char MsgText[80];
- strcpy(MsgText,valueName);
- strcat(MsgText,"环境变量配置成功!");
- ::MessageBox(NULL,MsgText,"fc提示",MB_OK);
- // 使设置立即生效,下面两种法都可以
- SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)TEXT("Environment"));
- //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
- (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, 0);
- }
-
- // 关闭句柄
- ::RegCloseKey(hKey);
- return 0;
- }
复制代码 - on error resume next
- set sysenv = CreateObject("WScript.Shell").Environment("system") '系统环境变量的数组对象
- sysenv.Remove("ztest3") '删除变量
- sysenv("ztest3")="test value" '添加变量
复制代码 VBS这种简单吧……
|