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
Printable View
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
What OS/compiler ?
16-bit DOS. I'm using the MARS 16 bit compiler.
Your help is appreciated.
Thanks
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.
> 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.