Solved

I need to send a broadcast To all Windows

Posted on 2002-03-26
14
303 Views
Last Modified: 2007-11-27
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.
0
Comment
Question by:AA69
  • 7
  • 6
14 Comments
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
0
 

Author Comment

by:AA69
Comment Utility
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?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
use postmessage api instead.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
SendMessageTimeout API: for NT.
0
 

Author Comment

by:AA69
Comment Utility
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!

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Did you try with Tim code but postmessage instead of sendmessage?
0
 

Author Comment

by:AA69
Comment Utility
Yes I have tried SendMessageTimeout,SendMessage and postmessage still no luck.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 200 total points
Comment Utility
Well, at work, we are using postmessage api and it does the job.
What i was thinking is some values cannot be changed dinamically and only works after restart.
Maybe this is one of those cases.
i used a similar tim's code  to change list separators in Regional settings of Control Panel and it works (win95/98/2000).
0
 

Author Comment

by:AA69
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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.  
0
 

Author Comment

by:AA69
Comment Utility
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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.
0
 

Author Comment

by:AA69
Comment Utility
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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now