This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Accessing Windows environment variables from within C code (on C28x)

I have a TMS320C28x customer who wants to access Windows environment variables from within his C code at compile time.  For example, he has a Win environment variable called "USERNAME" that is a string identifying the person logged into the PC.  He would like to be able to access this string from his C-code, e.g.

   printf("Program was compiled by %s \n", $USERNAME);

Of course, the above code does not work (I played around with this for awhile before posting).

***QUESTION: Is there a way to do this?

I see the compiler has a few built-in macros such as __TIME__ and __DATE__ that access Windows information, but I don't see anything listed for a generic environment variable.

Thank you and Happy New Year.

- David

  • David,

    Section 2.4 of the C2000 Compiler User's Guide (SPRU514C) talks about controlling the compiler using environment variables. Please check and see if this is what you need...

    Hope this helps,

    Rafael

  • Rafael,

    Thank you for responding, but Section 2.4 of SPRU514C does not address my issue.  Section 2.4 is talking about setting a specific Windows environment variable that the compiler uses to get default build options.  My issue is that I want my C-code to be able to access a Windows environment variable at build time, and use the value of the variable in the code.  This is a very different thing.

    Regards,

    David

  • Use a macro in the program, then define it on the command line from the environment variable.

    #include <stdio.h>

    main()
    {
      printf("compiled by %s\n", MYNAME);
    }

    Compile as follows (this is for Linux, probably similar for DOS):

    cl2000 -DMYNAME=\"$USER\" main.c

    The little magic with the backslashes and quotes is to turn the environment variable into a string; you could also do this inside the program by using the preprocessor's # operator:

    #define QUOTE(x) STR(x)
    #define STR(x)   #x

    printf("compiled by %s\n", QUOTE(MYNAME));

    Compile as:

    cl2000 -DMYNAME=$USER main.c

    The extra indirection in the definition of QUOTE is because of how C defines the # operator: its argument is not macro-expanded.

     -Alan

  • David,

    I was playing with it as well. In DOS/Windows world it works if you define the symbol as: -DUSER=\"%USERNAME%\"

    Cheers,

    Rafael

     

  • Alan,

    Thank you for helping.  What you suggested worked after a slight modification.  For the record, I am using C28x compiler v5.2.9, and WinXP+SP3.  I implemented your first method like this:

    Doing "set" at the DOS command prompt, I see an environment variable 'USERNAME=a0123456'.

    My C-code has:
        #include <stdio.h>
        printf("Compiled by %s\n", MYNAME);

    In the CCS project build options, compiler options, predefined symbols, I set:

              MYNAME=\"${USERNAME}\"

    Note the braces above.  These are needed or it doesn't work.  When I run the program, in the CCSv4 console window I get:

           Compiled by a0123456

    which is what I wanted.

    Many thanks,

    David

     

  • Rafael,

    Yep, your method works too.  That is:

              MYNAME=\"%USERNAME%\"

    in the CCS project build options, compiler predefined symbols (i.e., --define option).

    I'm all set here.  Thanks and regards,

    David