I am trying to update the PATH
environment variable inside my C++ program using Qt on Windows 10. There is something wired in this scenario that I cannot understand. The strange point is that the value of PATH
environment variable is getting different in the C++ code and in process explorer tool after dynamically loading a Matlab .dll file (i.e. MatlabEngine.dll). Here is a simplified version of the code:
QString new_path = getNewPath();
bool res = qputenv("PATH", new_path.toStdString().c_str());
assert(res);
auto applied_path = qgetenv("PATH"); // Line X of the code: process explorer shows the same value for PATH environment variable as applied_path variable
// load a Matlab .dll dynamically
if (!oneMatlabDll.load())
{
throw someException;
}
applied_path = qgetenv("PATH"); // Line Y of the code: process explorer does not show the updated value of PATH environment variable as before. But, the applied_path variable is exactly the same as line X.
Does anyone have any idea why the value of PATH
environment variable retrieved by qgetenv()
is different from the one showed by process explorer tool? Here are some thoughts:
qgetenv()
methodUpdate 1:
If I add this silly line of code after line Y, then the process explorer shows the same value as the output of qgetenv("PATH")
:
qputenv("PATH", qgetenv("PATH")); // this line fixes the issue
I could not find any reason behind this problem. It seems that it can be an internal issues comes from Qt/Windows. So, the only workaround that I have found is to add the below line again:
qputenv("PATH", qgetenv("PATH")); // this line fixes the issue