After modifying systems env var "Path", how to broadcast it?

Windows specific forum
oO0XX0Oo
User
User
Posts: 78
Joined: Thu Aug 10, 2017 7:35 am

After modifying systems env var "Path", how to broadcast it?

Post by oO0XX0Oo »

Hi,

I'd like to edit edit environment variables, e.g. the systems "Path" variables
(by reading the ones to append from an .ini file and writing the new combined
one into the registry). Ofc my .exe is started with the appropriate admin permissions
to be able to write to HKLM.

All these things are working fine so far, but what is missing is the ability to
broadcast this variable change so that a newly opened command prompt
would show the changes variables when "set" is executed.

I've tried it with:

Code: Select all

Define.s lParam = "Environment"
SendMessage_(#HWND_BROADCAST, #WM_SETTINGCHANGE, 0, @lParam)
but this doesn't cut it...

The only way (and this is what I want to avoid!) is to open the the belonging window
Control Panel - System and Security - System - Advanced system settings - Environment Variables...
and hit the OK button.
Then a "set" in a new command prompt will show the changes...

It does work fine when I do this with the users "Path" env variable, but it does not
work with the systems "Path" env var...
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: After modifying systems env var "Path", how to broadcast

Post by skywalk »

I use this after changing an environment variable.

Code: Select all

Procedure.i SL_RegEnvUpdate(tmo_ms.i=5000)
  ; Update Registry without logout/reboot:
  ; After programmatically adding or modifying system environment variables in registry key:
  ;   HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
  ; We must broadcast a WM_SETTINGCHANGE message with lParam = "Environment" for them to be used.
  ; WM_SETTINGCHANGE signals all windows(Windows Explorer, Program Manager, Task Manager, Control Panel, etc.) to update.
  ; SendMessageTimeout(#HWND_BROADCAST, #WM_SETTINGCHANGE, 0, (LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &dwReturnValue)
  ; LRESULT WINAPI SendMessageTimeout(_In_      HWND       hWnd,
  ;                                   _In_      UINT       Msg,
  ;                                   _In_      WPARAM     wParam,
  ;                                   _In_      LPARAM     lParam,
  ;                                   _In_      UINT       fuFlags,
  ;                                   _In_      UINT       uTimeout,
  ;                                   _Out_opt_ PDWORD_PTR lpdwResult);
  ; RETURN: 0 if timeout, else OK.
  Protected.l rl, rl2
  rl = SendMessageTimeout_(#HWND_BROADCAST, #WM_SETTINGCHANGE, 0, "Environment", #SMTO_ABORTIFHUNG, tmo_ms, @rl2)
  Delay(30)
  ProcedureReturn rl
EndProcedure
SL_RegEnvUpdate() ;SendMessageTimeout_(#HWND_BROADCAST, #WM_SETTINGCHANGE, 0, "Environment", #SMTO_ABORTIFHUNG, 5000, @rl)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: After modifying systems env var "Path", how to broadcast

Post by Marc56us »

Look Windows help for SETX command (yes setx not set)
Try use it with runprogram
(Not tested)

:wink:
oO0XX0Oo
User
User
Posts: 78
Joined: Thu Aug 10, 2017 7:35 am

Re: After modifying systems env var "Path", how to broadcast

Post by oO0XX0Oo »

@skywalk

Thanks a lot, it works fine with your code now :mrgreen:


@Marc56us

Thanks but I prefer a more native way :oops:
Post Reply