Link to home
Start Free TrialLog in
Avatar of AA69
AA69

asked on

I need to send a broadcast To all Windows

How do I in VB using API calls send a broadcast that gets all windows to updated their System environment values?

I really need to see the code not links elsewhere.
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Private Const HWND_BROADCAST = &HFFFF&
Private Const WM_SETTINGCHANGE = &H1A
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Command1_Click()
    SendMessage HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0
End Sub
Avatar of AA69
AA69

ASKER

Thanks for the reply

I have changed a system environment variable and used this code but the change is not picked up by either a running application or one that is subsequently started.

I have read that I might need to use the SendMessageTimeout API call. Is that correct?
Avatar of Richie_Simonetti
use postmessage api instead.
SendMessageTimeout API: for NT.
Avatar of AA69

ASKER

I have attempted to use the SendMessageTimeout API call.
Which I have executed from a command button ok with a lastDllerror=0.

However for some reason the change of environment settings is still not accessible to any newly started applications.

So to be clear ...

A machine environment value MYMACHINEVAR starts off with a value of W.

I then fire up application 1 which sets MYMACHINEVAR=X. Application 1 then ends.
This is where I execute the SendMessageTimeout HWND_BROADCAST WM_SETTING_CHANGE using my command button code.

I then start application 2 which see to think that MYMACHINEVAR's value is still set to W.

If I inspect the machine Varibles using the control panel route the correct value is present i.e. X. If I ok from this dialog the next execution of application 2 has the correct value.

Please help this is doing my head in!

Did you try with Tim code but postmessage instead of sendmessage?
Avatar of AA69

ASKER

Yes I have tried SendMessageTimeout,SendMessage and postmessage still no luck.
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AA69

ASKER

Richie,

It must be possible to do this without restart as you can go into the Control pannel and find the system variable that you have programatically set. This variable's value is correct but it still hasn't taken effect. If you then click OK the update becomes available from that point onwards. Has Tim or Riche got an example app (just a test project or something) that proves this works?

Thanks
Andy.
Strange, changes are made but app were not notified?
Maybe, what is failling is post/sendmessage api. Try to read return value from it with debug.
Take a look at this:

HOWTO: Propagating Environment Variables to the System
Last reviewed: May 27, 1997
Article ID: Q104011  
The information in this article applies to:
Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.5, 3.51, 4.0


SUMMARY
User environment variables can be modified by editing the following Registry key:


   HKEY_CURRENT_USER \
         Environment


System environment variables can be modified by editing the following Registry key:

   HKEY_LOCAL_MACHINE \
               SYSTEM \
    CurrentControlSet \
              Control \
      Session Manager \
          Environment


Note, however, that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.
To effect these changes without having to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so that any interested applications (such as Program Manager, Task Manager, Control Panel, and so forth) can perform an update.



MORE INFORMATION
For example, on Windows NT, the following code fragment should propagate the changes to the environment variables used in the Command Prompt:


   SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    (LPARAM) "Environment", SMTO_ABORTIFHUNG,
    5000, &dwReturnValue);


None of the applications that ship with Windows 95, including Program Manager and the shell, respond to this message. Thus, while this article can technically be implemented on Windows 95, there is no effect except to notify third-party applications. The only method of changing global environment variables on Windows 95 is to modify the autoexec.bat file and reboot.  
Avatar of AA69

ASKER

Hi

This is on windows2000.

As I am writing the registry key I can see my change under environment System environment values. The broadcast is not updating the shell. Because any new nt dos window has the old value until I click Ok in the environment values system dialog box.

Andy
Well, this is not what i like but, could you try it?

WshEnvironment Object
The WshEnvironment object is not exposed directly. For access to it, use the WshShell.Environment property.

ProgID N/A
Filename WSHom.Ocx
CLSID
IID


The following table describes the properties associated with the WshEnvironment object.

Property Description
Item Gets or sets the value of a specified environment variable.
Count The number of enumerated items.
length The number of enumerated items (JScript).


The following table describes the method associated with the WshEnvironment object.

Method Description
Remove Deletes a specified environment variable.


WshEnvironment.Item
The Item property sets or returns the value for the strName environment variable. It is the default property.

Syntax

WshEnvironment.Item("strName") = strValue
WshEnvironment("strName") = strValue

Example

' Get the value of NUMBER_OF_PROCESSORS environment variable
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")

' Set the value of EXAMPLE volatile environment variable to A_VALUE
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshEnv = WshShell.Environment("VOLATILE")
WshEnv("EXAMPLE")= "A_VALUE"

' List all system environment variables
Set WshShell = Wscript.CreateObject("Wscript.Shell")
For Each strVarName In WshShell.Environment("SYSTEM")
MsgBox strVarName
Next

See Also

WshShell.Environment property

WshEnvironment.Count
The Count property provides the number of enumerated items.

Syntax

WshEnvironment.Count = natNumberOfItems

See Also

WshEnvironment.length property

WshEnvironment.length
The length property provides the number of enumerated items. This property provides the same functionality as the Count property and is provided for compatibility with JScript.

Syntax

WshEnvironment.length = natNumberOfItems

See Also

WshEnvironment.Count property

WshEnvironment.Remove
The Remove method deletes the environment variable specified by strName.

Syntax

WshEnvironment.Remove(strName)

Example

' Delete the EXAMPLE volatile environment variable
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Environment("VOLATILE").Remove("EXAMPLE")

' Delete multiple variables
Set WshUsrEnv = Wscript.Environment("User")
WshUsrEnv.Remove("EXAMPLE_1")
WshUsrEnv.Remove("EXAMPLE_2")
WshUsrEnv.Remove("EXAMPLE_3")
WshUsrEnv.Remove("EXAMPLE_4")

See Also

WshShell.Environment property

so, to enumerate you use:

Private Sub Form_Load()
Set WshShell = CreateObject("Wscript.Shell")
For Each strVarName In WshShell.Environment("SYSTEM")
    MsgBox strVarName
Next
End Sub

Similar approach could be used to change values. Maybe script object takes care to send broadcast message by itself.
Good luck.
Avatar of AA69

ASKER

This isn't the correct answer i used a bodge instead in the end but you spent a far amount of time helping my thinking process so I'm more than happy to give you the points.


Sorry it took so long I've been busy playing in Linux

Andy