5

How can I get and set PATH variable using QT 4.8? I know I can get PATH variable values using getenv from the STL but 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 rather than going and using Windows API for it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224

3 Answers3

12

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

S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59
3

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 to the standard headers, and will only affect sub-processes spawned by the calling process. In order to change environment variables for all new processes spawned, a system-specific method must be used. For windows, the PATH variable can be changed in the

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

registry key. This will make sure the PATH is set for all new processes, and will apply over a reboot.

exrook
  • 90
  • 5
  • Thank you very much :) i didnt know that we have setenv at STL!. But do you know if we have some thing Qt based? if there is'nt anything like it i accept his as answer. – Hossein Jul 06 '13 at 03:38
  • It seems that you can use [QProcessEnviroment](http://qt-project.org/doc/qt-4.8/qprocessenvironment.html) to set the environment of processes spawned using [QProcess](http://qt-project.org/doc/qt-4.8/qprocess.html). – exrook Jul 06 '13 at 14:33
  • Thanks, I need a machine wide permission ( i mean pertaining to the whole OS, not the process itself ) will it still get me going? – Hossein Jul 07 '13 at 14:49
  • If you need the `PATH` to be changed system wide, then you need to use the Windows API to add the desired change to the `HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment` [registry key](http://stackoverflow.com/questions/3636055/how-to-modify-the-path-variable-definitely-through-the-command-line-in-windows), which will change the path for any new applications launched. The changes you make will also survive a reboot. – exrook Jul 08 '13 at 19:33
  • Thanks, then would you add this last comment as well to the answer ? and also mention _putenv() as well (since setenv seems not to be in standard headers! couldnt find it!) – Hossein Jul 09 '13 at 02:52
2

I worked with the Registry value using 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" );

This way I get the actual Path value without reloading the program and write the value broadcasting the change to all processes.

Couldn't realize first how to use SendMessage from this answer:
How to modify the PATH variable definitely through the command line in Windows.
I thought I should create a Win32 app in Visual Studio and then send this message inside of it.

But this function should be called just after the registry changed. So I could edit the registry value manually and then press a button which called the SendMessageA and the Path updated.


BTW, there is SendMessage macro which calls SenMessageW function but it didn't work and the Path didn't change. Don't know what the A means but it changes the variable.

Community
  • 1
  • 1
mortalis
  • 2,060
  • 24
  • 34