Refresh System Environment Variables for all processes without Logout/ Restart in C++

1.9k views Asked by At

I am writing a program in c++ that adds a new directory to the path environment variable for the system. The directory is successfully added with the RegSetValueEx() function but the changes are not reflected for all processes.

I have tried BroadcastSystemMessage() and SendMessageTimeout() functions seperately as below

LPCTSTR keyPath = TEXT("System\\CurrentControlSet\\Control\\Session Manager\\Environment");
        
BroadcastSystemMessage(0, 0, WM_SETTINGCHANGE, 0, (LPARAM)keyPath); 

and

LPCTSTR keyPath = TEXT("System\\CurrentControlSet\\Control\\Session Manager\\Environment");

SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)keyPath, SMTO_BLOCK, 100, NULL); 

but they both dont work, however when i restart my system the changes are then reflected.

What i want

I want the changes to be reflected for all process without logout and it is possible as few days ago i installed a software (Composer https://getcomposer.org) that added its environment path and refreshed the environment variables for all processes without requiring a system restart.

Here are some useful links i have already viewed

1

There are 1 answers

0
Huzaifa On BEST ANSWER

I figured out what i was doing wrong, I was using

LPCTSTR keyPath = TEXT("System\\CurrentControlSet\\Control\\Session Manager\\Environment");
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)keyPath, SMTO_BLOCK, 100, NULL); 

when it should be

LPCTSTR keyPath = TEXT("Environment");
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)keyPath, SMTO_BLOCK, 100, NULL);

and this refreshed the environment variables for other processes without a restart.