Results 1 to 5 of 5
  1. #1
    EmuTalk Member Poobah's Avatar
    Join Date
    Feb 2004
    Posts
    475

    Changing environment variables in Windows.

    I was recently trying to make a program that appends a directory to the PATH environment variable, but I'm having trouble. I've been using the SET command, but it doesn't actually change the environment variables when it's called from within another program. So if I do something like:
    Code:
    SET "PATH=%PATH%;C:\Program Files"
    within the program using C's system() function, the environment variable will not change at all, despite the fact that SET returns 0 to the program. Calling the exact same command in Windows' command prompt works fine. I've tried writing the command to a batch file, which didn't work, and I've also tried sending it to cmd.exe, which also didn't work.

    So could someone recommend an alternative to the SET comand for changing environment variables?

    My Site
    Batch-extract and re-archive individual ROMs from GoodMerged 7z archives into individual ZIP or JMA files. (Now a Windows GUI program!)

    Drug Free For Life: 98% of the teenage population has
    tried drugs at least once. If you are one of the 2%
    who hasn't, copy and paste this into your signature.


    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    EmuTalk Member
    Join Date
    May 2003
    Location
    Montreal
    Posts
    6
    Setting environment variable is a little bit tricky because it only change it for the current process. In your example, you started a 2nd process that did the change but the change 'died' when that process closed. There was no modification to the system or your own process.

    If you only need the change to be made in your own process, use the 'C' function "putenv". Or, if you are programming in .Net, use System.Environment.SetEnvironmentVariabl e (only available in framework 2.0).

    On the other hand, if you need the change to be permanent, you need to modify the registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControl Set\Control\Session Manager\Environment\Path

    After the change, you need to reboot the computer to make the change available to all process.

    Hope this help.

  3. #3
    Plugin coder / Betatester Falcon4ever's Avatar
    Join Date
    Mar 2002
    Location
    The Netherlands
    Posts
    1,253
    just go to your system settings (rightclick on the This Computer icon in the explorer) choose tab advanced, then you see a button ...variables (i'm on a dutch system win xp pro... so i guess it's named a bit different) and you can add your path there... (don't mess with the reg

    ah found a general one:
    http://www.phys.ufl.edu/~coldwell/cl..._home_path.htm

  4. #4
    EmuTalk Member Poobah's Avatar
    Join Date
    Feb 2004
    Posts
    475
    Quote Originally Posted by Warcon
    Setting environment variable is a little bit tricky because it only change it for the current process. In your example, you started a 2nd process that did the change but the change 'died' when that process closed. There was no modification to the system or your own process.

    If you only need the change to be made in your own process, use the 'C' function "putenv". Or, if you are programming in .Net, use System.Environment.SetEnvironmentVariabl e (only available in framework 2.0).

    On the other hand, if you need the change to be permanent, you need to modify the registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControl Set\Control\Session Manager\Environment\Path

    After the change, you need to reboot the computer to make the change available to all process.

    Hope this help.
    I don't think Putenv() or Setenv() are ANSI-compliant, because they aren't available in MinGW. I was hoping there'd be an alternative to editing the registry, because I don't know much about Windows programming. Thanks for the help, though.

    Quote Originally Posted by Falcon4ever
    just go to your system settings (rightclick on the This Computer icon in the explorer) choose tab advanced, then you see a button ...variables (i'm on a dutch system win xp pro... so i guess it's named a bit different) and you can add your path there... (don't mess with the reg

    ah found a general one:
    http://www.phys.ufl.edu/~coldwell/cl..._home_path.htm
    I was looking for a method to change it in C, not through My Computer.
    My Site
    Batch-extract and re-archive individual ROMs from GoodMerged 7z archives into individual ZIP or JMA files. (Now a Windows GUI program!)

    Drug Free For Life: 98% of the teenage population has
    tried drugs at least once. If you are one of the 2%
    who hasn't, copy and paste this into your signature.

  5. #5
    Moderator smcd's Avatar
    Join Date
    Jun 2004
    Posts
    2,503
    There is no standards-compliant method (to my knowledge) so you'll have to break portability in order to do so. As mentioned above, using "set xxx=yyy" it doesn't make permanent changes and is only in effect for the current process, and perhaps spawned child processes depending on how they're launched. Editing it in the registry I've not done, but used the "My Computer" properties dialog and editing it there should put it into the registry after you save the changes and I've never had to restart in order for the changes to take effect.

Similar Threads

  1. Who emulates the ROM Body Harvest???
    By scotty in forum General N64 Emulation Discussion
    Replies: 14
    Last Post: December 27th, 2005, 00:42
  2. Running Windows 3.11 - dosbox
    By smcd in forum Other Emulation Discussion
    Replies: 21
    Last Post: April 20th, 2005, 11:23
  3. Windows .NET Server 2003 RC2 *FREE*
    By lario in forum TechTalk
    Replies: 1
    Last Post: December 27th, 2002, 00:19
  4. Comments on Windows XP SP1
    By ra5555 in forum TechTalk
    Replies: 65
    Last Post: September 21st, 2002, 19:38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •