Setting environment variables.

This is a discussion on Setting environment variables. within the C++ Programming forums, part of the General Programming Boards category; I have to make a DOS (real DOS, not a Win32 command line app) program that parse a file a ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    17

    Setting environment variables.

    I have to make a DOS (real DOS, not a Win32 command line app) program that parse a file a set some environment variables.

    I made the full program when I stuck against the apparently easy task of setting the environment variables.
    I know it exists a int putenv(const char*); function that should do the job. But it actually does not work.

    Here a little example:
    Code:
    #include <iostream>
    
    int main()
    <%using namespace std;
      int stat = putenv("HELLO=C:\\TEMP");
      if (stat == -1)
      <%
       cout<<"failed to define environment variable"<<endl;
      %>
    %>
    Try to compile this code and execute it, it won't set anything even if stat == 0 as success!

    I also tried uselessy system("set HELLO=TEST");, do anyone have an idea?

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you're not including the correct header for it. Here is a quick hit from your friendly neighborhood search engine.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    I read that pages many times before posting here, so many time that I can post a contradiction in their page.
    They write:
    On success putenv() returns 0, and -1 otherwise.
    and in the code actually check for zero as a faliure.

    The code I posted had been compiled and tested.
    IT IS NOT ABOUT MISSING HEADERS.

    Anyway,
    Code:
    #include <cstdlib>
    #include <iostream>
    
    int main()
    <%using namespace std;
      int stat = putenv("HELLO=C:\\TEMP");
      if (stat == -1)
      <%cout<<"failed to define environment variable"<<endl;
      %>
    %>
    Try to compile this code and execute it, it won't set anything even if stat == 0 as success!
    Last edited by Ezzetabi; 07-26-2005 at 09:36 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    From MSDN:
    _putenv and _wputenv affect only the environment that is local to the current process; you cannot use them to modify the command-level environment. That is, these functions operate only on data structures accessible to the run-time library and not on the environment "segment" created for a process by the operating system. When the current process terminates, the environment reverts to the level of the calling process (in most cases, the operating-system level). However, the modified environment can be passed to any new processes created by _spawn, _exec, or system, and these new processes get any new items added by _putenv and _wputenv.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    Thanks Daved. So there is no solution... :|

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Also from MSDN:
    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. This allows applications, such as the shell, to pick up your updates.
    although I doubt this will help you for your DOS needs. There very well might be another solution, but I don't know any.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    I know that registry trick, but as you said it wont help for my DOS need. :|

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    What compiler and OS are you using to compile (and test) your DOS application?

    gg
    Last edited by Codeplug; 07-25-2005 at 04:41 PM. Reason: added "and OS"

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    For true DOS this I reckon is quite involved. I'm betting that you would need to write a process that reads the config.sys and adds a line to it if its not there and reboots. You will need to write a TSR whose sole purpose is after reboot to run the process that depends on the changed environment variable unless someone else has a better idea.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    >> Try to compile this code and execute it, it won't set anything...
    >> Try to compile this code and execute it, it won't set anything...
    If your going to test any code, it should be on a DOS machine. Not too many folks round here running DOS.

    From what I've googled, there are DOS compilers and libraries in which putenv() does affect the global environment. However, this is not true of Turbo C/C++ or DJGPP.

    Further google'n revealed:
    system("set var=num") not working (yes, this was google'd)
    http://orion.planet.de/~jan/Snippets.9707/_e1c05.html
    http://garbo.uwasa.fi/pc/envutil.html

    Most of the code is for Turbo C/C++, but now you know what's involved.

    OOC, what are your writting DOS apps?

    gg

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    DJGPP is the compiler I use.

    Thanks for the links Codeplug! I imagined I had to use asm(), but seeing someone other job will surely help.

    I have to make a dos app for the first part of automatic OS installing that is still in DOS, freeDOS actually.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 04:10 AM
  2. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  3. Structures and setting variables
    By teck in forum Windows Programming
    Replies: 17
    Last Post: 11-30-2007, 05:38 PM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. Setting OS Environment Variables
    By ImNotMad in forum C Programming
    Replies: 4
    Last Post: 10-23-2003, 03:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21