Thread: Setting OS Environment Variables

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Setting OS Environment Variables

    putenv() sets the program environment variables. How do you set the OS environment variables? I want to be able to set an environment variable that will stay after the program ends.

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What OS/compiler ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    16-bit DOS. I'm using the MARS 16 bit compiler.

    Your help is appreciated.

    Thanks

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    You could use 'system' with a SET command.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
      int i;
      puts ("Trying to set environment variables");
      i = system ("SET BLASTER 220 5 1");
      if (i==-1) puts ("Error setting variables");
      else puts ("Environment variables set");
      return 0;
    }

    nb: Code blatently stolen and changed to fit your question.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i = system ("SET BLASTER 220 5 1");
    This creates a sub-process which gets an environment variable set for its very short existance and then all is lost again when the sub-process exits.

    It is possible to 'hack' the parent environment in real DOS (not any win32 console, nor any POSIX operating system), but as I recall, that involved some rather tricky assembler work.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple OS
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-06-2004, 10:47 PM
  2. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  3. ASP help. Setting variables within an <a> tag
    By TravisS in forum Tech Board
    Replies: 6
    Last Post: 03-28-2003, 04:56 AM
  4. Your favourite fantasy game setting?
    By fry in forum Game Programming
    Replies: 4
    Last Post: 10-16-2002, 06:26 AM
  5. 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