简体   繁体   中英

How to change environment variables in C language

I was working on my game and decided to use eclipse as my compiler. I had to compile it for both platforms: x86 and x64. The trouble started there. There are many dependency files in the system path. And every time I had to change them in order to change the platform. So, I've created a line to set up my configurations faster and without affect the path itself. This is the line to add into the path that I've created: %DRIVE%\mingw\mingw%PLATFORM%\bin;%DRIVE%\Dropbox\Machine\Windows\C\Place\bin\x%PLATFORM%;%DRIVE%\Dropbox\Machine\Windows\C\PLUGIN\x%PLATFORM%\bin;

As you guys can see there are two variables there: %DRIVE% and %PLATFORM%. I wish to change them with a file that I try to create in c.

Here is the code

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

char *strremove(char *str, const char *sub) {
    char *p, *q, *r;
    if ((q = r = strstr(str, sub)) != NULL) {
        size_t len = strlen(sub);
        while ((r = strstr(p = r + len, sub)) != NULL) {
            while (p < r)
                *q++ = *p++;
        }
        while ((*q++ = *p++) != '\0')
            continue;
    }
    return str;
}

#ifndef HAVE_SETENV
int setenv(const char * variable,const char * value) {
    if(!variable || !value)return(0);
    int len = strlen(variable)+1+strlen(value)+1;
    char * EnvString = calloc(len,sizeof(char));
    sprintf(EnvString, "%s=%s", variable, value);
    if (!_putenv(EnvString)) {
        return (1);
    }
    if(EnvString)free(EnvString);
    return (0);
}
#endif

void change_platform(int argc,char ** argv) {
    char * variable = "PLATFORM",* value = "86";
    if(argc > 1){
        value = argv[1];
    }
    if (setenv(variable, value)) {
        printf("\n environmental variable successfully written");
        printf("\n value of the environmental variable written is %s",
                getenv(variable));

    } else {
        printf("\n error in writing the environmental variable");
    }
}

int main(int argc, char ** argv) {
    change_platform(argc,argv);
    getch();
    return 0;
}

My code shows the right result inside the program, but when I go and check the system environment itself, nothing changes. Am I doing something wrong. Detail: I thought it was because of mingw which isn't native from Windows, then I've created I file in Visual c++ too, but it did not work either.

Please remember it affects only the environment of the current process

getenv, _wgetenv

int main( void )
{
   char *libvar;

   // Get the value of the LIB environment variable.
   libvar = getenv( "LIB" ); // C4996
   // Note: getenv is deprecated; consider using getenv_s instead

   if( libvar != NULL )
      printf( "Original LIB variable is: %s\n", libvar );

   // Attempt to change path. Note that this only affects the environment
   // variable of the current process. The command processor's
   // environment is not changed.
   _putenv( "LIB=c:\\mylib;c:\\yourlib" ); // C4996
   // Note: _putenv is deprecated; consider using putenv_s instead

   // Get new value.
   libvar = getenv( "LIB" ); // C4996

   if( libvar != NULL )
      printf( "New LIB variable is: %s\n", libvar );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM