Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command window not seeing new system env. variables

Status
Not open for further replies.

chiph

Programmer
Jun 9, 1999
9,878
US
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:
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;
}
Thanks in advance.
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
About the only window already open that actually bothers noticing that system envronment variables have been modified when receiving a WM_SETTINGCHANGE message is the shell (and it passes it's changed environement block on to any new applications that are opened from that point onwards).
 
After some late-night searching in the newsgroups, I've come to that conclusion too. Cmd.exe just doesn't pay attention to the WM_SETTINGCHANGE message.

Thanks for the reply.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top