Skip to main content
Participant
April 1, 2011
Question

How to set environment variable?

  • April 1, 2011
  • 1 reply
  • 3235 views

Hi,

I just succeeded starting up an external executable by using nativeprocess.

Now I have to set the portnumber as an environment variable.

After setting this portnumber, I can send queries to the executable.

But I don't now how to set this environment variable.

You can see my try in bold:

  • Starting the executable (an extract of the code):

public var portnumber;

file = file.resolvePath("C:\\Program Files\\CM Synergy 6.3\\bin\\start_dbase.exe");    #start executable

process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, getportnumber);    #get portnumber as output of the executable

  • The next step is to define the variable portnumber:

file = file.resolvePath("C:\\WINDOWS\\system32\\cmd.exe");

processArgs[0] = "/C";

processArgs[1] = "set";

processArgs[2] = "portnumber=";

processArgs[3] = portnumber;

  • When I try to print out all environment variables, portnumber is not in:

file = file.resolvePath("C:\\WINDOWS\\system32\\cmd.exe");

processArgs[0] = "/C";

processArgs[1] = "set";

If I replace the last executable with my real command (running the query), the executable is giving the error that my portnumber is not set.

Is it possible? I have also tried it with setx.exe. Even running a python script where I set os.environ is not working.

I have the feeling that the command is executed but that the variable is not remembered for the next executable.

This topic has been closed for replies.

1 reply

Participant
April 5, 2011

Hi,

I was playing with this lately too. You should try sending the command on standard input of the cmd.exe process instead of using arguments.

So start the process without any arguments.

var file:File = new File("C:/WINDOWS/System32/cmd.exe");

var si:NativeProcessStartupInfo = new NativeProcessStartupInfo();
si.executable = file;


var process:NativeProcess = new NativeProcess();

process.start(si);

The command line will output some data on its standard output, you should wait for that data in an event listener. And after that send the commands to standard input like this

process.standardInput.writeUTF("set portnumber=234\n");

"\n" is to tell the command line that it is the end of the command (line break or ENTER)

I was able to send more command line commands after each other and it worked.

The thing is that the variable will last only till end of the "session" of the cmd.exe process. So you should try setx or any other syntax that can permanently set an environment variable.

Hope this will help.

boeykenkAuthor
Participant
April 5, 2011

Hello Martin,

Thanks for your input. I have tried what you suggested but it didn't work. I will post my source code. Perhaps I'm doing something wrong. After setting my variable portnumber, I would normally run a query. Instead of running the query, I'm writing out the environment variables. Unfortunately, portnumber is not in the list . I also tried using "setx" insetad of "cmd.exe set" but without any difference.

    public var portnumber;

    public function run_query():void

    {

        var file:File = new File("C:/WINDOWS/System32/cmd.exe");

        var si:NativeProcessStartupInfo = new NativeProcessStartupInfo();

        si.executable = file;

        var processArgs:Vector.<String> = new Vector.<String>();

        processArgs[0] = "/C";

        processArgs[1] = "set";

        nativeProcessStartupInfo.arguments = processArgs;

        var process:NativeProcess = new NativeProcess();

        process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);

        process.start(si);

    }

    public function set_portnumber():void

    {

        var file:File = new File("C:/WINDOWS/System32/cmd.exe");

        var si:NativeProcessStartupInfo = new NativeProcessStartupInfo();

        si.executable = file;

        var process:NativeProcess = new NativeProcess();

        process.start(si);

        process.standardInput.writeUTF("set portnumber="+portnumber+"\n")

        setTimeout(run_query, 1000);

    }

    public function onOutputData(event:ProgressEvent):void

    {

        var process:NativeProcess = event.target as NativeProcess;

        textReceived.text += process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable) + "\n";

    }

Participant
April 5, 2011

Well I think "set" will shurely not work, because it will last only till the cmd.exe process is closed. And in your code you start another instance of cmd.exe to read out the variable. But all I was able to find out about setting a permanent environment variable is that one should use "setx" instead of "set". On my rather old Windows (XP) I am unable to call setx and I was playing with this a bit more on Mac not on Windows and there the computer must restart for the variable to take effect, so maybe try the same for Windows...