LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing Environment variables in Windows 10

Solved!
Go to solution

I thought that the answer to can LV read an environment variable (EV) would be a simple yes! but no such luck. I came across https://forums.ni.com/t5/LabVIEW/How-do-I-manipulate-Windows-environment-variables-in-LabView/m-p/79... with a useful download from AKH https://forums.ni.com/ni/attachments/ni/170/363850/1/UES%20Read%20Environmental%20Variables.vi which initially seemed ideal. However if I create a new EV (in either USER or SYSTEM) and use the VI to search for it - it is not found - if I then shutdown LV and run the VI it again - low and behold there is my EV. However if I then edit the EV value without closing LV the change is not reported (note VI is single shot so have to press run each time), however closing LV and restarting allows the VI to pick up the change. So clearly LV or IWshShell2 grabs the EVs on first entry/call and never refreshes on subsequent calls. Is there any way of forcing a refresh without closing and restarting LV?

0 Kudos
Message 1 of 4
(2,519 Views)
Solution
Accepted by topic author IanR99

I'm going to guess no - when you spawn a process it typically gains some set of environment variables (which may be all of them, or not), which are not connected to the hosting process (e.g. Windows). Instead, they are a copy. I believe this has a bunch of security implications, beyond anything else. 

 

This article describes some of the setx method or the registry method described in the linked thread: http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-windows-command-l...

 

You may also be able to use the System Exec node to call Powershell and then from that call some command which sets environment processes, but it will be subject to the same limitations I would think.


GCentral
0 Kudos
Message 2 of 4
(2,461 Views)

It's possible to signal that environmental variables have changed using WM_SETTING­CHANGED message, but, according to the following, LV (along with most other applications) probably doesn't support it:

https://devblogs.microsoft.com/oldnewthing/20150915-00/?p=91591

 

As cbutcher said, they are copied to the child process and act as a template.



Using LV2018 32 bit

Highly recommended open source screen capture software (useful for bug reports).

https://getsharex.com/
0 Kudos
Message 3 of 4
(2,428 Views)

Thanks to all who replied - I also finally found the info on EVs being copied to a process and the need to react to WM_SETTING­CHANGED signal if updates to be picked up. I suspect that also explains why LV does not pick up changes to the daylight saving ON/OFF flag until it is shut down and restarted. It is a fun 'feature', if you use the EXEC VI to run CMD with SET outputting to a file - even that mechanism does not pick up a change - of course in that case CMD is a sub-process of LV and gets a copy of the original EVs.

Thinking harder I did notice that using SET in a CMD window to change an EV - the change was not picked up by the Control Panel - Environmental Variables menu even if CP run after the change so does that make the only way to change an EV through the Control Panel and changes in a CMD window only affecting the copies in that invocation of CMD - and its sub-processes? I think so.

0 Kudos
Message 4 of 4
(2,402 Views)