|
how do I do that?
Elapid For The Win
|
|
|
|
|
See here and here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I am attempting to create/update system variables in WinXP via the registery. I can add/update the variables and values just fine, however I cannot get the changes to recognized by the OS without rebooting. Now I have searched here and googled this question and everyone has the same answer....Use SendMessageTimeout to send a HWND_BROADCAST message with the WM_SETTINGCHANGE parameter. I have done this, but when I start another program that accesses the variable, the old value is still there... So it must be my program...But its hard to screw up a cut and paste of code so any ideas ?
for(int x=0;x<m_VarList.GetItemCount();x++)
{
if(IsDlgButtonChecked(IDC_USERRADIO))
SetEnvironmentalVariable(m_VarList.GetItemText(x,0),m_VarList.GetItemText(x,1),FALSE);
else if(IsDlgButtonChecked(IDC_SYSTEMRADIO))
SetEnvironmentalVariable(m_VarList.GetItemText(x,0),m_VarList.GetItemText(x,1));
}
if(IsDlgButtonChecked(IDC_FORCEUPDATE))
{
DWORD dwReturnValue;
long res;
res=SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,(LPARAM) "Environment", SMTO_ABORTIFHUNG,5000, &dwReturnValue);
if(res!=0)
AfxMessageBox("System update was successful you will NOT have to reboot the computer");
else
AfxMessageBox("System update was successful, but you will have to reboot the computer");
}
CDialog::OnOK()
As you can see I've tried different combinations of broadcast messages, but the value still does not get updated. I'd appreciate any help you could provide.
The program I use that accesses the system variable was written for testing and consists of a single line of C# code
string Value=Environment.GetEnvironmentVariable("TEST_SYS_VAR");
|
|
|
|
|
Guinness4Strength wrote:
for(int x = 0; x < m_VarList.GetItemCount(); x++)
{
if (IsDlgButtonChecked(IDC_USERRADIO))
SetEnvironmentalVariable(m_VarList.GetItemText(x, 0), m_VarList.GetItemText(x, 1), FALSE);
else if (IsDlgButtonChecked(IDC_SYSTEMRADIO))
SetEnvironmentalVariable(m_VarList.GetItemText(x, 0), m_VarList.GetItemText(x, 1));
}
Since the state of the radio buttons will not change within this for() loop, why not put the if() statement outside of the loop?
In any case, since you indicate that the registry is being updated correctly, how is this other program accessing the registry such that it is not getting the updated values?
Also, if this other program is not running at the time the registry is being updated, the SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, ...) code is meaningless anyway.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
DavidCrow wrote:
In any case, since you indicate that the registry is being updated correctly, how is this other program accessing the registry such that it is not getting the updated values?
If you look at the second code section of the originasl post I explain this. Its just a simple C# console program.
DavidCrow wrote:
Also, if this other program is not running at the time the registry is being updated, the SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, ...) code is meaningless anyway.
Well in a live situation running programs will need to know there is a change
Basically after I update the registry and send the broadcast (which is meaningless in my test scenerio) the C# program that attempts to access the environmetal variable does not return the correct value for the variable.
I would assume the broad cast message would force the shell/OS to update its values from the registry so the C# program would receive the new value.
|
|
|
|
|
Guinness4Strength wrote:
If you look at the second code section of the originasl post I explain this. Its just a simple C# console program.
Does GetEnvironmentVariable() read from the registry or from a cache? Try reading from the registry explicitly and note the results.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
If I read from the registry its correct of course because getting the correct value in the registry has never been the issue.... The whole problem is forcing the cache to update. I have no control over how other blackbox programs get the environmental variables, so I need to assume that both the registry and cache must be updated.
|
|
|
|
|
Unless the other programs/processes are responding to the WM_SETTINGCHANGE message, there's nothing more you can do.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I was afraid of this. So there is no way to force Winodws "re-read" the registry to update its system variable values ?
|
|
|
|
|
No. This is akin to the "You can lead a horse to water but you can't make him drink it." adage. By broadcasting the WM_SETTINGCHANGE message, you've done your part. It's up to the other programs/processes to re-read from the registry if they see fit.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
But what doesn't make sense is that even if I stop and start the programs that use the environmental variables after I've made the change to the registry the old value still persists. So the only thing I can assume is that the OS itself is not responding to the WM_SETTINGCHANGE message. Basically the whole point of this program was to allow a user to change environmental variables without having to reboot for them to take effect. So from what you saying this cannot be done.
|
|
|
|
|
Guinness4Strength wrote:
But what doesn't make sense is that even if I stop and start the programs that use the environmental variables after I've made the change to the registry the old value still persists.
Are those programs reading directly from the registry?
See here and here for more.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Thanks for the additional info, but since I'm neither touching the PATH variable nor am I using the SetEnvironmentVariable() API, it doesn't really get me any closer to solving this problem. I appreciate you efforts though Dave.
|
|
|
|
|
Guinness4Strength wrote:
...nor am I using the SetEnvironmentVariable() API...
Check your original post. Unless my eyes deceive me, I believe that SetEnvironmentVariable() is being used.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Your eyes do deceive you... I wrote SetEnvironmentALVariable(string,string BOOL)....its my own function
Basically just updates the
HKLM\\System\\CurrentControlSet\\Control\\Session Manager\\Environment key.
|
|
|
|
|
Guinness4Strength wrote:
Your eyes do deceive you... I wrote SetEnvironmentALVariable(string,string BOOL)....its my own function
Fair enough! Those two little characters were hiding behind the bushes.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Yup, and if I ever get this thing into production, I'll change the name of the function to be more unique.
|
|
|
|
|
Your base assumption is invalid:
//cycle through entrys and add to registry
You only modified the environment block for your current process!
Calling SetEnvironmentVariable has no effect on the system environment variables. The user can add or modify system environment variables using the Control Panel. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message. This allows applications, such as the shell, to pick up your updates. Note that environment variables listed in this key are limited to 1024 characters.
You really DO need to 'change the registry' yourself.
THEN broadcast the message and it should be picked up by other processes.
|
|
|
|
|
Upon closer inspection you will see that SetEnvironmentalVariable is my own function....
As I said in my original post, the variables are getting set in the registry correctly, I have already verified this.
|
|
|
|
|
If you are modifying the PATH, for example, make sure your registry is using the REG_EXPAND_SZ field type if that is appropriate.
I rechecked what we do, and we call RegFlushKey before closing the registry key and before calling the WM_SETTINGCHANGE. If you are not doing that already, it might help your situation.
|
|
|
|
|
Hi,
I'm having trouble with handle maps. I've been reading some information about the topic but I don't see how it applies to the problem I'm having.
Basically I have a class that represents a camera. It's a wrapper for API calls to set properties and capture images. The camera class also has a modeless dialog box as one of its members to display camera properties to the user. The dialog is created and exists for the lifetime of the camera class and there are function to show/hide it.
For some reason the following fails
<br />
bool MultipleCameraWrapper::SetShutter(int nShutter)<br />
{<br />
...<br />
this->m_pConfigDlg->m_nShutter = nShutter;<br />
this->m_pConfigDlg->UpdateData(FALSE);
}<br />
More specifically
CHandleMap* pMap = afxMapHWND(); returns NULL inside UpdateData(). Can someone tell me why and how to fix it?
|
|
|
|
|
Are you calling it from a different thread than the one that created it?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
|
Well then that's the problem. MFC stores handles in Thread Local Storage, so they aren't available from another thread. It's not a good idea to update the UI from another thread anyway (http://www.codeproject.com/csharp/begininvoke.asp[^]. I'd suggest posting a message to the UI instead (using PostMessage) and handling the message there.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
hi all,
I have followed the tutorial in this site about how to add items in the context menu.
But I don't know why I integrate it with another sample code, which is about cascading menu, the explorer.exe halt.
The problem happens when I rightclick on some empty spaces (not on files / folders) or the desktop.
Am I missing some controls to the context menu for that, besides files / folder?
What should I do?
Thank you very much
|
|
|
|