0

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.

3
  • As an aside, the time to be checking this if(EnvString) would be before you used EnvString in your sprintf.
    – lurker
    May 23, 2020 at 11:52
  • If you look at the documentation for _putenv, it states: _putenv and _wputenv affect only the environment that is local to the current process; you cannot use them to modify the command-level environment.
    – lurker
    May 23, 2020 at 11:58
  • I meant how do I change environment variable in c, that was the question. May 23, 2020 at 17:30

1 Answer 1

0

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 );
}
1
  • 1
    Thanks for the reply dude. I solved the problem by another way. May 24, 2020 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.