Page 1 of 1

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

Posted: Sun Jan 21, 2018 6:04 pm
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...

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

Posted: Sun Jan 21, 2018 8:04 pm
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)

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

Posted: Sun Jan 21, 2018 8:27 pm
by Marc56us
Look Windows help for SETX command (yes setx not set)
Try use it with runprogram
(Not tested)

:wink:

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

Posted: Mon Jan 22, 2018 12:38 am
by oO0XX0Oo
@skywalk

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


@Marc56us

Thanks but I prefer a more native way :oops: