How to set PATH variable using QT?

How can I get and set the PATH variable using QT 4.8? I know that I can get the values ​​of the PATH variable using getenv from STL, but I don’t know how to set it using STL or any Qt based method?

If QT has a function for it, I would like to know and use it, and not go and use the Windows API for this.

+4
source share
3 answers

You can use setenv from stdlib.h to set PATH to a new value.

setenv("PATH","/new/path/value",1) 

However, this is a non-standard extension of standard headers and affects only the subprocesses generated by the calling process. To change the environment variables for all new processes, you must use the system method. For windows, the PATH variable can be changed to

 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 

registry key. This will ensure that PATH is configured for all new processes and will be applied upon reboot.

+3
source

Thanks to my friend Mr. Toshi, you can set the environment variable for the current process using qputenv("key", "value") and get it using qgetenv("key") .
This also works on Qt 5.5.0 :)

+8
source

I worked with the Registry value with this code:

Include:

 #include <windows.h> 

To read:

 QSettings setting( "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", QSettings::NativeFormat ); QString pathVal = setting.value("Path", "no-path").toString(); 

To write:

 setting.setValue( "Path", path ); SendMessageA( HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment" ); 

Thus, I get the actual Path value without reloading the program and write the value that passes the change to all processes.

Failed to figure out how to use SendMessage from this answer:
How to change the PATH variable specifically through the command line in Windows .
I thought I should create a Win32 application in Visual Studio and then send this message inside it.

But this function should be called immediately after changing the registry. Therefore, I can manually change the registry value, and then click the button called SendMessageA and the Path updated.


By the way, there is a SendMessage macro that calls the SenMessageW function, but it does not work, and Path not changed. I don’t know what A means, but it changes the variable.

+2
source

Source: https://habr.com/ru/post/1489914/


All Articles