cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Amarjeet
Level 7

VB Script for WM_SETTINGCHANGE

Hi,

In Windows 10 Environment variable does not get updated till you restart the computer.

Do you have working vb script to programmatically broadcast this message ?

Microsoft says that following code snippet should work. Directly copy pasting this code in vb script does not work.


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


Thanks is advance.

Amarjeet
Labels (1)
0 Kudos
(7) Replies
hidenori
Level 17

I'm not sure if VBScript can call Windows APIs directly. Instead you may want to try an InstallScript custom action to send a Windows message. Here is a sample code calling the SendMessageTimeout API:

prototype BOOL USER.SendMessageTimeout(HWND, SHORT, SHORT, POINTER, SHORT, SHORT, POINTER);

#define HWND_BROADCAST 0xFFFF
#define WM_SETTINGCHANGE 0x001A
#define SMTO_ABORTIFHUNG 0x0002

export prototype RefreshEnvironment(HWND);

function RefreshEnvironment(hMSI)
STRING sParam;
POINTER pParam, pResult;
LONG lResult;

begin
sParam = "Environment";
pParam = &sParam;
pResult = &lResult;

if (!SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, pParam, SMTO_ABORTIFHUNG, 100, pResult))
then
SprintfBox(WARNING, "", "SendMessageTimeout failed in RefreshEnvironment, Error %d", GetLastError());
endif;
end;

If you author the Environment table to install environment variables, a WM_SETTINGCHANGE message should be sent by the WriteEnvironmentStrings action. I had a quick test with a Basic MSI project just installing a new environment variable using the Environment Variables view in InstallShield, without having the custom code sending the WM_SETTINGCHANGE message. I was able to see the environment variable in the CMD prompt on a Windows 10 (10.0.14393) machine immediately after the installation was completed.
0 Kudos
Amarjeet
Level 7

Hi hidenori,

I tried to follow your steps by simply authoring environment variable table. But unfortunately it does not work. Following is my workflow :

- Installed my application. I was able to see environment variable in registry after successful installation. Confirmed that writeenvironmentstring custom action was executed successfully.

Doing action: WriteEnvironmentStrings
Action 17:15:35: WriteEnvironmentStrings. Updating environment strings
Action start 17:15:35: WriteEnvironmentStrings.
WriteEnvironmentStrings: Name: Updating environment strings, Value: , Action
Action ended 17:15:35: WriteEnvironmentStrings. Return value 1.

- Tried to open my application which uses this string as license server location. My application was not able to locate newly added environment variable.

- Closed my application. Opened environment variable dialog from my computer properties. Newly installed environment variable can be seen in this dialog. Clicked OK on dialog to close the dialog. As per publicly available documents closing this dialog sends above mentioned broadcast message.

- Opened my application and it was successfully able to detect license server location.

I would recommend you to try this by invoking an application which uses installed environment variable.

Regards,

Amarjeet
0 Kudos
hidenori
Level 17

As mentioned, I was using the Command Prompt (cmd.exe) to verify an environment variable change made by Windows Installer as follows:

[LIST=1]
  • Create a new Basic MSI project.
  • Go to the Environment Variables view.
  • Right-click the Environment Variables root node and select Add Environment Variable context menu.
  • The Create a New Component dialog box should be displayed.
  • Create and select a new feature, and then click OK to dismiss the Create a New Component dialog.
  • A new environment variable should be added to the Environment Variables tree.
  • Select and rename the new environment variable to "MyEnvVar".
  • Set the Value setting to "MyValue".
  • Build and install the setup.
  • Launch the Command Prompt (cmd.exe) after the installation was completed.
  • Type Set MyEnvVar and then press Enter key in the Command Prompt.
  • MyEnvVar=MyValue is displayed, instead of Environment variable MyEnvVar not defined.
  • 0 Kudos
    Amarjeet
    Level 7

    Hi Hidenori,

    The procedure you've described is right. It does write env. variable in registry but applications are not able to use it until message is broadcasted by wm_settingchange.

    Try to search internet and you'll find lot of people struggling with the issue I am facing, specifically with windows 10.

    In article at below link Microsoft is saying that environment variable will be propagated after issuing wm_settingchange command.

    https://support.microsoft.com/en-gb/kb/104011

    Can you point me to the document in installshield site where its specified that custom action writeenvironmentstrings executes wm_settingchange ?

    Regards,

    Amarjeet
    0 Kudos
    hidenori
    Level 17

    I also created a C++ console application that just reads my environment variable using the GetEnvironmentVariable API. It just worked like a charm on my Windows 10 box. My application was able to retrieve the value of the environment variable immediately after the installation was completed. Is the unexpected behavior exhibited on all of your Windows 10 machines? Did you test it on any other platforms like Windows 7? To narrow down the issue, I recommend that you create a setup just installing an environment variable from scratch as well as you write a small application just reading the environment variable, and test it on a clean machine. My C++ code is as simple as below:

    [Code]
    TCHAR szEnvValue[256];
    DWORD dwSize = 255;

    GetEnvironmentVariable(_T("MyEnvVar2"), szEnvValue, dwSize);
    MessageBox(NULL, szEnvValue, _T("MyEnvVar2"), MB_OK);
    [/Code]


    For more details about the WriteEnvironmentStrings action, click the link below:
    https://msdn.microsoft.com/en-us/library/windows/desktop/aa372883(v=vs.85).aspx

    0 Kudos
    Amarjeet
    Level 7

    Hi Hidenori,

    I checked on Windows 7 and in Windows 7 the environment variable is picked correctly by my application.

    So this issue is specific to Windows 10. It seems that in Windows 10 the msi is not able to propagate the addition of env. variables.

    Regards,

    Amarjeet
    0 Kudos
    Amarjeet
    Level 7

    Finally i was able to fix the problem with help of this little executable.

    Running this executable at end of setup and it correctly broadcasts the updated environment variables to all applications.

    Amarjeet

    0 Kudos