1

Here is an example in source code.

When I use

SetEnvironmentVariable("key","false");

and run the compiled file inside a batch file

myfile.exe 
echo %key% 
pause

my environment variable is undefined.

How can I change this so that it will output false?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • A solution is that the environment variables with their values are written by the executable into a file, for example the folder for temporary files, which is read or executed next by the batch file. For example the executable creates `"%TEMP%\MyFileEnvs.bat"` with the line `set "key=false"` and other __SET__ command lines and the batch file contains as next line after calling the executable the line `call "%TEMP%\MyFileEnvs.bat" & del "%TEMP%\MyFileEnvs.bat"`. – Mofi Jul 07 '18 at 09:18

1 Answers1

1

Environment variables can only be passed in one direction: from parent to child processes. In your case, you are trying to go the inverse path, which is extremely difficult.

Instead, you can use a return value of your program

int main(int, char**)
{
    // ...

    return 123; // some error code maybe
}

to easily communicate with the parent process, but this is limited to integers and you might want to respect existing conventions on the meaning of these return values. Another possibility is to let the main program write to the standard output, which can then be parsed within the batch file for further processing.

lubgr
  • 37,368
  • 3
  • 66
  • 117