I'm trying to set a new system variable via a console application. It's working -- I'm seeing the new variable appear in:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
But immediately after running my program when I issue a SET command, it's not there. If I close the Cmd window and reopen it, it is there. It's like the current Cmd window isn't responding to the WM_SETTINGCHANGE message.
Here's the code I'm using:
Thanks in advance.
Chip H.
____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
But immediately after running my program when I issue a SET command, it's not there. If I close the Cmd window and reopen it, it is there. It's like the current Cmd window isn't responding to the WM_SETTINGCHANGE message.
Here's the code I'm using:
Code:
bool AddUpdateSystemEnvironmentVar(LPCTSTR VarName, LPCTSTR Value)
{
LONG rc;
HKEY hNewKey;
DWORD dwDisposition;
size_t Len;
DWORD dwResult;
LRESULT lr;
rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
_T("System\\CurrentControlSet\\Control\\Session Manager\\Environment"),
(DWORD)0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
(DWORD)0,
&hNewKey,
&dwDisposition);
if (rc != ERROR_SUCCESS)
return false;
SUCCEEDED(StringCbLength(Value, 255 * sizeof(TCHAR), &Len));
rc = RegSetValueEx(hNewKey,
VarName,
(DWORD)0,
REG_SZ,
(LPBYTE)Value,
(DWORD)(Len + 1));
rc = RegFlushKey(hNewKey);
RegCloseKey(hNewKey);
// let all top-level windows know of the change
lr = SendMessageTimeout(HWND_BROADCAST,
WM_SETTINGCHANGE,
0,
(LPARAM)_T("Environment"),
SMTO_ABORTIFHUNG,
5000,
&dwResult);
return true;
}
Chip H.
____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first