Thread: Setting environment variables for use outside program

  1. #1
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179

    Setting environment variables for use outside program

    I have a program that is trying to set the environment variable in a XP environment so that it can be used outside of the program. Unfortunately it seems that one the program terminates any environment variables that it creates are immediately forgotten. For example:

    setenvtest.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        putenv ("TEST=Works");
        system ("echo %TEST%");
        return 0;
    }
    Run it and it works fine within in itself, but run the exe as part of a batch file like:

    test.bat
    Code:
    @echo off
    setenvtest
    echo %TEST%
    pause
    And it treats %TEST% like it is unassigned outside of the program.

    So how do I fix that?
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There's no easy way for a program to change the environment of it's parent process.
    It's almost certainly impossible to do it without the any involvement of the parent process.

    Typically, the child writes the information to a file, which the parent can then run as a batch file.
    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
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I am looking at different code using SetEnvironmentVariable() and I see nothing useful for contributing to the parent process. I will keep kicking this around, its actually kind of a nifty idea. But like many nifty ideas, sometimes you need to just do whatever works (i.e. do as Salem suggested). I am going to play with the code a bit since you have piqued my curiosity a little.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Moin,

    the only way I know is to set the values in the Windows Registry under
    Code:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    Set the value name to Works, the type to REG_EXPAND_SZ and the value itself to %TEST%.

    This should do what you want, but you need Admin-Rights to write to the Registry.
    And don't forget to delete the value if you're done !

    EDIT:

    You can do it by calling
    Code:
     system ("reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v "Works" /t REG_EXPAND_SZ /d "%TEST%" /f
    or with the RegSetValueEx Function


    Greetz
    Last edited by Greenhorn__; 09-03-2008 at 05:50 PM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    True, but he is wanting to (and jump in and correct me if I am mistaken) alter the environment variables of a parent process that already exists.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Quote Originally Posted by guesst View Post
    I have a program that is trying to set the environment variable in a XP environment so that it can be used outside of the program.
    ...
    Run it and it works fine within in itself, but run the exe as part of a batch file
    I understood this like he want's to set a global env to the system with the parent process.
    If he do what I pointed to, other applications/scripts can also use the env.

    Give it a try, maybe it is what you're asked for ...


    Greetz

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    A suitable solution pending you are not aiming for environment variables for an already running program to be altered. Try out Greenhorn__'s solution, see if it can get the job done for ya.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you were using Bash on Linux you could do:
    Code:
    source ./your_Prog
    and then any environment variables your_prog sets would still exist in your current shell environment.

    But since you're on Windows, I don't have an answer at the moment.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by Greenhorn__ View Post
    Moin,

    the only way I know is to set the values in the Windows Registry under
    Code:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    Set the value name to Works, the type to REG_EXPAND_SZ and the value itself to %TEST%.

    This should do what you want, but you need Admin-Rights to write to the Registry.
    And don't forget to delete the value if you're done !

    EDIT:

    You can do it by calling
    Code:
     system ("reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v "Works" /t REG_EXPAND_SZ /d "%TEST%" /f
    or with the RegSetValueEx Function


    Greetz
    Just an addedum. Windows will still not be aware of your changes unless you log off or reboot. Thus, you should also broadcast a WM_SETTINGCHANGE to all of the windows in the system.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Think about the security problems of having some child program change the parent's PATH variable for instance.
    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.

  11. #11
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Thanks guys. In the end I just decided to go with the batch file solution. It seems the most friendly, and since this was just supposed to be a simple thing I don't want to complicate matters... for now. But it's good to have options if I decide to revisit it any time later.

    If you're curious, this is what I was using it for.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I actually have never encountered an instance where I needed to do that in an intrusive way. The way Salem mentioned earlier seems to be the least invasive method since the parent is granting the child's priv's to do make such changes, instead of the child just acquiring them.

    Are you trying to hack another program or something by ghosting one of its child processes?

  13. #13
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by master5001 View Post
    I actually have never encountered an instance where I needed to do that in an intrusive way. The way Salem mentioned earlier seems to be the least invasive method since the parent is granting the child's priv's to do make such changes, instead of the child just acquiring them.

    Are you trying to hack another program or something by ghosting one of its child processes?
    No, I believe I laid all my cards on the table when I gave the link as to what I was doing.

    I'm going to go back and try Salem's method. I'll tell you all how it goes.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Setting OS Environment Variables
    By ImNotMad in forum C Programming
    Replies: 4
    Last Post: 10-23-2003, 02:07 AM
  3. environment variables in make and gcc?
    By mart_man00 in forum C Programming
    Replies: 0
    Last Post: 07-23-2003, 03:02 PM
  4. Having trouble correctly setting DJGPP's env. variables in XP
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-05-2002, 10:51 PM