最近開始學(xué)Java。我們主要學(xué)Java Application和Java Applet兩個分類。為了在任意目錄下都能編譯和運行Java程序,需要對path和classpath環(huán)境變量進行相關(guān)設(shè)置。終于知道環(huán)境變量用處之一了。想用程序來實現(xiàn)一下設(shè)置環(huán)境變量。好不容易用注冊表函數(shù)寫好,結(jié)果不行。暈。。。后來想到應(yīng)該要刷新一下。注銷和重起都可以。但我想實現(xiàn)的是立即生效。后來上網(wǎng)搜了一下,得到如下結(jié)果: 通過修改注冊表,修改了系統(tǒng)的環(huán)境變量,但是不會立即生效,除非在【我的電腦】上點擊右鍵,然后打開環(huán)境變量的窗口,點擊【OK】后,才能生效。后來用Spy++工具捕獲消息,發(fā)現(xiàn)在點擊【OK】按鈕時,發(fā)送了WM_SETTINGCHANGE消息。 這里有兩種方法可以實現(xiàn): 1.SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)TEXT("Environment")); 2.SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, 0); 終于成功了,高興哇。后來無意中發(fā)現(xiàn)用VBS配置環(huán)境變量并立即生效簡單得要命,當(dāng)場暈倒。。。 下面把兩種實現(xiàn)方法都貼出來吧。 - //////////配置JAVA環(huán)境變量////////
- #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);
- // 得到當(dāng)前執(zhí)行文件的路徑名
- ::GetCurrentDirectory(nLength,DirectoryName);
- // 設(shè)置path環(huán)境變量
- strcat(pValue,";");
- strcat(pValue,DirectoryName);
- strcat(pValue,"\\bin");
- SetValue("path",pValue);
- // 重置pValue
- ::ZeroMemory(pValue,MAX_PATH);
- // 設(shè)置classpath環(huán)境變量
- 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);
- // 如果已設(shè)置則返回
- if (pIsset)
- {
- char MsgText[80];
- strcpy(MsgText,valueName);
- strcat(MsgText,"環(huán)境變量已配置好,不需再配置!");
- ::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,"環(huán)境變量配置成功!");
- ::MessageBox(NULL,MsgText,"fc提示",MB_OK);
- // 使設(shè)置立即生效,下面兩種法都可以
- SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)TEXT("Environment"));
- //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
- (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, 0);
- }
-
- // 關(guān)閉句柄
- ::RegCloseKey(hKey);
- return 0;
- }
復(fù)制代碼 - on error resume next
- set sysenv = CreateObject("WScript.Shell").Environment("system") '系統(tǒng)環(huán)境變量的數(shù)組對象
- sysenv.Remove("ztest3") '刪除變量
- sysenv("ztest3")="test value" '添加變量
復(fù)制代碼 VBS這種簡單吧……
|