Thread: Setting path environment variable

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Setting path environment variable

    I have made some code to change the path environment variable, but it doesn't seem to be working. Somehow. I know that there are current user and system env vars, but I don't know which I am setting. I change it, all functions are successful, but when it is done, I can not access the things in the path in the command prompt.

    Code:
    	std::vector<TCHAR> v;
    	int ret = 0;
    
    	ret = ::GetEnvironmentVariable
    		(TEXT("PATH"), 0, 0);
    
    	if(ret)
    	{
    		v.resize(ret);
    	}
    
    	ret = ::GetEnvironmentVariable
    		(TEXT("PATH"), &v[0], v.size());
    
    	if(ret)
    	{
    		std::tstring t(&v[0]);
    
    		t += TEXT(";C:\\Program Files\\MinGWStudio\\MinGW\\bin");
    
    		std::tcout << t;
    
    		ret = ::SetEnvironmentVariable(TEXT("PATH"), t.c_str());
    	}
    Definitely um, yeah, doesn't work. Confusing.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Enviroment variables inherit only one-way, from parent to child.
    Any change you make to a childs enviroment works on a copy of the environment only.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What about system environment variables, like path. I don't have spawned processes or anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Same thing.

    If you spawn a process from what you've done, you'll see the local changes you've made.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465


    Sorry. I guess I'm not trying to make local changes, but rather global ones. Is that possible with these API's?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Edit: I'm dumb.

    Calling SetEnvironmentVariable has no effect on the system environment variables. The user can add or modify system environment variables using the Control Panel. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Contro l\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.
    So uh, yeah. I'm doing this now:

    Code:
    	LONG ret;
    	HKEY hkey;
    
    	ret = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\ \
    			Session Manager\\Environment"), 0, KEY_ALL_ACCESS, &hkey);
    
    	if(ret != ERROR_SUCCESS)
    		std::tcout << ::GetLastError();
    It is never ERROR_SUCCESS, and GetLastError is always 0. Yah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. Environment variable corruption
    By miclus in forum Linux Programming
    Replies: 7
    Last Post: 10-01-2004, 01:45 PM
  3. getcwd() function messes up my environment variable
    By miclus in forum C Programming
    Replies: 5
    Last Post: 09-20-2004, 06:33 PM
  4. single environment variable
    By SpEkTrE in forum Linux Programming
    Replies: 2
    Last Post: 10-21-2003, 12:36 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM